Spring-boot JMS 发送消息慢的解决方法

作者:YSHY 时间:2023-02-06 07:50:54 

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
标签:Spring,boot,JMS,发送消息
0
投稿

猜你喜欢

  • 详解C# WebApi 接口测试工具:WebApiTestClient

    2022-05-15 07:58:44
  • MyBatis的 config.xml标签

    2021-07-18 02:01:34
  • Java中二维数组的正确使用方法介绍

    2023-11-19 16:14:18
  • 如何安装系统认证签名过的APK

    2023-07-24 21:35:40
  • C#获取Windows进程监听的TCP/UDP端口实例

    2021-11-20 13:06:21
  • java 中类似js encodeURIComponent 函数的实现案例

    2023-03-20 13:13:50
  • Android APK使用Debug签名重新打包 Eclipse更改默认Debug签名

    2022-03-07 13:27:12
  • 基于App自适应draw9patch不失真背景的方法详解

    2022-04-06 17:36:51
  • Java编程实现月食简单代码分享

    2022-12-27 12:33:24
  • Android sqlite设置主键自增长的方法教程

    2023-01-26 16:16:00
  • c#开发cad预览图块步骤详解

    2022-12-30 10:49:07
  • 设计简单的Android图片加载框架

    2023-08-06 22:23:18
  • Mybatis-plus foreach拼接字符串查询无数据返回问题

    2022-09-26 17:29:02
  • Java中的stream流的概念解析及实际运用总结

    2022-06-10 23:19:10
  • Java实现动态获取图片验证码的示例代码

    2023-07-24 22:32:05
  • C#归并排序的实现方法(递归,非递归,自然归并)

    2023-10-03 01:00:36
  • C# 中DateTime 的使用技巧汇总

    2022-04-17 07:32:05
  • Android提醒微技巧你真的了解Dialog、Toast和Snackbar吗

    2023-03-08 14:15:44
  • java递归实现汉诺塔步骤介绍

    2022-03-03 09:44:17
  • 快速学习c# 枚举

    2022-07-19 18:12:12
  • asp之家 软件编程 m.aspxhome.com