A: Create a new MAVEN project and join the following dependencies
<dependency><groupid>org.apache.activemq</groupid><artifactid>activemq-all</ Artifactid><version>5.11.1</version></dependency>
Two:
Server
Package Com.lala.mq;import Java.util.concurrent.timeunit;import Org.apache.activemq.broker.brokerservice;public Class Server {public static void main (string[] args) throws Exception{brokerservice broker = new Brokerservice (); broker.ad Dconnector ("tcp://127.0.0.1:55551"); Broker.setusejmx (false); Broker.start (); TimeUnit.SECONDS.sleep (9999);}}
Producer
Package Com.lala.mq;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.messageproducer;import Javax.jms.queue;import Javax.jms.session;import Javax.jms.TextMessage;import Org.apache.activemq.activemqconnectionfactory;public class Myproducer{public static void Main (string[] args) throws Exception{connectionfactory CF = new Activemqconnectionfactory ("tcp://127.0.0.1:55551"); Connection conn = Cf.createconnection (); Conn.start (); Session session = Conn.createsession (false, Session.auto_acknowledge); Queue queue = Session.createqueue ("CIB"); MessageProducer producer = Session.createproducer (queue); TextMessage TM = null;for (int i=1;i<=5;i++) {TM = Session.createtextmessage ("This is test message and MsgId =" + i);p ro Ducer.send (tm);} Session.close (); Conn.close ();}}
Consumer
Package Com.lala.mq;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.messageconsumer;import Javax.jms.queue;import Javax.jms.session;import Javax.jms.TextMessage;import Org.apache.activemq.activemqconnectionfactory;public class Myconsumer {public static void main (string[] args) throws Exception{connectionfactory CF = new Activemqconnectionfactory ("tcp://127.0.0.1:55551"); Connection conn = Cf.createconnection (); Conn.start (); Session session = Conn.createsession (false, Session.auto_acknowledge); Queue queue = Session.createqueue ("CIB"); Messageconsumer consumer = session.createconsumer (queue), Consumer.setmessagelistener ((message), {TextMessage TM = (TextMessage) message;try {System.out.println ("received message:" + tm.gettext ());} catch (Exception e) {e.printstacktrace ();}});}}
Three: Run
Run First: Server
Re-run: Consumer
Last run: Producer
You can see the output
At last
Note: The above practices do not require prior download and installation of the ACTIVEMQ service
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ActiveMQ Introductory Example