Spring integration of Activemq entry actual combat detailed introduction two

This article is based on the integration of the jar package, not the maven method. If you need to understand the maven method, please check the blogger’s Activemq entry actual combat detailed introduction. It is written in the maven method.

MQ
first briefly introduce MQ, the English name of MQ is MessageQueue, and the Chinese name is the message queue that everyone uses. Why do you use it? To put it bluntly, it is a message receiving and forwarding container that can be used for message push.

ActiveMQ is produced by Apache, a most popular and powerful open source message bus. ActiveMQ is a JMS Provider implementation that fully supports JMS1.1 and J2EE 1.4 specifications. It is very fast, supports clients and protocols in multiple languages, and can be easily embedded in the enterprise application environment, and has many advanced functions.

Download ActiveMQ, here is a case based on windows version (actual production or

Official website: http://activemq.apache.org/ 

Download and unzip to get as shown below:

The above directory structure description: 

Bin stores script files,
conf stores basic configuration files,
data stores log files,
docs stores description documents,
examples stores simple examples,
lib stores jar packages required by activemq, and
webapps is used to store the directory of the project.
Start ActiveMQ 
We understand the basic directory of activemq, let's run the activemq service, enter the activemq.bat script file in the bin directory or run the activemq.bat under our own computer version, you can see the effect of the following figure. 

è¿éåå¾çæè¿ °

 When monitoring 
ActiveMQ is started by default, the built-in jetty server is started and an admin application for monitoring ActiveMQ is provided. 
Enter in the browser: http://127.0.0.1:8161/admin/

Username and password are both admin

To stop the server, just press Ctrl+Shift+C, and then enter y.

=====================================================================

First of all, here is the blogger’s project structure diagram as follows: This should be clearer, if you still don’t understand, the blogger can upload the code, but think the code is too simple, you can follow the blogger’s code to run through , Come on! The two projects are actually similar to the actual enterprise development process, one project calls the data of the other project. If you still don’t understand, please leave a message to the blogger. If you are a great god, you can ignore this project. After all, the current technical level of the blogger is not good, thank you.
————————————————

Two project structures: activemq-producer sends items in the message queue, and activemq-consumer receives items in the consumption queue.

Specific steps: 1: First create two projects, activemq-producer and activemq-consumer.

2. Then import the jar package required by the dependency:

3: Configure the configuration of the producer: applicationContext-activemq.xml

<?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">
	<!-- ActiveMQConnectionFactory 工厂连接 -->
	<bean id="targetConnection" class="org.apache.activemq.ActiveMQConnectionFactory">
		<!--activemq的链接地址  -->
		<property name="brokerURL" value="tcp://127.0.0.1:61616"></property>
	</bean>
	<!-- 通用的connectionfacotry 指定真正使用的连接工厂 ,如其他RibbitMQ,kafka,RiketMQ等,到时候只需要修改class指定的工厂-->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="targetConnection"></property>
	</bean>
	<!-- 接收和发送消息时使用的类 模板对象-->
	<bean class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"></property>
	</bean>
	<!--点对点模式,只能是一个生产者产生一个消息,被一个消费者消费。  -->
	 <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
	 		<!-- 队列消息名称 -->
		<constructor-arg name="name" value="demo-change-queue"></constructor-arg>
	</bean> 
	
	<!-- 发布订阅模式,一个生产者可以一个消息,可以被多个消费者消费。 -->
	 <!-- <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
		订阅消息名称
		<constructor-arg name="name" value="demo-change-topic"></constructor-arg>
	</bean>  -->
	
</beans>

4: Configure the configuration of the consumer: applicationContext-activemq.xml

<?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">
	<!-- ActiveMQConnectionFactory 工厂连接 -->
	<bean id="targetConnection" class="org.apache.activemq.ActiveMQConnectionFactory">
		<!--activemq的链接地址  -->
		<property name="brokerURL" value="tcp://127.0.0.1:61616"></property>
	</bean>
	<!-- 通用的connectionfacotry 指定真正使用的连接工厂 ,如其他RibbitMQ,kafka,RiketMQ等,到时候只需要修改class指定的工厂-->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="targetConnection"></property>
	</bean>
	<!-- 接收和发送消息时使用的类 模板对象-->
	<bean class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"></property>
	</bean>
	<!--点对点模式,只能是一个生产者产生一个消息,被一个消费者消费。  -->
	<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg name="name" value="item-change-queue"></constructor-arg>
	</bean> 
	<!-- 发布订阅模式,一个生产者可以一个消息,可以被多个消费者消费。 -->
	<!-- <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
		订阅名称
		<constructor-arg name="name" value="demo-change-topic"></constructor-arg>
	</bean> -->
	
	<!-- 监听器 -->
	 <bean id="myMessageListener" class="com.learn.activemq.MyMessageListener"></bean>
	<!-- 监听容器,作用:启动线程做监听 -->
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<!--  这里ref需要对应相应的模式进行修改-->
		<property name="destination" ref="queueDestination"></property>
		<property name="messageListener" ref="myMessageListener"></property>
	</bean>
	
	<!-- <bean id="myMessageListener2" class="com.learn.activemq.MyMessageListener"></bean>
	监听容器,作用:启动线程做监听
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="destination" ref="topicDestination"></property>
		<property name="messageListener" ref="myMessageListener2"></property>
	</bean>
	<bean id="myMessageListener3" class="com.learn.activemq.MyMessageListener"></bean>
	监听容器,作用:启动线程做监听
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="destination" ref="topicDestination"></property>
		<property name="messageListener" ref="myMessageListener3"></property>
	</bean> -->
</beans>

5: Write the test of the producer: Producer

package com.learn.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class Producer {
	@Test
	public void send() throws Exception{
		//1.初始化spring容器
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-activemq.xml");
		//2.获取到jmstemplate的对象
		JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
		//3.获取destination
		Destination destination = (Destination) context.getBean(Destination.class);
		//4.发送消息
		jmsTemplate.send(destination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage("我是activemq发送者,生产者,请问消费者在吗?");
			}
		});
		Thread.sleep(1000);//休息1000毫秒  =1秒
		
	}
}

 

6: Write the consumer's listener:

package com.learn.activemq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.xml.soap.Text;

public class MyMessageListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		//获取消息
		if(message instanceof TextMessage){
			TextMessage textMessage = (TextMessage)message;
			String text;
			try {
				text = textMessage.getText();
				System.out.println(text);
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}

}

7: Write consumer test: (the blogger here has no problems with the queue message mode test, but the subscription message mode test was not successful and needs to be modified)

package com.learn.activemq;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ActivemqConsumerTest {
	@Test
	public void testQueueConsumer() throws Exception {
		//初始化spring容器
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-activemq.xml");
		//等待
		System.in.read();
	}

}

Startup method: Start the Producer first, and then run ActivemqConsumerTest. Run to see the result:

Finally, the blogger uploads the code to CSDN. If you don’t understand, you can refer to the downloaded code to learn Demo.

Guess you like

Origin blog.csdn.net/qq_30764991/article/details/100930028