ActiveMQ (5.10.0)-Hello World

Source: Internet
Author: User

Sending a JMS message
 Public classMymessageproducer {...//Create a connection factory instanceConnectionFactory connfactory =Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_password ,             "tcp://localhost:61616"); Connection Conn=NULL; Try {        //get Connection object instanceconn =connfactory.createconnection (); //Start ConnectionConn.start (); //To create a Session object instanceSession session = Conn.createsession (true, Session.auto_acknowledge); //Create a message destinationDestination Destination = Session.createqueue ("Hello_queue"); //Create a message producerMessageProducer Msgproducer =session.createproducer (destination); //Create a Message object instanceMessage textmsg = Session.createtextmessage ("This is a test message.")); //Send Messagemsgproducer.send (textmsg); //Submit a sessionSession.commit (); } Catch(jmsexception e) {e.printstacktrace (); } finally {        //Close Connection        if(Conn! =NULL) {            Try{conn.close (); } Catch(jmsexception e) {e.printstacktrace (); }        }    }        ...}

Receiving a JMS message synchronously
 Public classMysynmessageconsumer {...//Create a connection factory instanceConnectionFactory connfactory =Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_password ,             "tcp://localhost:61616"); Connection Conn=NULL; Try {        //get Connection object instanceconn =connfactory.createconnection (); //Start ConnectionConn.start (); //To create a Session object instanceSession session = Conn.createsession (false, Session.auto_acknowledge); //Create a message destinationDestination Destination = Session.createqueue ("Hello_queue"); //Create message ConsumersMessageconsumer Msgconsumer =Session.createconsumer (destination); //Receiving MessagesTextMessage textmsg = (textmessage) msgconsumer.receive (10 * 1000); if(Textmsg! =NULL) {System.out.println (Textmsg.gettext ()); }    } Catch(jmsexception e) {e.printstacktrace (); } finally {        if(Conn! =NULL) {            Try{conn.close (); } Catch(jmsexception e) {e.printstacktrace (); }        }    }    ...}

Receiving a JMS message asynchronously
 Public classMyasynmessageconsumer {...//Create a connection factory instanceConnectionFactory connfactory =Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_password ,             "tcp://localhost:61616"); Connection Conn=NULL; Try {        //get Connection object instanceconn =connfactory.createconnection (); //Start ConnectionConn.start (); //To create a Session object instanceSession session = Conn.createsession (false, Session.auto_acknowledge); //Create a message destinationDestination Destination = Session.createqueue ("Hello_queue"); //Create message ConsumersMessageconsumer Msgconsumer =Session.createconsumer (destination); //registering a message listenerMsgconsumer.setmessagelistener (NewMessageListener () {@Override Public voidonMessage (Message msg) {textmessage textmsg=(TextMessage) msg; Try{System.out.println (Textmsg.gettext ()); } Catch(jmsexception e) {e.printstacktrace ();        }            }        }); Thread.Sleep (60 * 1000); } Catch(jmsexception e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); } finally {        //Close Connection        if(Conn! =NULL) {            Try{conn.close (); } Catch(jmsexception e) {e.printstacktrace (); }        }    }    ...}

Others
    • For the PUB/SUB model, use the Session.createtopic method to create the Destination.
    • Messageconsumer the Receive () method blocks the thread until it receives the next message while synchronizing the consumption message, the receive (long Timeout) method blocks the thread for the specified time until it receives the next message, and returns a null value if it times out The Receivenowait () method immediately receives the next message and returns a null value if there is no message in the source.

ActiveMQ (5.10.0)-Hello World

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.