Installation and use of ActiveMQ

Keywords: Spring Session Apache Linux

ActiveMQ installation

Go to http://activemq.apache.org/ official website to download ActiveMQ
Installation environment:
1. jdk required
2. Install the Linux system. The production environment is all Linux systems.
Installation steps:
Step 1: upload the ActiveMQ compressed package to the Linux system.
Step 2: decompress.
Step 3: start.
Use the activemq command in bin directory to start:
[root@localhost bin]# ./activemq start
Close:
[root@localhost bin]# ./activemq stop
View status:
[root@localhost bin]# ./activemq status
Note: do not use the activemq-all-5.12.0.jar package when ActiveMQ integrates spring. 5.11.2 is recommended
Enter the management background of ActiveMQ pair:
http://192.168.25.168:8161/admin
User name: admin
Password: admin

How to use ActiveMQ

Provider:

jar package 5.11.2

<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-all</artifactId>
</dependency>
//1. To create a connection factory object, you need to specify the ip and port of the service.
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://IP address: 61616 ");
		//2. Create a Connection object using the factory object.
		Connection connection = connectionFactory.createConnection();
		//3. Open the Connection and call the start method of the Connection object.
		connection.start();
		//4. Create a Session object.
		//First parameter: whether to start transaction. If true opens the transaction, the second parameter has no meaning. Generally, transaction false is not enabled.
		//The second parameter: answer mode. Automatic response or manual response. General automatic response.
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//5. Use the Session object to create a Destination object. There are two forms: queue and topic. Now you should use queue
		Queue queue = session.createQueue("test-queue");
		//6. Use the Session object to create a Producer object.
		MessageProducer producer = session.createProducer(queue);
		//7. To create a Message object, you can use TextMessage.
		TextMessage textMessage = new ActiveMQTextMessage();
		textMessage.setText("hello Activemq");
		TextMessage textMessage = session.createTextMessage("hello activemq");
		//8. Send message
		producer.send(textMessage);
		//9. Close resources
		producer.close();
		session.close();
		connection.close();


Consumer: receive message

//Create a ConnectionFactory object to connect to the MQ server
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://IP address: 61616 ");
		//Create a connection object
		Connection connection = connectionFactory.createConnection();
		//Open connection
		connection.start();
		//Create a Session object using the Connection object
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//Create a Destination object. queue object
		Queue queue = session.createQueue("test-queue");
		//Use the Session object to create a consumer object.
		MessageConsumer consumer = session.createConsumer(queue);
		//receive messages
		consumer.setMessageListener(new MessageListener() {
			
			@Override
			public void onMessage(Message message) {
				//Print results
				TextMessage textMessage = (TextMessage) message;
				String text;
				try {
					text = textMessage.getText();
					System.out.println(text);
				} catch (JMSException e) {
					e.printStackTrace();
				}
				
			}
		});
		//Waiting to receive message
		System.in.read();
		//close resource
		consumer.close();
		session.close();
		connection.close();

 

Integrating the use of spring

Step 1: reference the relevant jar package

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency

Step 2: configure spring to integrate activeMQ

<!-- It can really produce Connection Of ConnectionFactory,Corresponding JMS Provided by service provider -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://IP address: 61616 "/ >
</bean>
<!-- Spring For managing real ConnectionFactory Of ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- target ConnectionFactory Corresponding to the real can produce JMS Connection Of ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>

Step 3: configure message producers

<! -- configure producers -- >
	<! -- the JMS tool class provided by spring, which can send and receive messages -- >
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<! -- this connectionfactory corresponds to the connectionfactory object provided by Spring that we define -- >
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>

Step 4: configure Destination -- message sending form

<! -- this is the queue destination, point-to-point -- >
	<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>spring-queue</value>
		</constructor-arg>
	</bean>

 

send message

// Step 1: initialize a spring container
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("Path of configuration file");
// Step 2: get the JMSTemplate object from the container.
JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
// Step 3: get a Destination object from the container
Queue queue = (Queue) applicationContext.getBean("queueDestination");
// Step 4: Send a message using the JMSTemplate object. You need to know the Destination
jmsTemplate.send(queue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("spring activemq test");
return textMessage;
}
});

Acceptance message

Step 1: create an implementation class of MessageListener

public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
//Get message content
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {e.printStackTrace();
}
}
}

Step 2: integrate spring

<!-- Listen to the product adding message and synchronize the index library -->
	<bean id="Custom listener name" class="Route"/>

	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="queueDestination" />
		<property name="messageListener" ref="Custom listener name" />
	</bean>

Complete spring configuration information

Sender

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- It can really produce Connection Of ConnectionFactory,Corresponding JMS Provided by service provider -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.25.128:61616" />
	</bean>
	<!-- Spring For managing real ConnectionFactory Of ConnectionFactory -->
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- target ConnectionFactory Corresponding to the real can produce JMS Connection Of ConnectionFactory -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>
	<!-- Configure producers -->
	<!-- Spring Provided JMS Tool class, which can send and receive messages -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<!-- this connectionFactory That's what we define Spring The one provided ConnectionFactory object -->
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>
	<!--This is the queue destination, point-to-point -->
	<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>spring-queue</value>
		</constructor-arg>
	</bean>

	
</beans>

Receiver

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- It can really produce Connection Of ConnectionFactory,Corresponding JMS Provided by service provider -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.25.128:61616" />
	</bean>
	<!-- Spring For managing real ConnectionFactory Of ConnectionFactory -->
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- target ConnectionFactory Corresponding to the real can produce JMS Connection Of ConnectionFactory -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>
		<!--This is the queue destination, point-to-point -->
	<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>spring-queue</value>
		</constructor-arg>
	</bean>

	<!-- Listen to the product adding message and synchronize the index library -->
	<bean id="htmlGenListener" class="cn.e3mall.item.listener.HtmlGenListener"/>
	
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="queueDestination" />
		<property name="messageListener" ref="htmlGenListener" />
	</bean>
</beans>

Posted by TechXpert on Fri, 03 Apr 2020 11:28:46 -0700