Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java middleware ActiveMQ

Shulou Source: shulou.com Published: 2022-06-03 09:20:50 09月11日 Update

This article will explain in detail how to use the Java middleware ActiveMQ. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

What is the middle message room?

The middleware technology of message transmission mechanism / message queue is adopted to exchange data and be used in the integration of distributed systems. Jms is defined in Java, which is a unified interface for Java messages. What is ActiveMq? This is an implementation of this interface, which is equivalent to the database connection driver. Different vendors have their own different implementations. Let's see how to implement it in code as soon as possible.

There are two forms of receiving and sending messages: point-to-point and publish subscription mode, that is, "one-to-one" and "one-to-many".

1. Guide package (maven):

Org.apache.activemq activemq-client 5.13.4

two。 To start writing classes, the provider (sender) and the consumer (receiver) are two different projects. Let's first create an ordinary maven project instead of a peer-to-peer web project (messages can only be consumed once. If there are multiple consumers at the same time, the one who grabs it first belongs to the one.)

Message provider

Public static void main (String [] args) throws JMSException {/ / create a connection factory. This parameter is the address of your own activeMQ ConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://192.168.25.180:61616"); / / 2. Create a connection Connection connection = connectionFactory.createConnection (); / / 3. Start the connection connection.start (); / / 4. Get session (session object) / * whether arg0 enables automatic confirmation of transaction arg1 messages * / Session session = connection.createSession (false,Session.AUTO_ACKNOWLEDGE); / / 5. Create a queue object with the name Queue firstQueue = session.createQueue ("firstQueue"); / / 6. Create a message producer object / / Destination destination =; / / target object MessageProducer producer = session.createProducer (firstQueue); / / 7. Create a message TextMessage textMessage = session.createTextMessage ("Welcome to the strange Tianyu software"); / / 8. Send message producer.send (textMessage); / / 9. Close the resource producer.close (); session.close (); connection.close ();}

Message consumer

The first few steps are the same, all are to create a connection, only step 6 is different, creating a consumer

Public static void main (String [args) throws JMSException, IOException {ConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://192.168.25.180:61616"); / / 2. Create a connection Connection connection = connectionFactory.createConnection (); / / 3. Start the connection connection.start (); / / 4. Get session (session object) / * whether arg0 enables automatic confirmation of transaction arg1 messages * / Session session = connection.createSession (false,Session.AUTO_ACKNOWLEDGE); / / 5. Create a queue object with the name Queue firstQueue = session.createQueue ("firstQueue"); / / 6. Create a message consumer object MessageConsumer consumer = session.createConsumer (firstQueue); / / 7. Set listening consumer.setMessageListener (new MessageListener () {@ Override public void onMessage (Message message) {TextMessage textMessage = (TextMessage) message; try {System.out.println ("extracted message is" + textMessage.getText ());} catch (JMSException e) {e.printStackTrace ();}); / / 8. Waiting for keyboard input / / the purpose is to make the program stop to see the effect System.in.read (); / / 9. Close the resource consumer.close (); session.close (); connection.close ();}

Publish and subscribe mode (after the message is published, only the previously running consumer can receive it, and after the message is consumed by any consumer, the later started consumer cannot consume the previous message)

Message provider

/ / create a connection factory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://192.168.25.180:61616"); / / 2. Create a connection Connection connection = connectionFactory.createConnection (); / / 3. Start the connection connection.start (); / / 4. Get session (session object) / * whether arg0 enables automatic confirmation of transaction arg1 messages * / Session session = connection.createSession (false,Session.AUTO_ACKNOWLEDGE); / / 5 Topic topic = session.createTopic ("first-topic"); / / 6. Create a message producer object / / Destination destination =; / / target object MessageProducer producer = session.createProducer (topic); / / 7. Create a message TextMessage textMessage = session.createTextMessage ("Welcome to the strange Tianyu software"); / / 8. Send message producer.send (textMessage); / / 9. Close the resource producer.close (); session.close (); connection.close ()

Consumer

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://192.168.25.180:61616") / / 2. Create a connection Connection connection = connectionFactory.createConnection (); / / 3. Start the connection connection.start (); / / 4. Get session (session object) / * whether arg0 enables automatic confirmation of transaction arg1 messages * / Session session = connection.createSession (false,Session.AUTO_ACKNOWLEDGE); / / 5 Topic topic = session.createTopic ("first-topic"); / / 6. Create a message consumer object MessageConsumer consumer = session.createConsumer (topic); / / 7. Set listening consumer.setMessageListener (new MessageListener () {@ Override public void onMessage (Message message) {TextMessage textMessage = (TextMessage) message; try {System.out.println ("extracted message is" + textMessage.getText ());} catch (JMSException e) {e.printStackTrace ();}); / / 8. Waiting for keyboard input / / the purpose is to make the program stop to see the effect System.in.read (); / / 9. Close the resource consumer.close (); session.close (); connection.close ()

This is the end of the article on "how to use ActiveMQ in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Message consumption object consumer way transaction resource different provider article queue project only name that is factory interface effect data more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux macOS MySQL vpn