1 producers
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> <!-- Message queue --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> </dependency>
Step 2: configure Activemq to integrate spring. Configure connectionfactory applicationcontext-Activemq.xml
Step 3: configure producers.
Use the JMSTemplate object. Send a message.
Step 4: configure the Destination in the spring container.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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.36.40: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> <!--This is the theme destination, one to many --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="topic" /> </bean> </beans>
Step 5: code test
package com.shi.page; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; /** * * @author: SHF * @date: 2018 1:39:20 p.m., March 16, 2016 * @Description: spring Integrate activemq testing */ public class springActiveMqTest { @Test public void testpublichMQ()throws Exception{ //1 initialize spring container ApplicationContext application=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml"); //2 get jsmTemplate object from spring container JmsTemplate jmsTemplate=application.getBean(JmsTemplate.class); //3 get the distinction object from the spring container Destination distination=(Destination) application.getBean("queueDestination"); //4 sending messages using the jmsTempate object jmsTemplate.send(distination,new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { // Create a message object and return return session.createTextMessage("spring active queue22222"); } }); } }
2 consumers
2.2.2. Receiving message
Receive message in E3 search service.
Step 1: add the jar package related to Activemq to the project
Step 2: create an implementation class of MessageListener.
package com.shi.search.activemq; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; /** * * @author: SHF * @date: 2018 2:33:05 PM, March 16, 2016 * @Description: Message queuing listener, consumer. Used to receive messages */ public class MyMessageListener implements MessageListener { @Override public void onMessage(Message paramMessage) { // Processing received messages in listening TextMessage textMessage=(TextMessage) paramMessage; try { String text = textMessage.getText(); System.out.println(text); } catch (JMSException e) { e.printStackTrace(); } } }
Step 3: configure spring and Activemq integration.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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.36.40: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> <!--This is the theme destination, one to many --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="topic" /> </bean> <!-- The self configured listener realizes MessageListener Interface --> <bean id="myMessageListener" class="com.shi.search.activemq.MyMessageListener"></bean> <!-- Message listening container --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="myMessageListener" /> </bean> </beans>
Step 4: test the code.
package com.shi.solr; import java.io.IOException; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author: SHF * @date: 2018 March 16, 2016 2:45:16 PM * @Description: ActiveMQ Consumer testing */ public class ActiveMQCusomerTest { @Test public void customerTest() throws IOException{ //Just initialize the spring container, and the system will help us listen ApplicationContext application=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml"); //wait for System.in.read(); } }