一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

Spring-boot JMS 發送消息慢的解決方法

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

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

Spring-boot JMS 發送消息慢的問題解決

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

?
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);
 }
}

經使用JMeter進行壓力測試,發現JMS的發送消息特別慢。

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99免费精品 | 国产亚洲精品看片在线观看 | 欧美精品日韩一区二区三区 | 国产灌醉 | 女人把私密部位张开让男人桶 | 2020年最新国产精品视频免费 | 粉嫩极品国产在线观看免费 | 1024免费观看完整版在线播放 | 日本中文字幕在线视频站 | 亚洲欧美日韩久久一区 | 亚洲国产精品久久精品怡红院 | 我和岳的性事小说 | 思思99热久久精品在2019线 | 欧美人禽杂交狂配无删完整 | aⅴ导航站 | 猛操女人| 成人永久免费福利视频网站 | 欧美伊香蕉久久综合类网站 | 乖女的嫩奶水h文孕妇 | 亚洲爱视频 | 免费观看小视频 | 狠狠色婷婷日日综合五月 | 国产在线成人a | 欧美最猛性xxxxx男男 | 青青青国产在线观看 | 国产视频三区 | 美女脱一光二净的视频 | 美女被草漫画 | 好男人在线观看hd中字 | 小寡妇水真多好紧 | 肥胖女人一级毛片 | 精品一区二区三区免费站 | 国产亚洲精品看片在线观看 | 亚洲国产精品综合久久一线 | 亚洲精美视频 | 91桃花视频 | 国产探花视频 | 国产良家| 国内精品久久久久久久久久久久 | 日本黄色网页 | 国产情侣啪啪 |