Spring整合ActiveMQ
1、添加Spring支持JMS的包(添加依赖)
<dependencies> <!-- activemq 所需要的jar 包--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.0</version> </dependency> <!-- 嵌入式activemq的broker所需要的依赖包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.1</version> </dependency> <!-- activemq连接池 --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.10</version> </dependency> <!-- spring支持jms的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>5.2.1.RELEASE</version> </dependency> <!--spring相关依赖包--> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>4.15</version> </dependency> <!-- Spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.23.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.23.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.23.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.23.RELEASE</version> </dependency> </dependencies>
2、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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <!-- 自动扫描 将带有@Conponent 等注解的类的实例,注入到ioc容器中--> <context:component-scan base-package="com.atguigu.activemq"/> <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.2.180:61616"></property> </bean> </property> <property name="maxConnections" value="100"></property> </bean> <!-- 队列目的地 --> <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="spring-active-queue"></constructor-arg> </bean> <!--主题--> <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg index="0" value="spring-active-topic"></constructor-arg> </bean> <!-- jms 的工具类 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsFactory"/> <property name="defaultDestination" ref="destinationTopic"/> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean> <!-- <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory"/> <property name="destination" ref="destinationTopic"/> <property name="messageListener" ref="myMessageListener"/> </bean>--> </beans>
3、队列
//生产者 @Service public class SpringMQ_Produce { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); SpringMQ_Produce produce = (SpringMQ_Produce)ac.getBean("springMQ_Produce"); produce.jmsTemplate.send((sesson)->{ return sesson.createTextMessage("spring和ActiveMQ的整合 ---> for topic "); }); System.out.println("消息发送到MQ成功!!"); } } // 消费者 @Service public class SpringMQ_Consumer { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); SpringMQ_Consumer consumer = (SpringMQ_Consumer)ac.getBean("springMQ_Consumer"); String message = (String)consumer.jmsTemplate.receiveAndConvert(); System.out.println("消费者收到消息:"+message); } }
4、主题
和上述队列的代码一致,只需修改applicationContext.xml中的
<!-- jms 的工具类 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsFactory"/> <property name="defaultDestination" ref="destinationTopic"/> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean>
5、在spring中实现消费者不启动,之间通过配置监听完成
编写监听,并加入到spring容器
@Component public class MyMessageListener implements MessageListener { @Override public void onMessage(Message message) { if(null!=message&&message instanceof TextMessage){ TextMessage textMessage=(TextMessage)message; try { System.out.println("消费者接收到消息:"+ textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } } }
Spring的配置文件applicationContext.xml配置监听容器
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="destinationTopic"/>
<property name="messageListener" ref="myMessageListener"/>
</bean>
直接启动消费者: