Easy getting started with ActiveMQ
Official download address of ActiveMQ: http://activemq.apache.org/download.html
After downloading and decompressing, execute activemq.bat under the bin directory to start the mq service
Locate the lib directory and introduce the relevant jar package
Sender message sender class
Package com.ycj.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.DeliveryMode;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageProducer;import javax.jms.Session;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {private static final String MQ_USER = "admin"; private static final String MQ_PWD = "admin"; private static final String MQ_URL = "tcp://localhost:61616" Private static final String MQ_QUEUE_DEFAULT = "testQueue"; public static void main (String [] args) throws JMSException {/ / create JMS connection factory ConnectionFactory factory = new ActiveMQConnectionFactory (MQ_USER, MQ_PWD, MQ_URL); / / get the link through the factory and start Connection connection = factory.createConnection () Connection.start (); / / create session Session session = connection.createSession (Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); / / create destination message destination Destination destination = session.createQueue (MQ_QUEUE_DEFAULT) / / initialize a producer MessageProducer producer = session.createProducer (destination) according to destination; / / producer messages do not persist producer.setDeliveryMode (DeliveryMode.NON_PERSISTENT); / / generate messages and send Message msg1 = session.createTextMessage ("Ihumm msg1") Message msg2 = session.createTextMessage ("Ihumm msg2"); producer.send (msg1); producer.send (msg2); session.commit (); connection.close (); System.out.println ("producer send msg done!");}}
Receiver message receiver class
Package com.ycj.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver {private static final String MQ_USER = "admin"; private static final String MQ_PWD = "admin"; private static final String MQ_URL = "tcp://localhost:61616" Private static final String MQ_QUEUE_DEFAULT = "testQueue"; public static void main (String [] args) throws JMSException {/ / create JMS connection factory ConnectionFactory factory = new ActiveMQConnectionFactory (MQ_USER, MQ_PWD, MQ_URL); / / get the link through the factory and start Connection connection = factory.createConnection () Connection.start (); / / create session Session session = connection.createSession (Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); / / create destination message destination Destination destination = session.createQueue (MQ_QUEUE_DEFAULT) / / initialize a consumer MessageConsumer consumer = session.createConsumer (destination) according to destination; while (true) {TextMessage msg = (TextMessage) consumer.receive (1000mm 1000); if (null! = msg) {System.out.println ("message has been received:" + msg.getText ()) }}
Run Receiver and Sender in turn, and output from the console:
Sender
Receiver
Open a browser and enter http://localhost:8161/admin/index.jsp, and select Queues to monitor queue consumption.