ActiveMQ sends and receives messages

This article mainly uses a simple example to show ActiveMQ sending and receiving messages:

 

1. Description of ActiveMQ

2. Code example

 

1. Description of ActiveMQ

1. The latest version of ActiveMQ is: ActiveMQ 5.15.3 Release ====" Click: Official website to download ActiveMQ address

 

2. After downloading, unzip it, and then start ActiveMQ. When starting, it should be selected according to the number of digits of the system.



 

 

2. Code example

 

Import dependency jar package

<dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-all</artifactId>
   <version> 5.15 . 3 </version>
</dependency>

 

(1) Producer code

package com.chinasoft.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * Producers of ActiveMQ messages Producers send messages to message middleware, and message middleware that implements the JMS specification interface is called JMS Provider
 *
 * @author Freedom
 *
 */
public class Sender {

	public static void main(String[] args) throws JMSException {
		sender();
	}

	public static void sender() throws JMSException {

		// 1. Create a ConnectionFactory
		ConnectionFactory f = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
				ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");

		// 2. Establish a Connection connection through the ConnectionFactory factory, and call start to open the connection
		Connection c = f.createConnection();
		c.start();

		// 3. Create a session session through the Connection object to receive messages, parameter 1: whether to open the transaction, parameter 2: set the sign-in method
		Session session = c.createSession(false, Session.AUTO_ACKNOWLEDGE);//Auto sign

		// 4. Create a Destination object through session, which refers to a client used to specify the target of producing messages and the source object of consuming messages
		// Destination is called Queue in PTP mode; Destination is called Topic in pub/sub mode
		Destination d = session.createQueue("firstMQ");

		// 5. Create senders and receivers (producers and consumers) of messages through sessions
		MessageProducer p = session.createProducer(null);// Specify the Destination when the producer sends the message

		// 6.MessageProducer sets persistent and non-persistent features

		// 7. The data in the form of TextMessage in the JMS specification is created through the Session, and the message is sent with the MessageProducer, and the client receive method accepts the data
		// Be sure to close the Connection connection after use
		TextMessage msg = null;
		for (int i = 0; i < 100; i++) {
			msg = session.createTextMessage();
			msg.setText("ActiveMQ as message middleware: " + i);
			System.out.println("The message sent by the producer==" + msg.getText());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}

			// send messages
			p.send(d, msg);
		}

		// close the connection
		c.close();

	}

}

 

Notice:

①In order to ensure the reliability of the data in the message middleware, the data is saved to kahadb by default

 

 

②The change of producer data can be seen through the console

ActiveMQ built-in jetty container, access the console (http://localhost:8161/admin)



 

 

(2) Consumer code example

package com.chinasoft.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 Consumer {

	public static void main(String[] args) throws JMSException {
		receiver();
	}

	public static void receiver() throws JMSException {

		// 1. Create a ConnectionFactory
		ConnectionFactory f = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
				ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");

		// 2. Establish a Connection connection through the ConnectionFactory factory, and call start to open the connection
		Connection c = f.createConnection();
		c.start();

		// 3. Create a session session through the Connection object to receive messages, parameter 1: whether to open the transaction, parameter 2: set the sign-in method
		Session session = c.createSession(false, Session.AUTO_ACKNOWLEDGE);//Auto sign

		// 4. Create a Destination object through session, which refers to a client used to specify the target of producing messages and the source object of consuming messages
		// Destination is called Queue in PTP mode; Destination is called Topic in pub/sub mode
		Destination d = session.createQueue("firstMQ");

		// 5. Create senders and receivers (producers and consumers) of messages through sessions
		MessageConsumer consumer = session.createConsumer(d); // Consumers are used to accept MQ data

		// 7. The data in the form of TextMessage in the JMS specification is created through the Session, and the message is sent with the MessageProducer, and the client receive method accepts the data
		TextMessage msg = null;
		while (true) {
			// Block waiting to receive data from MQ
			msg = (TextMessage) consumer.receive();
			System.out.println("Consumer accepts message ===" + msg.getText());
		}
	}

}

 

The console shows the result:



 

The console shows the result:



 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326192778&siteId=291194637