1. Download ACTIVEMQ:
Http://activemq.apache.org/download-archives.html
2. Run ACTIVEMQ unzip Apache-activemq-5.5.1-bin.zip, and then double-click Apache-activemq-5.5.1\bin\win64\activemq.bat to run the ACTIVEMQ program.
Note: If you encounter a situation that cannot be started, try changing the computer name to full English and not other characters!
After the launch can be browser open a Web site login, so that later to see the program after the operation of the information! Including message queue, message dequeue, consumer and so on information!
http://localhost:8161/admin/(default username and password is: admin admin) 3. Write producer and consumer.
Once the ACTIVEMQ is successfully deployed, you can develop producer and consumer.
Create a new Java project in Eclipse and bring the jar packages under the ACTIVEMQ extract directory into the project (Apache-activemq-5.13.0\activemq-all-5.13.0.jar), Then you can write producer and consumer.
The producer code is as follows:
Package Isa.qa.activemqtest;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.deliverymode;import Javax.jms.destination;import Javax.jms.messageproducer;import javax.jms.Session; Import Javax.jms.textmessage;import Org.apache.activemq.activemqconnection;import Org.apache.activemq.activemqconnectionfactory;public class Sender {public static void main (string[] args) {//c Onnectionfactory: The connection factory, JMS uses it to create the connection ConnectionFactory connectionfactory; CONNECTION:JMS client-to-JMS Provider connection Connection Connection = null; Session: A thread that sends or receives a message session session; Destination: The destination of the message, to whom the message is sent. Destination Destination; MessageProducer: Message sender MessageProducer producer; TextMessage message; Constructs the ConnectionFactory instance object, where Activemq is implemented with the jar connectionfactory = new Activemqconnectionfactory (Act Ivemqconnection.default_user, Activemqconnection.default_passwoRD, "tcp://localhost:61616"); try {//Construct get Connection object from factory connection = Connectionfactory.createconnection (); Start Connection.start (); Get operation Connection session = Connection.createsession (Boolean.true, Session.auto_acknowledge); Get session Note The parameter value is a server queue/topic//destination = Session.createqueue ("Firstqueue"); Destination = Session.createtopic ("Firsttopic"); Get the message Generator "sender" producer = Session.createproducer (destination); Persistent settings, which are studied here, are actually based on the project decision Producer.setdeliverymode (Deliverymode.persistent); Constructs the message, writes here to die, the project is the parameter, or the method obtains SendMessage (session, producer); Session.commit (); } catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = connection) {connection.close (); }} catch (Throwable ignore) {}}} public static void SendMessage (Session session, MessageProducer producer) throws exception{TextMessage message = session.createtextmessage ("Messages sent by Activemq"); Send message to Destination place System.out.println ("ACTIVEMQ send Message"); Producer.send (message); }}
The
Consumer code is as follows:
Package Isa.qa.activemqtest;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.destination;import Javax.jms.messageconsumer;import Javax.jms.session;import javax.jms.TextMessage; Import Org.apache.activemq.activemqconnection;import Org.apache.activemq.activemqconnectionfactory;public class Receiver {public static void main (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connection Connectionfacto Ry ConnectionFactory; CONNECTION:JMS client-to-JMS Provider connection Connection Connection = null; Session: A thread that sends or receives a message session session; Destination: The destination of the message, to whom the message is sent. Destination Destination; Consumer, message recipient Messageconsumer consumer; ConnectionFactory = new Activemqconnectionfactory (Activemqconnection.default_user, ActiveMQ Connection.default_password, "tcp://localhost:61616"); try {//Construct get Connection object from factory connection = ConnectionfaCtory.createconnection (); Start Connection.start (); Get operation Connection session = Connection.createsession (Boolean.false, Session.auto_acknowledge); Get session Note The parameter value is a server queue/topic//destination = Session.createqueue ("Firstqueue"); Destination = Session.createtopic ("Firsttopic"); Consumer = session.createconsumer (destination); while (true) {//Set the receiver to receive message timeout TextMessage message = (textmessage) consumer.receive (500000) ; if (null! = message) {SYSTEM.OUT.PRINTLN ("Receive Message" + Message.gettext ()); } else {break; }}} catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = connection) connection.close (); } catch (Throwable ignore) {}}}}
4. Note:
The point-to-point between producer and consumer is simply set:
Destination = Session.createqueue ("Firstqueue");
A one-to-many setup between producers and consumers:
Destination = Session.createtopic ("Firsttopic");
Message middleware-activemq Getting started instance