Activemq Entry Example

Source: Internet
Author: User

The Message notification mechanism may be used when various organizations in an enterprise collaborate with each other. For example, if something is updated, You can notify the index.

Multiple implementations of JMS are available in Java. Activemq in Apache is a good choice. Activemq is the most popular and powerful open-source message bus produced by Apache. Activemq is a JMS provider that fully supports jms1.1 and J2EE 1.4 specifications. In this example, activemq is used.

It is best to know about JMS with activemq.

JMS public Point-to-Point domain Publish/subscribe Domains
Connectionfactory Queueconnectionfactory Topicconnectionfactory
Connection Queueconnection Topicconnection
Destination Queue Topic
Session Queuesession Topicsession
Messageproducer Queuesender Topicpublisher
Messageconsumer Queuereceiver Topicsubscriber

JMS defines two methods: Quere (point-to-point) and topic (publish/subscribe ).

Connectionfactory is a connection factory that creates a connection.

Connection creates a session.

The session creates messageproducer (used to send messages) and messageconsumer (used to receive messages ).

Destination is the Message destination.

For details, refer to the JMS specifications (Chinese version) on the Internet ).

Download the apache-activemq-5.3.0. Http://activemq.apache.org/download.html

, Decompress the package, and double-click bin/activemq. bat. After running, you can go to http: // localhost: 8161/admin

Observe. There are also demos, http: // localhost: 8161/demo

. Add the activemq-all-5.3.0.jar to the classpath.

JMS sending code:

Public static void main (string [] ARGs) throws exception {<br/> connectionfactory = new activemqconnectionfactory (); </P> <p> connection = connectionfactory. createconnection (); <br/> connection. start (); </P> <p> session = connection. createsession (Boolean. true, session. auto_acknowledge); <br/> destination Destination = session. createqueue ("My-queue"); </P> <p> messageproducer producer = session. createproducer (destination); <br/> for (INT I = 0; I <3; I ++) {<br/> mapmessage message = session. createmapmessage (); <br/> message. setlong ("count", new date (). gettime (); <br/> thread. sleep (1000); <br/> // send a message through the message producer <br/> producer. send (Message); <br/>}< br/> session. commit (); <br/> session. close (); <br/> connection. close (); <br/>}

 

JMS receiving code:

Public static void main (string [] ARGs) throws exception {<br/> connectionfactory = new activemqconnectionfactory (); </P> <p> connection = connectionfactory. createconnection (); <br/> connection. start (); </P> <p> final session = connection. createsession (Boolean. true, session. auto_acknowledge); <br/> destination Destination = session. createqueue ("My-queue"); </P> <p> messageconsumer consumer = session. createconsumer (destination); <br/> int I = 0; <br/> while (I <3) {<br/> I ++; <br/> mapmessage message = (mapmessage) consumer. receive (); <br/> session. commit (); </P> <p> // todo something .... <br/> system. out. println ("received message:" + new date (message. getlong ("count"); <br/>}</P> <p> session. close (); <br/> connection. close (); <br/>}

 

 

 

Example of sending/receiving five kinds of JMS messages

From: http://chenjumin.javaeye.com/blog/687124
 

1. Send messages

// Connection factory <br/> connectionfactory connfactory = new activemqconnectionfactory (<br/> activemqconnection. default_user, <br/> activemqconnection. default_password, <br/> "TCP: // localhost: 61616"); </P> <p> // connect to the JMS provider <br/> connection conn = connfactory. createconnection (); <br/> Conn. start (); </P> <p> // transaction session, automatic confirmation message <br/> session = Conn. createsession (true, session. auto_acknowledge); </P> <p> // Message destination <br/> destination Destination = session. createqueue ("queue. hello "); </P> <p> // message producer <br/> messageproducer producer = session. createproducer (destination); <br/> producer. setdeliverymode (deliverymode. non_persistent); // non-persistent </P> <p> // text message <br/> textmessage = session. createtextmessage ("text message"); <br/> producer. send (textmessage); </P> <p> // key-Value Pair message <br/> mapmessage = session. createmapmessage (); <br/> mapmessage. setlong ("Age", new long (32); <br/> mapmessage. setdouble ("sarray", new double (5867.15); <br/> mapmessage. setstring ("username", "key-Value Pair message"); <br/> producer. send (mapmessage); </P> <p> // stream message <br/> streammessage = session. createstreammessage (); <br/> streammessage. writestring ("streammessage stream message"); <br/> streammessage. writelong (55); <br/> producer. send (streammessage); </P> <p> // byte message <br/> string S = "bytesmessage byte message"; <br/> bytesmessage = session. createbytesmessage (); <br/> bytesmessage. writebytes (S. getbytes (); <br/> producer. send (bytesmessage); </P> <p> // object message <br/> User user = new user ("CJM", "Object message "); // The user object must implement the serializable interface <br/> objectmessage = session. createobjectmessage (); <br/> objectmessage. setobject (User); <br/> producer. send (objectmessage); </P> <p> session. commit (); // in a transaction session, only after the commit, the message will actually reach the destination <br/> producer. close (); <br/> session. close (); <br/> Conn. close ();

2. Message receiving: receives messages through the message listener.

Public class extends er implements messagelistener {<br/> private Boolean stop = false; </P> <p> Public void execute () throws exception {<br/> // connection factory <br/> connectionfactory connfactory = new activemqconnectionfactory (<br/> activemqconnection. default_user, <br/> activemqconnection. default_password, <br/> "TCP: // localhost: 61616"); </P> <p> // connect to the JMS provider <br/> connection conn = connfactory. createconnecti On (); <br/> Conn. start (); </P> <p> // transaction session, automatic confirmation message <br/> session = Conn. createsession (true, session. auto_acknowledge); </P> <p> // source of the message <br/> destination Destination = session. createqueue ("queue. hello "); </P> <p> // message consumer <br/> messageconsumer consumer = session. createconsumer (destination); <br/> consumer. setmessagelistener (this); </P> <p> // wait for receiving the message <br/> while (! Stop) {<br/> thread. sleep (5000); <br/>}</P> <p> session. commit (); </P> <p> consumer. close (); <br/> session. close (); <br/> Conn. close (); <br/>}</P> <p> Public void onmessage (message m) {<br/> try {<br/> If (M instanceof textmessage) {// receive text messages <br/> textmessage message = (textmessage) m; <br/> system. out. println (message. gettext (); <br/>} else if (M instanceof mapmessage) {// receives a key-Value Pair message <br/> mapmessage m Essage = (mapmessage) m; <br/> system. out. println (message. getlong ("Age"); <br/> system. out. println (message. getdouble ("sarray"); <br/> system. out. println (message. getstring ("username"); <br/>} else if (M instanceof streammessage) {// receives stream messages <br/> streammessage message = (streammessage) m; <br/> system. out. println (message. readstring (); <br/> system. out. println (message. readlong (); <br/>} else if (M INS Tanceof bytesmessage) {// receives byte messages <br/> byte [] B = new byte [1024]; <br/> int Len =-1; <br/> bytesmessage message = (bytesmessage) m; <br/> while (LEN = message. readbytes (B ))! =-1) {<br/> system. out. println (new string (B, 0, Len); <br/>}< br/>} else if (M instanceof objectmessage) {// receive object message <br/> objectmessage message = (objectmessage) m; <br/> User user = (User) message. getObject (); <br/> system. out. println (user. getUserName () + "_" + User. getPassword (); <br/>}else {<br/> system. out. println (m); <br/>}</P> <p> stop = true; <br/>} catch (jmsexception e) {<br/> stop = true; <br/> E. printstacktrace (); <br/>}< br/>}

 

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.