本文将阐述如何把activemq与spring集成。
1、前提约束
可以使用java完成active的topic模式 https://www.jianshu.com/p/d3993d12f3f7
2、修改pom.xml
在pom.xml中加入以下依赖:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
3、创建一个消费者的消息监听
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class MessageAddListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("接收到消息:"+textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4、创建一个消费者的配置文件applicationContext-activemq-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- JMS服务厂商提供的ConnectionFactory -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="tcp://192.168.100.192:61616"/>
</bean>
<!--<bean id="itemAddtopic" class="org.apache.activemq.command.ActiveMQTopic">-->
<!--<constructor-arg name="name" value="item-add-topic"></constructor-arg>-->
<!--</bean>-->
<!-- 配置消息的Destination对象 -->
<bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg name="name" value="testQueue"></constructor-arg>
</bean>
<!-- 配置消息的接收者 -->
<bean id="messageAddListener" class="MessageAddListener"/>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="testQueue"/>
<property name="messageListener" ref="messageAddListener"/>
</bean>
</beans>
5、创建一个消费者测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMqQueueConsumer {
public static void main(String[] args) throws Exception{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-activemq-consumer.xml");
System.in.read();
}
}
启动测试类。
6、创建一个生产者的配置文件applicationContext-activemq-producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- JMS服务厂商提供的ConnectionFactory -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="tcp://192.168.100.192:61616"/>
</bean>
<!--<bean id="itemAddtopic" class="org.apache.activemq.command.ActiveMQTopic">-->
<!--<constructor-arg name="name" value="itemaddtopic"></constructor-arg>-->
<!--</bean>-->
<!-- 配置消息的Destination对象 -->
<bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg name="name" value="testQueue"></constructor-arg>
</bean>
<!-- 配置JMSTemplate -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="testQueue"></property>
</bean>
</beans>
7、创建一个生产者测试类,即发送消息
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
public class SpringMqQueueProducer {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-activemq-producer.xml");
JmsTemplate jmsTemplate = (JmsTemplate) applicationContext.getBean("jmsTemplate");
Destination destination = (Destination) applicationContext.getBean("testQueue");
jmsTemplate.send(destination,new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message message = session.createTextMessage("zhangli");
return message;
}
});
}
}
启动生产者,这时候去查看消费者的命令行,会看到打印出来的消息,即生产者发送了一条消息,消费者收到了这条消息,注意,我们这里是使用activemq的queue模式测试,当然也可以使用activemq的topic模式。