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 {

@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 say(@PathVariable String msg, @PathVariable String name){

Map 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发送苹果消息慢_Spring-boot JMS 发送消息慢的解决方法相关推荐

  1. java 集成ibm mq 教程_Spring Boot JMS与IBM WebSphere MQ集成配置

    Spring Boot JMS与IBM WebSphere MQ集成配置 [TOC] 前言 Spring Boot作为简化Spring开发的框架,已经为我们集成了ActiveMQ和RabbitMQ.只 ...

  2. 苹果MacBook 连接电源后却显示“未充电”的解决方法

    部分电脑运行macOS 10.15.5 以上版本作业系统,且电脑上配备Thunderbolt 3 连接埠的用户发现即便已经插上充电线,屏幕上却显示未充电的提示. 有些人会觉得肯定问题出在充电器或充电线 ...

  3. rabbitmq 消息长度_Spring Boot教程(29) – RabbitMQ必备基础

    RabbitMQ是使用最广泛的开源消息队列中间件之一,它是用Erlang写的,并实现了高级消息队列协议AMQP.消息队列可以在应用间交换消息,实现解耦.异步处理.削峰.缓冲等目的,应用场景还是比较丰富 ...

  4. 宝塔同时安装苹果cms海洋cms_★苹果cms常见问题有哪些?100个常见问题的解决方法...

    苹果cms是搭建视频网站不错的免费cms系统,再好的系统也会遇到使用方面上的问题,我的主题网在日常中汇总了100多个常见问题来给大家分享交流,后期还会不定期更新遇到的新问题来交流.如果你也是苹果cms ...

  5. java ibm 2035,C# java 连接 IBM MQ时出现 2035 或 2013认证错误的解决方法

    当C# 或 java 连接 IBM MQ 是出现 2035 或 2013的错误时 java的错误提示: com.ibm.msg.client.jms.DetailedJMSSecurityExcept ...

  6. win系统C++的udp通信(接收并发送)详细教程、win下inet_pton和inet_ntop无法使用解决方法

    对UDP编程0基础的可以参考这篇记录博文. 我做的是同一个程序中接收指定IP地址和端口号的信息作为输入,通过程序的算法进行处理,处理后的信息再通过另一个指定IP地址和端口号进行发送.也就是需要做两个u ...

  7. 苹果7和8的区别_★苹果cms常见问题有哪些?100个常见问题的解决方法

    苹果cms是搭建视频网站不错的免费cms系统,再好的系统也会遇到使用方面上的问题,我的主题网在日常中汇总了100多个常见问题来给大家分享交流,后期还会不定期更新遇到的新问题来交流.如果你也是苹果cms ...

  8. redis消息发布和订阅的运用与常见错误解决方法

    本文Redis所在系统:Linux,详细安装步骤可参考:https://www.cnblogs.com/zhaoyan001/p/6143170.html 本文运用程序所在系统:windows7 一  ...

  9. 为什么玩我的世界老提示Java se错误_我的世界error错误信息 error could解决方法

    我的世界是一个及其开放的沙盒游戏,而在这个游戏中有不少的问题,比如说遇到error该如何解决呢,看小编给大家带来的我的世界error错误的解决方法,希望大家喜欢. error应用程序错误信息.包括&q ...

  10. win8.1已阻止java_win8系统下打开java程序时出现应用程序已被安全设置阻止的解决方法...

    今天和大家分享一下win7系统下打开java程序时出现应用程序已被安全设置阻止问题的解决方法,在使用win7系统的过程中经常不知道如何去解决win7系统下打开java程序时出现应用程序已被安全设置阻止 ...

最新文章

  1. Centos运行级别和开机过程
  2. HCTL-2020正交码读写芯片
  3. java和ffmpeg使用内存转码_FFMPEG基于内存的转码实例
  4. 多字段回溯 mysql_回溯算法 | 追忆那些年曾难倒我们的八皇后问题
  5. java空间大战,看这篇足矣了!
  6. 数组排序和集合排序的使用
  7. OpenCV 调整图像亮度与对比度
  8. 市场调研报告-加工食品包装市场现状及未来发展趋势
  9. win系统磁盘管理知识
  10. 调用百度地图或是高德地图直接导航
  11. 米6 / MIUI10 自带电子邮件应用设置Exchange邮箱
  12. win10网页找不到服务器dns,win10无法找到dns地址是怎么回事|win10无法找到dns地址如何解决...
  13. java concurrent 探秘
  14. 根据文件名批量生成文件夹
  15. TOM、网易、腾讯企业邮箱 | 邮箱登录入口你了解多少?
  16. 【Bio】基础生物学 - 基本氨基酸 amino acids
  17. zabbix 企业级网络监控解决方案
  18. 中国木材3D打印机市场行业投资分析及前景趋势预测报告2022-2028年
  19. 什么是 API?电商API接口的用处是什么?
  20. SQL Server 数据恢复

热门文章

  1. 华南理工计算机基础知识题,华南理工_计算机应用基础_随堂练习答案(2017年)
  2. python fft库有哪些_Python图像处理库PIL中快速傅里叶变换FFT的实现(一)
  3. python视频延迟严重_【Python】改善 VideoCapture 的影像延迟
  4. 石头剪刀布python代码_我的第一个python程序,石头剪刀布猜拳游戏
  5. iOS 计算两个日期之间的差值
  6. [20190402]对比_mutex_wait_scheme不同模式cpu消耗.txt
  7. 什么是SOLID原则(第3部分)
  8. 20145240《网络对抗》MSF基础应用
  9. 【LeetCode】142 - Linked List Cycle II
  10. 如何居中一个浮动元素?