Activemq encounters a problem where the message cannot be confirmed.

Source: Internet
Author: User

The project needs to use activemq for the message bus. It is difficult to see the activemq in action font at the beginning. After reading it, I started to implement sender and receiver, generally, after implementation, it is okay, but after implementation, although he receives messages normally, but does not confirm the message, it is strange to think about such a big bug in the message middleware of Niu xx, it must be impossible. Then I thought I was wrong. I learned to query the information, but I did not have to worry about it. Let's get started.

First of all, there is nothing to say about message sending. You can query some documents and learn about some parameter settings. Here is an example.

Then, the hacker receives the message:

1. There are two ways to receive messages,

One is to directly use consumer. setmessagelistener (**) to set the listener. He is blocking and keeps listening to the entire message queue,

The other is directly consumer. Receive (timeout); directly returns the message, you can directly process the message. It is not blocking.

2. Here is the problem I encountered.

Session session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);

The two parameters are missing.

There are two scenarios:

1
When the first parameter of createsession is true, it indicates that the created session is marked as transactional. The confirmation message is automatically processed by confirmation and correction, and the second parameter is useless.

-- I did not understand this sentence.

2

When the first parameter of createsession is false, it indicates that the created session is not marked as transactional. At this time, there are three options for message confirmation:
** The auto_acknowledge session automatically confirms a message;
** The client_acknowledge client will call the confirmation method for the received message;
** The dups_ OK _acknowledge option command session is "lazy" to confirm message transmission. It can be thought that this will cause some replication messages transmitted by the message provider to fail.

My problem is always set to true, where is the problem at http: // localhost: 8161/admin/browse. jsp? Jmsdestination = file_up_001 the message is always pending and Its redelivered is true (that is, the message has been received.

Change "true" to "false" and use the automatically confirmed session. auto_acknowledge method. The problem will be solved once the result is returned.

3 There are two methods: non_persistent and persistent.

Ms supports two message transmission methods. Messages marked as non_persistent can be delivered at most once, while messages marked as persistent are delivered using a temporary mechanism. If a JMS service is offline, persistent messages will not be lost, but will not be transmitted until the service is restored online. Therefore, the default message transmission method is non-persistent. Although the use of non-persistent messages may reduce the memory and memory required, this method is only used when you do not need to receive all messages.


Code: jmssender. Java

Package com.gz HDI. bus; import javax. JMS. connection; import javax. JMS. connectionfactory; import javax. JMS. deliverymode; import javax. JMS. destination; import javax. JMS. jmsexception; import javax. JMS. messageproducer; import javax. JMS. objectmessage; import javax. JMS. session; import Org. apache. activemq. activemqconnection; import Org. apache. activemq. activemqconnectionfactory;/*** producer (sender) of the message **/public class jmssender {Private Static connection = NULL; Private Static connectionfactory = NULL; Private Static jmssender sender = NULL; private Static string file_up_queue_name = "file_up_001"; Private Static string mq_user = activemqconnection. default_user; // The Private Static string mq_password = activemqconnection that should be encrypted by todo. default_password; private jmssender () {// initial the upper var} public Boolean send (MSG) throws jmsexception {If (connectionfactory = NULL) {// connectionfactory: Connection factory, JMS uses it to create a connection connectionfactory = new activemqconnectionfactory (mq_user, mq_password, "TCP: // localhost: 61616"); // connection from the JMS client to the JMS provider = connectionfactory. createconnection (); connection. start () ;}// session: the session = connection. createsession (Boolean. true, session. auto_acknowledge); // destination: the destination of the message. destination = session. createqueue ("file_up_001"); // messageproducer: messageproducer producer = session. createproducer (destination); // you can specify a producer that is not persistent. setdeliverymode (deliverymode. non_persistent); // send a message objectmessage object = session. createobjectmessage (); object. setobject (MSG); producer. send (object); Session. commit (); // connection. close (); system. out. println ("Commit OK"); Return true ;} /*** get an entity * @ return jmssender * @ author yinlei | 6:55:30 * @ version 0.1 */public static jmssender getinstance () {If (sender = NULL) {sender = new jmssender ();} return sender ;}}

Jmsreceiver. Java


Import javax. JMS. connection; import javax. JMS. connectionfactory; import javax. JMS. destination; import javax. JMS. jmsexception; import javax. JMS. message; import javax. JMS. messageconsumer; import javax. JMS. messagelistener; import javax. JMS. objectmessage; import javax. JMS. session; import Org. apache. activemq. activemqconnection; import Org. apache. activemq. activemqconnectionfactory; import Org. apache. log4j. logger ;/* ** Message consumer (receiver) **/public class jmsreceiver implements messagelistener, runnable {Private Static logger = logger. getlogger (jmsreceiver. class); Public void run () {startconsumer ();} public void startconsumer () {try {// connectionfactory: Connection factory, JMS uses it to create a connection connectionfactory = new activemqconnectionfactory (activemqconnection. default_user, activemqconnection. default_password, "TCP: // loca Lhost: 61616 "); // connection from the JMS client to the JMS provider; connection = connectionfactory. createconnection (); connection. start (); // session: a thread that sends or receives messages. // remember, this parameter is only false, and the second parameter is used for session = connection. createsession (Boolean. false, session. auto_acknowledge); // destination: the destination of the message. destination = session. createqueue ("file_up_001"); // consumer, Message Receiver messageconsumer consumer = ses Sion. createconsumer (destination); consumer. setmessagelistener (this);} catch (jmsexception e) {logger. Error ("Consumer Error !!! ", E); E. printstacktrace () ;}}// listens to and processes the message. Public void onmessage (message) {filemsg fm = NULL; try {If (message. getjmsredelivered ()! = True) {fm = (filemsg) (objectmessage) message ). getObject (); If (FM. process () {logger.info ("message objectid:" + FM. getobjectid () + "processed") ;}} catch (jmsexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {jmsreceiver JMS = new jmsreceiver (); New thread (JMS ). start ();}}

Message Body:

Public class filemsg {Private Static final long serialversionuid =-518929429090930161l; // application idprivate string appid; // objectidprivate string objectid; // file path private string path; Public Boolean Process () {system. out. println ("Create a objectid objecid:" + objectid); Return true;} Public String getappid () {return appid;} public void setappid (string appid) {This. appid = appid;} Public String getobjectid () {return objectid;} public void setobjectid (string objectid) {This. objectid = objectid;} Public String getpath () {return path;} public void setpath (string path) {This. path = path ;}}

Reference: http://riddickbryant.iteye.com/blog/441890

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.