博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-boot JMS 发送消息慢的问题解决
阅读量:7127 次
发布时间:2019-06-28

本文共 5554 字,大约阅读时间需要 18 分钟。

1:在《ActiveMQ 基于zookeeper的主从(levelDB Master/Slave)搭建以及Spring-boot下使用》(http://www.cnblogs.com/yshyee/p/7277801.html)中,采用以下代码进行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
{ @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
say(@PathVariable String msg, @PathVariable String name){ Map
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

 

你可能感兴趣的文章
华为余承东:自产AI芯片 旗舰机比苹果、三星强
查看>>
微软Azure SQL数据仓储供优惠价格购买预留容量
查看>>
Java8ConcurrentHashMap
查看>>
数据分析Power BI数据可视化教程(三)——如何创建矩阵和表以及散点图
查看>>
NoSQL最新现状和趋势:云NoSQL数据库将成重要增长引擎
查看>>
Vue组件传值
查看>>
react-native搭建用例(非CRNA)
查看>>
HTTP协议
查看>>
github简单使用
查看>>
Python提取网站数据笔记
查看>>
隐私政策
查看>>
一些个人认为值得推荐的IT编程技术社区、博客或文章收集与分享
查看>>
排序算法性能比较
查看>>
Java设计模式-策略模式
查看>>
java B2B2C 源码 多级分销springmvc mybatis多租户电子商城系统-注册中心Eureka
查看>>
学习笔记(4.6)
查看>>
java B2B2C Springcloud多租户电子商城系统-spring-cloud-eureka
查看>>
掌握设计规范,UI设计师不得不知的三件事!
查看>>
The SQL vs NoSQL Difference: MySQL vs MongoDB
查看>>
武汉区块链软件技术公司:区块链+工业4.0对制造业的影响
查看>>