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();}}