Activemq Performance Optimization

Source: Internet
Author: User
Tags prefetch

Activemq runs stably and data throughput is high. If the incoming or outgoing queue is slow, check your ownCodeIs it too slow to process the obtained data.

The performance optimization mentioned in this Article is to list some important points. Please make sure your project has no problems:

1. Use spring jmstemplate

The send and convertandsend modes of jmstemplate use persistent mode even if non_persistent is set. This slows down the inbound queue speed.

Solution: replace jmstemplate with myjmstemplate.

 Public   Class Myjmstemplate Extends  Jmstemplate {  Private  Session session;  Public  Myjmstemplate (){  Super ();}  Public  Myjmstemplate (connectionfactory ){  Super  (Connectionfactory );}  Public   Void Dosend (messageproducer producer, message) Throws  Jmsexception {  If  (Isexplicitqosenabled () {producer. Send (message, getdeliverymode (), getpriority (), gettimetolive ());}  Else {Producer. Send (Message );}}  Public  Session getsession (){  Return  Session ;}  Public   Void  Setsession (session ){  This . Session = Session ;}} 

 

2. deliverymode selection. If you enter the data in the queue without considering the failure of MQ (this probability is very small), using non_persistent will significantly increase the Data Writing speed.

3. When a producer uses a transaction, the performance of the incoming queue is improved. However, if a consumer starts a transaction, the data consumption speed is significantly affected. The related code is as follows:

Session session = connection. createsession (False, Session. auto_acknowledge );

False in the Code indicates that the transaction is not started.

4. The message processing method of the consumer is optimized by the onmessage method, for example:

 Public   Class Smsmopool Implements  Messagelistener {  Private   Final   Static Logger logger = loggerfactory. getlogger (smsmopool. Class  );  Private Defaulteventpubliser moeventpublisher;  Private   Final Eventfactory = New  Defaulteventfactory ();  Private  Defaultdatagather datagather;  Private Executorservice pool = executors. newfixedthreadpool (5 ); @ Override  Public   Void Onmessage ( Final Message message) {pool.exe cute (  New  Runnable () {@ override  Public   Void  Run (){  Final Objectmessage MSG = (Objectmessage) message; serializable OBJ = Null  ;  Try  {OBJ = MSG. GetObject ();} Catch  (Jmsexception e) {logger. Error ( "An error occurred while obtaining upstream information from the Message Queue {}" , E );}  If (OBJ! = Null  ) {Datagather. incrementdatecount (masentityconstants. traffic_sms_mo_in); agenttoserverreq req = (Agenttoserverreq) OBJ;  If  (Logger. isinfoenabled () {logger.info ( "Driver --> scheduling :{}", Req. toxmlstr ();} event = Eventfactory. createmoevent (req); moeventpublisher. publishevent (event );}}});}} 

This Code uses the thread pool, and you should note that MSG. getObject (); this method is a relatively time-consuming method. You should not use GetObject () multiple times in your code ().

5. How do consumers use prefetch? The following uses the spring version as an example.

<BeanClass= "Org. Apache. activemq. Command. activemqqueue"><Constructor-ArgValue= "Data.mo? Consumer. prefetchsize = 100"/></Bean>

The prefetch quantity depends on the specific incoming queue data. The above setting is 100, which is set for the inbound queue speed of 2000/sec.
In addition, if it is a slow consumer, it can be set to 1.

6. Check your MQ data throughput speed to maintain a balance between production and consumption without a large backlog.

7. When activemq uses the TCP protocol, tcpnodelay = the default value is false. Setting it to true can improve performance.

Or the spring version:

  < Bean  ID  = "Mqpoolconnectionfactory"  Class  = "Org. Apache. activemq. Pool. pooledconnectionfactory"  Destroy-Method  = "Stop"  >          <  Property  Name  = "Connectionfactory"  >              <  Bean  ID = "Mqconnectionfactory"  Class  = "Org. Apache. activemq. activemqconnectionfactory"  P: useasyncsend  = "True"  P: brokerurl  = "Failover: // (TCP: // 127.0.0.1: 61616? Tcpnodelay = true )"  />          </  Property  >      </  Bean  > 

 

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.