Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Easy getting started with ActiveMQ

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report