温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Spring-boot 中利用JMS发送消息很慢如何解决

发布时间:2020-11-30 17:13:00 来源:亿速云 阅读:427 作者:Leah 栏目:编程语言

在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

关于在Spring-boot 中利用JMS发送消息很慢如何解决问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI