当前位置: 首页 > 编程笔记 >

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

齐向笛
2023-03-14
本文向大家介绍Spring-boot JMS 发送消息慢的解决方法,包括了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 我正在将SpringWebSocket 4.2.4与sockjs和stomp一起使用,并试图在异步任务中从服务器向所有订阅者发送消息,但运气不佳 我的班级是: 但是订阅者没有得到消息 有什么帮助吗?我做错了什么:( *编辑* 我的消息代理: 当我订阅时: 谢谢 **编辑2:** 谢谢你帮我解决这个问题:)

  • 如何使用新的Spring Cloud Stream Kafka功能模型发送消息? 不推荐的方式是这样的。 但是我如何以函数式风格发送消息呢? 应用yml公司 我会自动连接MessageChannel,但对于process、process-out-0、output或类似的东西,没有MessageChannel Bean。或者我可以用供应商Bean发送消息吗?谁能给我举个例子吗?谢谢!

  • 接口说明 轻推轻应用/订阅号支持发送文本、图片、文本卡片、图文、key-value、文件、待办等消息类型。本接口针对各种消息类型和发送的对象(单发、群发以及给部分人发送)进行了定义。 注:openid是用户关注某个轻应用/订阅号后生成的唯一id,单发和给部分人发送消息必须携带此参数,可以通过如下接口来获取: 根据qt_code获取用户基本信息 获取使用者列表 通过userId获取openid 消息

  • 主动发送消息 use EasyWeChat\Kernel\Messages\TextCard; // 获取 Messenger 实例 $messenger = $app->messenger; // 准备消息 $message = new TextCard([ 'title' => '你的请假单审批通过', 'description' => '单号:1928373, ...

  • 向已经创建连接凭据的设备发送消息数据。 请求方式: |4|2|3|message|\r 参数 message 发送的消息内容 返回值: "|4|2|3|1|\r" 发送成功 "|4|2|3|2|\r" 发送失败 Arduino样例: softSerial.print("|4|2|3|DFRobot|\r");

  • Helloc我试图使用Spring WebFlux创建webscoket终结点。我希望这个endpoint返回一些事件。 为了做到这一点,我创建了事件的ConnectableFlux,并在句柄(...)方法中将其映射到Flux。但是在我把它给WebSocket会话之后,什么都没有发生——webSocket会话客户端没有收到任何东西。但是同时println(event.toString()),你可以