Messagepublisher
Package JMS. activemq. myexample; import Java. util. date; import javax. JMS. connection; import javax. JMS. deliverymode; import javax. JMS. jmsexception; import javax. JMS. messageproducer; import javax. JMS. session; import javax. JMS. topic; import Org. apache. activemq. activemqconnectionfactory; public class messagepublisher implements runnable {private string URL; private string user; private string password; private stri Ng topicname; Public messagepublisher (string topicname, string URL, string user, string password) {This. url = URL; this. user = user; this. password = password; this. topicname = topicname;} @ overridepublic void run () {activemqconnectionfactory connectionfactory = new activemqconnectionfactory (user, password, URL); Session session = NULL; messageproducer sendpublisher; connection = NULL; int message Count = 0; try {connection = connectionfactory. createconnection (); connection. start (); // create a topic // topic = new activemqtopic (this. topicname); Session = connection. createsession (false, session. auto_acknowledge); Topic topic = session. createtopic (this. topicname); sendpublisher = session. createproducer (topic); While (true) {string text = new date () + "the current message is sent" + messagecount + "message"; sendpublisher. setdeli Verymode (deliverymode. non_persistent); sendpublisher. send (Session. createtextmessage (text); If (+ + messagecount) = 3) {// send 10 messages to exit break;} thread. sleep (1000);} // sendpublisher. close (); // connection. close (); system. out. println ("the publishing message thread ends !!!!!!!!!!!!!!!!!!!!!!!! ");} 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;} Public String gettopic () {return topicname ;}}
Messagesubscriber
Package JMS. activemq. myexample; import javax. JMS. connection; import javax. JMS. jmsexception; import javax. JMS. messageconsumer; import javax. JMS. session; import javax. JMS. topic; import Org. apache. activemq. activemqconnectionfactory; public class messagesubscriber implements runnable {private string URL; private string user; private string password; private string topicname; Public messagesubscriber (string topicn Ame, string URL, string user, string password) {This. url = URL; this. user = user; this. password = password; this. topicname = topicname;} @ overridepublic void run () {activemqconnectionfactory connectionfactory = new activemqconnectionfactory (user, password, URL); Session session = NULL; messageconsumer subscriber; connection = NULL; try {connection = connectionfactory. createconnection (); Session = Connection. createsession (false, session. auto_acknowledge); // create topictopic topic = session. createtopic (this. topicname); Session = connection. createsession (true, session. auto_acknowledge); subscriber = session. createconsumer (topic); subscriber. setmessagelistener (New textlistener (); connection. start (); system. out. println (thread. currentthread (). getname () + "enable"); // connection. close ();} catch (jmsexceptio N e) {If (connection! = NULL) {try {connection. close ();} catch (jmsexception E1) {// todo auto-generated catch blocke1.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;} Public String gettopic () {return topicname ;}}
Textlistener
Package JMS. activemq. myexample; import javax. JMS. *;/*** text message listener ** @ author xxxx
**/Public class textlistener implements messagelistener {/*** casts the message to a textmessage and displays its text. ** @ Param message * the incoming message */Public void onmessage (message) {textmessage MSG = NULL; try {If (Message instanceof textmessage) {MSG = (textmessage) message; system. out. println ("reading message:" + MSG. gettext ();} else {system. out. println ("message of wrong type:" + message. getclass (). getname () ;}} catch (jmsexception e) {system. out. println ("jmsexception in onmessage ():" + E. tostring ();} catch (throwable t) {system. out. println ("exception in onmessage ():" + T. getmessage ());}}}
Myactivemqdemo
package jms.activemq.myexample;import javax.jms.JMSException;public class MyActiveMQDemo {public static void main(String[] args) throws InterruptedException, JMSException {String url = "tcp://localhost:61616";String user = null;String password = null;String query = "MyQueueA";String topic = "TestTopic";//new Thread(new MessageSender(query,url,user,password), "Name-Sender").start();//new Thread(new MessageReceiver(query,url,user,password), "Name-Receiver1").start();//new Thread(new MessageReceiver(query,url,user,password), "Name-Receiver2").start();//new Thread(new MessageReceiver(query,url,user,password), "Name-Receiver3").start();//new Thread(new MessageReceiver(query,url,user,password), "Name-Receiver4").start();//new Thread(new MessageReceiver(query,url,user,password), "Name-Receiver5").start();new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber1").start();new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber2").start();new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber3").start();new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber4").start();new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber5").start();Thread.sleep(5000);new Thread(new MessagePublisher(topic,url,user,password), "Name-Publisher").start();//new TopicPublisher().run(); }}