spring 管理ActiveMQ
- Maven修改,需要添加Spring 支持Jms的pom 包
- Spring配置文件
- 队列
- 主题
- 在Spring里面实现消费不启动,直接通过配置监听完成
依赖引入
<!--spring整合mq-->
<!--activemq对JMS的支持,整合SPringle和Activemq-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.23.RELEASE</version>
</dependency>
<!--activemq所需要的pool包配置-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.9</version>
</dependency>
<!-- activemq 所需要的jar 包-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
<!-- activemq 和 spring 整合的基础包 -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
spring配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启包的自动扫描-->
<context:component-scan base-package="com.example"/>
<!--配置生产者-->
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<!--真正可以产生Connection的ConnectionFactory,由对应的JMS服务厂商提供-->
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://8.222.137.167:61616"/>
</bean>
</property>
<!--最大连接数-->
<property name="maxConnections" value="100"></property>
</bean>
<!--这个是队列目的地,点对点的-->
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<!--队列的名字spring-active-queue-->
<constructor-arg index="0" value="spring-active-queue"/>
</bean>
<!--这个是主题-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-active-Topic"/>
</bean>
<!--Spring提供的JMS工具类,它可以进行消息发送、接收等-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"/>
<!--主题-->
<property name="defaultDestination" ref="destinationQueue"/>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean>
</beans>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"/>
<!--主题-->
<property name="defaultDestination" ref="destinationQueue"/> //引用点对点队列
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean>
编写队列消费者和生产者代码
- 生产者代码
package com.example.activemq.queue.spring;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.TextMessage;
@Service
public class SpringMQ_Produce {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringMQ_Produce produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");
produce.jmsTemplate.send(session -> {
TextMessage textMessage = session.createTextMessage("*******spring和ActiveMQ的整合case for MessageListener....");
return textMessage;
});
System.out.println("********send task over");
}
}
- 消费者代码
package com.example.activemq.queue.spring;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
@Service
public class SpringMQ_Consumer {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");
String retValue = (String) consumer.jmsTemplate.receiveAndConvert();
System.out.println("*********消费者收到的消息:"+ retValue);
}
}
只有配置文件需要改变,改变目的地,代码不需要改变
配置消费者的监听类
- 需要写一个类来实现消息监听
消费者配置了自动监听相当于在spring里面后台进行运行,有消息的时候运行我们实现监听类里面的
方法
只需要在配置文件中加入一个监听类的配置即可。
如下:
<!--配置监听程序-->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="destinationTopic"/>
<!--public class MyMessageListener implements MessageListener-->
<property name="messageListener" ref="myMessageListener"/>
</bean>