时间:2021-05-19
Spring-boot JMS 发送消息慢的问题解决
1、在《ActiveMQ 基于zookeeper的主从(levelDB Master/Slave)搭建以及Spring-boot下使用》中,采用以下代码进行JMS消息发送:
@Servicepublic class Producer { @Autowired private JmsMessagingTemplate jmsTemplate; public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination, message); }}经使用JMeter进行压力测试,发现JMS的发送消息特别慢。
2、下面通过自定义CachingConnectionFactory解决。
(1)SenderConfig.java
package com.example.springbootactivemq.jms;import org.apache.activemq.ActiveMQConnectionFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.connection.CachingConnectionFactory;import org.springframework.jms.core.JmsTemplate;/** * Created by yan on 2017/8/3. */@Configurationpublic class SenderConfig { @Value("${spring.activemq.broker-url}") private String brokerUrl; @Bean public ActiveMQConnectionFactory activeMQConnectionFactory() { ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); activeMQConnectionFactory.setBrokerURL(brokerUrl); return activeMQConnectionFactory; } @Bean public CachingConnectionFactory cachingConnectionFactory() { return new CachingConnectionFactory(activeMQConnectionFactory()); } @Bean public JmsTemplate jmsTemplate() { return new JmsTemplate(cachingConnectionFactory()); } @Bean public Sender sender() { return new Sender(); }}(2)Sender.java
package com.example.springbootactivemq.jms;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import javax.jms.TextMessage;/** * Created by yan on 2017/8/3. */public class Sender { @Autowired private JmsTemplate jmsTemplate; public void send(final String destination, final String message){ this.jmsTemplate.convertAndSend(destination, message); }}(3)Receiver.java
package com.example.springbootactivemq.jms;import org.springframework.jms.annotation.JmsListener;import org.springframework.jms.listener.SessionAwareMessageListener;import org.springframework.jms.support.JmsUtils;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;/** * Created by yan on 2017/8/3. */public class Receiver implements SessionAwareMessageListener<TextMessage> { @JmsListener(destination = "${queue.destination}") public void receive(String message) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } }}(4)ReceiverConfig.java
package com.example.springbootactivemq.jms;import org.apache.activemq.ActiveMQConnectionFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.annotation.EnableJms;import org.springframework.jms.config.DefaultJmsListenerContainerFactory;/** * Created by yan on 2017/8/3. */@Configuration@EnableJmspublic class ReceiverConfig { @Value("${spring.activemq.broker-url}") private String brokerUrl; @Bean public ActiveMQConnectionFactory activeMQConnectionFactory() { ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); activeMQConnectionFactory.setBrokerURL(brokerUrl); return activeMQConnectionFactory; } @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(activeMQConnectionFactory()); factory.setConcurrency("3-10"); return factory; } @Bean public Receiver receiver() { return new Receiver(); }}(5)TestCtrl.java
package com.example.springbootactivemq.test;import com.example.springbootactivemq.jms.Sender;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * Created by yan on 2017/8/2. */@RestController@RequestMapping( value = "/test", headers = "Accept=application/json", produces = "application/json;charset=utf-8")public class TestCtrl { @Autowired private Sender sender; @Value("${queue.destination}") private String destination; @RequestMapping( value = "/say/{msg}/to/{name}", method = RequestMethod.GET ) public Map<String, Object> say(@PathVariable String msg, @PathVariable String name){ Map<String, Object> map = new HashMap<>(); map.put("msg", msg); map.put("name", name); sender.send(destination, msg); return map; }}(6)application.properties
spring.activemq.broker-url=failover:(tcp://192.168.3.10:61616,tcp://192.168.3.11:61616,tcp://192.168.3.12:61616)spring.activemq.in-memory=truespring.activemq.pool.enabled=falsespring.activemq.user=adminspring.activemq.password=adminqueue.destination=test.queuequeue.concurrency=3-10以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
spring-boot是基于spring框架的,它并不是对spring框架的功能增强,而是对spring的一种快速构建的方式。spring-boot应用程序提供
了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文
1.什么是spring-boot-devtoolsspring-boot-devtools是spring-boot项目开发时的一个热部署工具,安装了spring
女朋友他们项目用了spring-boot,以spring-boot-parent作为parent:org.springframework.bootspring-
SpringBoot官方文档http://docs.spring.io/spring-boot/docs/current/reference/htmlsingl