spring 项目集成ActiveMQ
1. 修改pom文件引入依赖
<!-- active mq -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
注意: 这边引入的依赖如果版本过高,在农污项目中会出现冲突或者其他问题
2. 新增activeMQ配置文件spring-activemq.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
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-4.3.xsd">
<!-- 开启包扫描 (减少在xml中注册bean)-->
<context:component-scan base-package="com.zjky.sewerage.activemq" />
<!-- #### ActiveMq配置 start ####-->
<!-- 1. ActiveMq连接工厂 -->
<bean id="amqConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<!-- 注意端口号默认为61616 -->
<value>tcp://192.168.2.113:61616</value>
</property>
<property name="userName">
<value>admin</value>
</property>
<property name="password">
<value>admin</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<!-- 2. Spring Caching 连接工厂(类似数据库线程池的东西,减少连接的创建。) -->
<!-- 由于jmsTemplate每次发送消息都需要创建连接和创建session了,所以引入这个类似连接池的连接工厂,优化Mq的性能 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 3. 配置jmsTemplate,用于发送发送mq消息 -->
<!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
<bean id="jmsTemplateQueue" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="receiveTimeout" value="10000" />
<!-- true是topic,false是queue,默认是false,此处显示写出false -->
<property name="pubSubDomain" value="false" />
</bean>
<!-- 配置JMS模板(Topic) -->
<bean id="jmsTemplateTopic" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="receiveTimeout" value="10000" />
<!-- true是topic,false是queue,默认是false,此处显示写出false -->
<property name="pubSubDomain" value="true" />
</bean>
<!-- 4.1定义Queue监听器 -->
<!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->
<jms:listener-container destination-type="queue" connection-factory="connectionFactory">
<!-- TODO 每添加一个queue的监听,都需要在这里添加一个配置 -->
<!-- 这样配置就可以方便的对多个队列监听 , 每增加一个队列只需要添加一个 jms:listener -->
<!-- destination:队列名称, ref:指向对应的监听器对象 -->
<!-- 示例: <jms:listener destination="queueName" ref="consumerBean" /> -->
<jms:listener destination="queueTest" ref="queueMessageListener" />
</jms:listener-container>
<!-- 显示注入消息监听容器(Topic),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->
<jms:listener-container destination-type="topic" connection-factory="connectionFactory">
<jms:listener destination="topicTest" ref="topicMessageListener" />
</jms:listener-container>
<!-- #### ActiveMq配置 end ####-->
</beans>
import到applicationContent.xml文件中去
<import resource="classpath:/spring-activemq.xml" />
配置文件中需要修改的地方
- 指定bean扫描监听器listener的包,为实际项目中的包。
- ActiveMQ连接工厂brokerURL的地址以及用户名密码。
- JMS模板中pubSubDomain属性的值,false指队列 true指订阅。
- JMS监听容器中的监听对象,destination来指定队列或订阅的名称,ref来指定相应的bean,例如:这个queueTest为队列的名称,queueMessageListener为所在监听器的类所创建的bean,和监听器类名同名。
注意:随着项目的开发,如果后面需要增加队列或者订阅,需要在jsm:listener监听容器中增加监听对象。
可用的配置文件头部
<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:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
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/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
参考https://www.jianshu.com/p/fca631501e5b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
3. 创建productor
@Resource(name="jmsTemplateQueue")
private JmsTemplate jmsTemplateQueue;
@Resource(name="jmsTemplateTopic")
private JmsTemplate jmsTemplateTopic;
@Override
public void sendQueueMsg(String destinationString, String msg) throws Exception {
System.out.println(Thread.currentThread().getName()+" SEND MESSAGE TO QUEUE "+destinationString+" : "+msg);
jmsTemplateQueue.send(new ActiveMQQueue(destinationString), new MessageCreator() {
@Override
public Message createMessage(Session arg0) throws JMSException {
return arg0.createTextMessage(msg);
}
});
}
@Override
public void sendTopicMsg(String destinationString, String msg) throws Exception {
System.out.println(Thread.currentThread().getName()+" SEND MESSAGE TO TOPIC "+destinationString+" : "+msg);
jmsTemplateTopic.send(new ActiveMQTopic(destinationString), new MessageCreator() {
@Override
public Message createMessage(Session arg0) throws JMSException {
return arg0.createTextMessage(msg);
}
});
}
注意:上面红色部分代码很重要,需要通过Resource注解,指定配置文件中的JMS模板
- 创建监听器
@Component
public class TopicMessageListener implements MessageListener{
@Override
public void onMessage(Message message) {
TextMessage ms = (TextMessage)message;
try {
System.out.println(this.getClass().getName() + " RECEIVE TOPIC MESSAGE : "+ms.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
注意:监听器的类名,需要和xml配置文件中指定的监听对象一致。通过ref指定TopicMessageListener自动创建的bean。创建的bean和TopicMessageListener差别就是首字母大小写。
5. 总结
通过xml配置文件中的配置,监听器中
指定队列或订阅的名称,指向相应的监听器类,实现多个不同队列或多个不同订阅的监听。
我们只需要监听队列或者订阅的名称,再在相应的监听器中实现相应的逻辑即可。
参考:
https://www.cnblogs.com/zeng1994/p/5f9cd67f9ca155e8329263a2e5467aa4.html
https://www.cnblogs.com/liulihaha/p/10573076.html