"JAVA.JMS" Specifies the Jmsreplyto__java in the JMS message header

Source: Internet
Author: User
Tags gettext
Jmsreplyto

When producer sends a message, specifies a destination by specifying the Jmsreplyto attribute in its messages header, and when the consumer receives it, you can pass the getxxx () method to obtain this destination, and then to choose whether to send some message (arbitrary) to this destination, and the original producer can also choose to monitor the destination simultaneously, So that you can receive some messages sent by consumer.

Both sides of the communication can send/receive messages as both the sender and receiver by saving the value in the Jmsreplyto For example, consumer can send a confirmation message (via Jmsreplyto designated destination) to producer upon receipt of the message, and producer respond to this confirmation message.





In the following example, the first two queue is created, the sender sends a queue, the receiver replies to another queue after receiving the message, and then creates a consumer to accept the returned message.

Import javax.jms.Connection;
Import javax.jms.JMSException;
Import Javax.jms.Message;
Import Javax.jms.MessageConsumer;
Import Javax.jms.MessageListener;
Import Javax.jms.MessageProducer;
Import Javax.jms.Queue;
Import javax.jms.Session;

Import Javax.jms.TextMessage;
Import Org.apache.activemq.ActiveMQConnectionFactory;

Import Org.apache.activemq.command.ActiveMQQueue; public class Messagesendreceiveandreply {public static void main (string[] args) throws Exception {ACTIVEMQC
   
        Onnectionfactory factory = new Activemqconnectionfactory ("Vm://localhost");
        Connection Connection = Factory.createconnection ();
       
        Connection.start ();

        Message sent to this queue queue = new Activemqqueue ("Testqueue");
       
        The message reverts to this queue queue Replyqueue = new Activemqqueue ("Replyqueue");

        Final session session = Connection.createsession (false, Session.auto_acknowledge);
        Creates a message and sets its jmsreplyto to Replyqueue. Message MessagE = Session.createtextmessage ("Andy");
       
        Message.setjmsreplyto (Replyqueue);
        MessageProducer producer = Session.createproducer (queue);
           
        Producer.send (message);
        The recipient of the message Messageconsumer Comsumer = Session.createconsumer (queue);
                    Comsumer.setmessagelistener (New MessageListener () {public void OnMessage (message m) {try {
                    Create a new messageproducer to send a reply message.
                    MessageProducer producer = Session.createproducer (M.getjmsreplyto ());
                Producer.send (Session.createtextmessage ("Hello" + ((TextMessage) m). GetText ());
                catch (JMSException E1) {e1.printstacktrace ();
       
        }
            }
           
        });
        This receiver is used to receive replies to messages Messageconsumer Comsumer2 = Session.createconsumer (replyqueue); Comsumer2.setmessagelistener (New MessageListener () {public void OnMessage (MeSsage m) {try {System.out.println ((TextMessage) m). GetText ());
                catch (JMSException e) {e.printstacktrace ();
    }
            }
        }); }

}


First the message producer sends a message, "Andy", and then the consumer receives the message and responds to a message based on the message, "Hello Andy" Jmsreplyto. Finally, a consumer who receives the reply message is created on the queue of the reply, and it outputs the content of the reply.



Run the above program, you can get the following output results:
Hello Andy



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.