激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring-boot JMS 發(fā)送消息慢的解決方法

Spring-boot JMS 發(fā)送消息慢的解決方法

2020-12-10 14:23YSHY Java教程

這篇文章主要為大家詳細(xì)介紹了Spring-boot JMS 發(fā)送消息慢的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Spring-boot JMS 發(fā)送消息慢的問題解決

1、在《ActiveMQ 基于zookeeper的主從(levelDB Master/Slave)搭建以及Spring-boot下使用》中,采用以下代碼進(jìn)行JMS消息發(fā)送:

?
1
2
3
4
5
6
7
8
9
10
@Service
public class Producer {
 
 @Autowired
 private JmsMessagingTemplate jmsTemplate;
 
 public void sendMessage(Destination destination, final String message){
  jmsTemplate.convertAndSend(destination, message);
 }
}

經(jīng)使用JMeter進(jìn)行壓力測(cè)試,發(fā)現(xiàn)JMS的發(fā)送消息特別慢。

2、下面通過自定義CachingConnectionFactory解決。

(1)SenderConfig.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

?
1
2
3
4
5
6
7
8
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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩视频在线一区二区三区 | 日本黄色不卡视频 | 国产亚洲精品综合一区91555 | 欧美爱爱视频网站 | 精品麻豆cm视频在线看 | 黄色一级片在线观看 | 成人国产精品一区 | 久久免费视频在线 | 国产精品久久99精品毛片三a | 成人在线视频网 | 国产一区二区三区在线视频 | 久久亚洲国产精品 | 久久久中精品2020中文 | 中文字幕在线第二页 | 一区二区三区欧洲 | 久久久久久久久久久久久久国产 | 中文字幕在线观看精品 | 欧美黑大粗硬毛片视频 | 久久777国产线看观看精品 | 成人不卡一区二区 | 国产毛片视频 | 99久久久| 国产一级毛片网站 | 新久草在线视频 | 黄色网址在线播放 | 日韩精品中文字幕一区二区三区 | 久久久久久久久久久久久久国产 | cosplay裸体福利写真 | 在线日韩av电影 | 久久蜜桃香蕉精品一区二区三区 | 久久精品亚洲一区二区 | 欧美成人免费电影 | 日韩中文字幕一区二区三区 | 亚洲精品av在线 | 国产精品久久久久久久久岛 | h视频免费在线观看 | 欧美在线观看黄色 | 日日操操 | 亚洲第一成人在线视频 | 亚洲最大的成人网 | 在线观看免费污视频 |