spring 整合ActiveMQ
-
新建maven项目
-
·添加依赖
-
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.11</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.10</version> </dependency> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>4.15</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>5.2.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.1.RELEASE</version> </dependency> </dependencies>
-
-
配置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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.xiefei.activemq"/> <!-- 配置生产者 --> <bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <!-- 正真可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --> <bean class="org.apache.activemq.spring.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.10.130:61616"/> </bean> </property> <property name="maxConnections" value="100"/> </bean> <!-- 这个是队列目的地,点对点的Queue --> <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 通过构造注入Queue名 --> <constructor-arg index="0" value="spring-active-queue"/> </bean> <!-- 这个是队列目的地, 发布订阅的主题Topic--> <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="connectionFactory"/> <!-- 传入目的地 --> <property name="defaultDestination" ref="destinationQueue"/> <!-- 消息自动转换器 --> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean> </beans>
-
-
编写消息生产者
-
@Component public class SpringMQ_Provider { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); SpringMQ_Provider provider= (SpringMQ_Provider) ctx.getBean("springMQ_Provider"); provider.jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage tesxt = session.createTextMessage("spring和activemq的整合32122"); return tesxt; } }); System.out.println("发送消息完成"); } }
-
-
编写消息消费者
-
@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 returnvalue= (String) consumer.jmsTemplate.receiveAndConvert(); System.out.println("消费者收到的消息:"+returnvalue); }
-
-
先运行生产者在运行消费者
-
-
主题模式
-
修改spring的配置文件
<!-- 这个是队列目的地, 发布订阅的主题Topic--> <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg index="0" value="spring-active-topic"/> </bean> <!--修改消息传入目的地为topic --> <!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 传入连接工厂 --> <property name="connectionFactory" ref="connectionFactory"/> <!-- 传入目的地 --> <property name="defaultDestination" ref="destinationTopic"/> <!-- 消息自动转换器 --> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean>
-
其他都不用改,直接测试即可(先启动消费者,在启动生产者)
-