Activemq the actual combat of the queue point to point message

Source: Internet
Author: User
Tags gettext

For this type of message, which is actually a message using the Peer-to-peer (point-to-point) message model in JMS, here is a simple example
message sender

Package com.googlecode.garbagecan.jmsstudy.activemq.queue; Import org.apache.activemq.ActiveMQConnection; Import Org.apache.activemq.ActiveMQConnectionFactory; Import javax.jms.*; public class Sender {public static void main (string[] args) throws JMSException {ConnectionFactory connectionfactory = n EW activemqconnectionfactory ("tcp://localhost:61616"); Connection Connection = Connectionfactory.createconnection (); Connection.start (); Session session = Connection.createsession (false, Session.auto_acknowledge); Destination Destination = Session.createqueue ("Myqueue"); MessageProducer producer = Session.createproducer (destination); Producer.setdeliverymode (deliverymode.non_persistent); while (true) {TextMessage message = Session.createtextmessage (); Message.settext ("Message_" + System.currenttimemillis ()); Producer.send (message); SYSTEM.OUT.PRINTLN ("Sent message:" + message.gettext ()); try {thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();}} Session.close(); Connection.stop (); Connection.close (); } }

Message Recipients

Package com.googlecode.garbagecan.jmsstudy.activemq.queue; Import org.apache.activemq.ActiveMQConnection; Import Org.apache.activemq.ActiveMQConnectionFactory; Import javax.jms.*; public class Receiver {public static void main (string[] args) throws JMSException {ConnectionFactory connectionfactory = New Activemqconnectionfactory ("tcp://localhost:61616"); Connection Connection = Connectionfactory.createconnection (); Connection.start (); Session session = Connection.createsession (false, Session.auto_acknowledge); Destination Destination = Session.createqueue ("Myqueue"); Messageconsumer consumer = session.createconsumer (destination); Consumer.setmessagelistener (New MessageListener () {public void OnMessage (Message message) {TextMessage TM = (textmessag e) message; try {System.out.println ("Received message:" + tm.gettext ());} catch (JMSException e) {e.printstacktrace ();}} }); Session.close (); Connection.stop (); Connection.close (); } }

Run two classes separately, you can see the sender release of the message, receiver can be accepted.

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.