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

 更新时间:2017年08月04日 08:39:19   作者:YSHY  
这篇文章主要为大家详细介绍了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

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

相关文章

  • Java设计模式中的建造者(Builder)模式解读

    Java设计模式中的建造者(Builder)模式解读

    这篇文章主要介绍了Java设计模式中的建造者(Builder)模式解读, 建造者模式是一种创建对象的设计模式,它通过将对象的构建过程分解为多个步骤,并使用一个建造者类来封装这些步骤,从而使得对象的构建过程更加灵活和可扩展,需要的朋友可以参考下
    2023-10-10
  • 解析Java图形化编程中的文本框和文本区

    解析Java图形化编程中的文本框和文本区

    这篇文章主要介绍了Java图形化编程中的文本框和文本区,是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-10-10
  • springboot单文件下载和多文件压缩zip下载的实现

    springboot单文件下载和多文件压缩zip下载的实现

    这篇文章主要介绍了springboot单文件下载和多文件压缩zip下载的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • SpringBoot实现Md5对数据库数据加密的示例

    SpringBoot实现Md5对数据库数据加密的示例

    本文主要介绍了SpringBoot实现Md5对数据库数据加密的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • 详解如何使用Java编写图形化的窗口

    详解如何使用Java编写图形化的窗口

    这篇文章主要介绍了如何使用Java编写图形化的窗口,是Java的本地GUI软件开发的基础,需要的朋友可以参考下
    2015-10-10
  • Spring IOC原理补充说明(循环依赖、Bean作用域等)

    Spring IOC原理补充说明(循环依赖、Bean作用域等)

    这篇文章主要介绍了Spring IOC原理补充说明(循环依赖、Bean作用域等),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • Spring Boot Actuator入门指南

    Spring Boot Actuator入门指南

    SpringBootActuator是SpringBoot提供的一系列产品级特性,用于监控应用程序、收集元数据和运行情况,通过添加依赖,可以通过HTTP或JMX与外界交互,本文介绍Spring Boot Actuator的相关知识,感兴趣的朋友一起看看吧
    2025-02-02
  • springboot实现获取客户端IP地址的示例代码

    springboot实现获取客户端IP地址的示例代码

    本文介绍了在SpringBoot中获取客户端IP地址的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-11-11
  • 一名Java高级工程师需要学什么?

    一名Java高级工程师需要学什么?

    作为一名Java高级工程师需要学什么?如何成为一名合格的工程师,这篇文章给了你较为详细的答案,需要的朋友可以参考下
    2017-08-08
  • 详解在spring中使用JdbcTemplate操作数据库的几种方式

    详解在spring中使用JdbcTemplate操作数据库的几种方式

    这篇文章主要介绍了详解在spring中使用JdbcTemplate操作数据库的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07

最新评论