Spring-boot JMS 发送消息慢的解决方法
作者:YSHY 发布时间:2023-02-06 07:50:54
标签:Spring,boot,JMS,发送消息
Spring-boot JMS 发送消息慢的问题解决
1、在《ActiveMQ 基于zookeeper的主从(levelDB Master/Slave)搭建以及Spring-boot下使用》中,采用以下代码进行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


猜你喜欢
- 这篇文章主要介绍了Springboot打包部署代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 多线程编程多线程编程模式.NET 中,有三种异步编程模式,分别是基于任务的异步模式(TAP)、基于事件的异步模式(EAP)、异步编程模式(A
- 1.实现一个ItsClient 客户端用来实例化调用验证功能public class ItsClient {private static f
- 本文实例讲述了Android编程之在SD卡上进行文件读写操作的方法。分享给大家供大家参考,具体如下:很多知识只有真正理解掌握之后才能运用自如
- 一、Spring能做什么?Spring的主要目的是使J2EE易用和促进好编程习惯。倒置控制容器 Spring的设计核心是 org.sprin
- 一、前言问题阐述:在某一场景下,我们的代码在 Service 实现相同,但却在 Controller 层访问时却希望不同的前缀可以访问。如下
- 在调用一些简单的方法实现一系列的动作时,回退的问题比较重要。作为一款用户体验良好的产品而言,有回退功能将显得比较人性化,想想如果我们常用的w
- 面试题:1同步方法和同步块,哪种更好?2.如果同步块内的线程抛出异常会发生什么?1. 同步方法和同步块,哪种更好?同步块更好,这意味着同步块
- mybatis-plus 查询传入参数Map,返回List<Map>原因有时实体类属性不够用,又不想写自定义VO了,所以用map
- 安全无处不在,趁着放假读了一下 Shiro 文档,并记录一下 Shiro 整合 Spring Boot 在数据库中根据角色控制访问权限简介A
- 1. kotlin 数值型fun main() { // 整数型 val a: Byte
- 本文实例为大家分享了Android实现密码明密文切换的具体代码,供大家参考,具体内容如下小眼睛在密码栏右边!奉上我使用的素材:添加图片到re
- 在自然语言处理(NLP)研究中,NGram是最基本但也是最有用的一种比对方式,这里的N是需要比对的字符串的长度,而今天我介绍的TrieTre
- 常见的EditText长按菜单如下oppo小米需求是隐藏掉其中的分享/搜索功能,禁止将内容分享到其他应用。最终解决方案这里先说下最终解决方案
- 1.概念a.是个二叉树(每个节点最多有两个子节点)b.对于这棵树中的节点的节点值左子树中的所有节点值 < 根节点 < 右子树的所
- @Scheduled不执行的原因1. 今天用@Schedule做了一个定时任务希望凌晨1点执行,代码如下@Servicepublic cla
- 题目要求思路一:模拟迭代依次判断每个节点是否合法:左子树判断是否>low,合法就向左下走,不合法往右下;右子树判断是否<high
- 众所周知Web服务器与客户端之间的通信是使用HTTP协议的。HTTP是一个客户端和服务器端请求和应答的标准(TCP)。因为HTTP协议是基于
- 本文实例讲述了java实现新浪微博Oauth接口发送图片和文字的方法。分享给大家供大家参考。具体如下:基于网上很多人利用新浪api开发新浪微
- 这篇文章主要介绍了Java并发CopyOnWrite容器原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值