Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 697|回复: 0

Spring-boot JMS 发送消息慢的问题解决

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-5-26 02:18:47 | 显示全部楼层 |阅读模式

    1:在《ActiveMQ 基于zookeeper的主从(levelDB Master/Slave)搭建以及Spring-boot下使用》(http://www.cnblogs.com/yshyee/p/7277801.html)中,采用以下代码进行JMS消息发送:

    @Service
    public 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.
     */
    @Configuration
    public 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
    @EnableJms
    public 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=true
    spring.activemq.pool.enabled=false
    spring.activemq.user=admin
    spring.activemq.password=admin
    
    queue.destination=test.queue
    queue.concurrency=3-10

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-3-10 12:30 , Processed in 0.065860 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表