在Spring-boot 中利用JMS發(fā)送消息很慢如何解決?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
自貢ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
Spring-boot JMS 發(fā)送消息慢的問題解決
1、在《ActiveMQ 基于zookeeper的主從(levelDB Master/Slave)搭建以及Spring-boot下使用》中,采用以下代碼進(jìn)行JMS消息發(fā)送:
@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
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 Mapsay(@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
關(guān)于在Spring-boot 中利用JMS發(fā)送消息很慢如何解決問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。