Spring-boot JMS sending messages slow problem resolution

Source: Internet
Author: User

1: ActiveMQ Zookeeper-based master-slave (LevelDB master/slave) construction and Spring-boot use (http://www.cnblogs.com/yshyee/p/7277801. HTML), the following code is used to send the JMS message:

@Service  Public class Producer {    @Autowired    private  jmsmessagingtemplate jmstemplate;      Public void Final String message) {        jmstemplate.convertandsend (destination, message);}    }

After using JMeter for stress testing, it was found that JMS sent messages were particularly slow.

2: Resolve by customizing cachingconnectionfactory below.

(1) Senderconfig.java

 PackageCOM.EXAMPLE.SPRINGBOOTACTIVEMQ.JMS;Importorg.apache.activemq.ActiveMQConnectionFactory;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.jms.connection.CachingConnectionFactory;Importorg.springframework.jms.core.JmsTemplate;/*** Created by Yan on 2017/8/3.*/@Configuration Public classsenderconfig {@Value ("${spring.activemq.broker-url}")    PrivateString Brokerurl; @Bean Publicactivemqconnectionfactory activemqconnectionfactory () {activemqconnectionfactory ActiveMQConnectionFactory 
    =Newactivemqconnectionfactory ();        Activemqconnectionfactory.setbrokerurl (Brokerurl); returnactivemqconnectionfactory; } @Bean Publiccachingconnectionfactory cachingconnectionfactory () {return Newcachingconnectionfactory (Activemqconnectionfactory ()); } @Bean Publicjmstemplate jmstemplate () {return Newjmstemplate (Cachingconnectionfactory ()); } @Bean PublicSender Sender () {return NewSender (); }}

(2) Sender.java

 PackageCOM.EXAMPLE.SPRINGBOOTACTIVEMQ.JMS;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.jms.core.JmsTemplate;

Importjavax.jms.JMSException;ImportJavax.jms.Message;Importjavax.jms.Session;ImportJavax.jms.TextMessage;/*** Created by Yan on 2017/8/3.*/ Public classSender {@AutowiredPrivatejmstemplate jmstemplate; Public voidSendFinalString destination,FinalString message) { This. Jmstemplate.convertandsend (destination, message); }}

(3) Receiver.java

 PackageCOM.EXAMPLE.SPRINGBOOTACTIVEMQ.JMS;ImportOrg.springframework.jms.annotation.JmsListener;ImportOrg.springframework.jms.listener.SessionAwareMessageListener;Importorg.springframework.jms.support.JmsUtils;Importjavax.jms.JMSException;ImportJavax.jms.MessageProducer;Importjavax.jms.Session;ImportJavax.jms.TextMessage;/*** Created by Yan on 2017/8/3.*/ Public classReceiverImplementsSessionawaremessagelistener<textmessage>{@JmsListener (destination= "${queue.destination}")     Public voidreceive (String message) {Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); }    }}

(4) Receiverconfig.java

 PackageCOM.EXAMPLE.SPRINGBOOTACTIVEMQ.JMS;Importorg.apache.activemq.ActiveMQConnectionFactory;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.jms.annotation.EnableJms;Importorg.springframework.jms.config.DefaultJmsListenerContainerFactory;/*** Created by Yan on 2017/8/3.*/@Configuration @enablejms Public classreceiverconfig {@Value ("${spring.activemq.broker-url}")    PrivateString Brokerurl; @Bean Publicactivemqconnectionfactory activemqconnectionfactory () {activemqconnectionfactory ActiveMQConnectionFactory 
    =Newactivemqconnectionfactory ();        Activemqconnectionfactory.setbrokerurl (Brokerurl); returnactivemqconnectionfactory; } @Bean Publicdefaultjmslistenercontainerfactory jmslistenercontainerfactory () {Defaultjmslistenercontainerfactory Factory =Newdefaultjmslistenercontainerfactory ();        Factory.setconnectionfactory (Activemqconnectionfactory ()); Factory.setconcurrency ("3-10"); returnFactory; } @Bean PublicReceiver Receiver () {return NewReceiver (); }}

(5) Testctrl.java

 Packagecom.example.springbootactivemq.test;ImportCom.example.springbootactivemq.jms.Sender;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RestController;ImportJava.util.HashMap;ImportJava.util.Map;/*** Created by Yan on 2017/8/2.*/@RestController @requestmapping (value= "/test", Headers= "Accept=application/json", produces= "Application/json;charset=utf-8") Public classTestctrl {@AutowiredPrivateSender Sender ; @Value ("${queue.destination}")    PrivateString destination; @RequestMapping (Value= "/say/{msg}/to/{name}", Method=requestmethod.get) PublicMap<string, object>Say (@PathVariable string msg, @PathVariable string name) {Map<string, object> map =NewHashmap<>(); Map.put ("MSG", MSG); Map.put ("Name", name);        Sender.send (destination, msg); returnmap; }}

(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=falsespring.activemq.user=  Adminspring.activemq.password=adminqueue.destination=test.queuequeue.concurrency= 3-10

Spring-boot JMS sending messages slow problem resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.