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
0
投稿
猜你喜欢
- 在模板文件的表达式中,可以使用“${T(全限定类名).方法名(参数)}”这种格式来调用Java类的静态方法。开发环境:IntelliJ ID
- 本文实例讲述了C#不重复输出一个数组中所有元素的方法。分享给大家供大家参考。具体如下:1.算法描述0)输入合法性校验1)建立临时数组:与原数
- 1、CountDownLatch:一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。2、ThreadPoolE
- 无论哪种界面框架输入文本框都是非常重要的控件, 但是发现flutter中的输入框TextField介绍的虽然多,但是各个属性怎么组合满足需要
- 本篇紧接上一篇内容继续,还是从继承里的细节开始1.代码块初始化关于代码块的定义和使用因为之前已经进行过介绍,所以这里就不再赘述,我们所关注的
- 使用范围synchronized使用上用于同步方法或者同步代码块在锁实现上是基于对象去实现使用中用于对static修饰的便是class类锁使
- 前言作为大数据家族中的重要一员,在大数据以及海量数据存储方面,hbase具有重要的地方,本篇将从java对hbase的操作上,进行详细的说明
- 1、回顾一下JDK * 的核心参数如果我们要为target类创建一个【JDK * 对象】,那么我们必须要传入如下三个核心参数加载targ
- Java提示缺少返回值语句怎么办?这里我们给大家提供具体的解决方法。首先,以下面的程序为例,会看到在控制台有:错误:缺少返回语句的提示。找到
- 本文实例为大家分享了Android自定义View之组合控件,仿电商app顶部栏的相关代码,供大家参考,具体内容如下效果图:分析:左右两边可以
- 1.新建springBoot项目在前面有两种方式2.加入thymeleaf模板引擎SpringBoot推荐使用thymeleaf模板引擎语法
- 简介最近几年,各种新的高效序列化方式层出不穷,不断刷新序列化性能的上限,最典型的包括:专门针对Java语言的:Kryo,FST等等跨语言的:
- Maven setting中私有仓库配置浅析最近遇到过不少这样那样的问题,曾经做过maven的分享,但是发现当时部分内容还是太想当然了,下面
- 先给大家展示下效果图:1、验证码生成类:import java.util.Random;import java.awt.imag
- java @Value("${}")获取不到配置文件中值1、property.yml配置spring: ma
- 一、logback日志技术介绍Spring Boot中使用的日志技术为logback。其与Log4J都出自同一人,性能要优于Log4J,是L
- 在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户
- 一. 安装依赖包yum install -y wgetyum install -y gcc-c++yum install -y zlib-d
- 今天是解决报错的一天,首先在操作Springboot中的时候,有些朋友的yml显示的不是绿叶的图标,或者是配置了之后不生效的问题。第一个解决
- 本文实例讲述了C#简单输出日历的方法。分享给大家供大家参考。具体如下:用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是