Simple Example of activemq implementing JMS

Source: Internet
Author: User

The preliminary work is omitted from the table.

The Code is as follows:

Messagereceiver

Package JMS. activemq. myexample; import javax. JMS. connection; import javax. JMS. destination; import javax. JMS. jmsexception; import javax. JMS. message; import javax. JMS. messageconsumer; import javax. JMS. session; import javax. JMS. textmessage; import Org. apache. activemq. activemqconnectionfactory; public class messagereceiver implements runnable {private string URL; private string user; private string password; private final string queue; Public messagereceiver (string queue, string URL, string user, string password) {This. url = URL; this. user = user; this. password = password; this. queue = queue;} @ overridepublic void run () {activemqconnectionfactory connectionfactory = new activemqconnectionfactory (user, password, URL); Session session = NULL; destination receivequeue; try {connection = connectionfactory. createconnection (); Session = connection. createsession (true, session. session_transacted); receivequeue = session. createqueue (Queue); messageconsumer consumer = session. createconsumer (receivequeue); connection. start (); While (true) {message = consumer. receive (); If (Message instanceof textmessage) {textmessage receivemessage = (textmessage) message; system. out. println ("I Am a receiver and receive the following message: \ r \ n" + receivemessage. gettext ();} else {session. commit (); break;} connection. close ();} catch (jmsexception e) {e. printstacktrace () ;}} Public String geturl () {return URL;} public void seturl (string URL) {This. url = URL;} Public String getuser () {return user;} public void setuser (string user) {This. user = user;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password ;}}

Messagesender

Package JMS. activemq. myexample; import Java. util. date; import javax. JMS. connection; import javax. JMS. destination; import javax. JMS. jmsexception; import javax. JMS. messageproducer; import javax. JMS. session; import javax. JMS. textmessage; import Org. apache. activemq. activemqconnectionfactory;/*** message sender * @ author xiaochuanyu **/public class messagesender implements runnable {private string URL; private string user; private string password; private final string queue; public messagesender (string queue, string URL, string user, string password) {This. url = URL; this. user = user; this. password = password; this. queue = queue;} @ overridepublic void run () {activemqconnectionfactory connectionfactory = new activemqconnectionfactory (user, password, URL); Session session = NULL; destination sendqueue; connection = NULL; int messagecount = 0; try {connection = connectionfactory. createconnection (); connection. start (); While (true) {session = connection. createsession (true, session. session_transacted); sendqueue = session. createqueue (Queue); messageproducer sender = session. createproducer (sendqueue); textmessage outmessage = session. createtextmessage (); outmessage. settext (new date () + "The message is sent now" + messagecount + "message"); sender. send (outmessage); Session. commit (); sender. close (); If (++ messagecount) = 10) {// send 10 messages to exit break;} thread. sleep (1000);} connection. close ();} catch (jmsexception e) {e. printstacktrace ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} Public String geturl () {return URL;} public void seturl (string URL) {This. url = URL;} Public String getuser () {return user;} public void setuser (string user) {This. user = user;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password ;}}

Myactivemqdemo

package jms.activemq.myexample;public class MyActiveMQDemo {public static void main(String[] args) {String url = "tcp://localhost:61616";String user = null;String password = null;String query = "MyQueue";new Thread(new MessageSender(query,url,user,password), "Name-Sender").start();new Thread(new MessageReceiver(query,url,user,password), "Name-Receiver").start();}}

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.