通过Spring对ActiveMQ进行配置开发,实现队列消息模式,支持消息的持久化。
在pom.xml添加jar包
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.8.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency>1.生产者
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd"> <!-- 配置JMS连接工厂 --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <!-- Session缓存数量 --> <property name="sessionCacheSize" value="10" /> <property name="targetConnectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <!-- MQ地址 --> <property name="brokerURL" value="tcp://192.168.18.43:61616" /> <!-- 是否异步发送 --> <property name="useAsyncSend" value="true" /> </bean> </property> </bean> <!-- 定义队列 --> <bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue"> <property name="physicalName" value="spring.queue" /> </bean> <!-- jmsTemplate,用于向任意地址发送消息 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="testQueue" /> <property name="sessionTransacted" value="false" /> <!-- receiveTimeout表示接收消息时的超时时间 --> <property name="receiveTimeout" value="30000" /> </bean> </beans>发送消息:
package cn.slimsmart.activemq.demo.spring.queue; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; @SuppressWarnings("resource") public class ProducerMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:queue/producer.xml"); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); jmsTemplate.convertAndSend("生产者发送队列消息"); } }2.消费者
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd"> <!-- 配置JMS连接工厂 --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <!-- Session缓存数量 --> <property name="sessionCacheSize" value="10" /> <property name="targetConnectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <!-- MQ地址 --> <property name="brokerURL" value="tcp://192.168.18.43:61616" /> </bean> </property> </bean> <!-- 定义队列 --> <bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue"> <property name="physicalName" value="spring.queue" /> </bean> <!-- 异步接收Queue消息Container --> <bean id="queueContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="testQueue" /> <property name="messageListener" ref="notifyMessageListener" /> <!-- 初始5个Consumer, 可动态扩展到10 --> <property name="concurrentConsumers" value="5" /> <property name="maxConcurrentConsumers" value="10" /> <property name="sessionTransacted" value="false" /> <!-- 设置消息确认模式为Client --> <property name="sessionAcknowledgeModeName" value="CLIENT_ACKNOWLEDGE" /> </bean> <bean id="notifyMessageListener" class="cn.slimsmart.activemq.demo.spring.NotifyMessageListener"></bean> </beans>接收消息监听:
package cn.slimsmart.activemq.demo.spring; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import com.google.gson.Gson; public class NotifyMessageListener implements MessageListener { public void onMessage(Message message) { System.out.println(new Gson().toJson(message)); if (message instanceof TextMessage) { try { System.out.println("已经收到消息:" + ((TextMessage)message).getText()); System.out.println("属性:" + ((TextMessage)message).getStringProperty("property")); } catch (JMSException e) { e.printStackTrace(); } } } }消费者启动类:
package cn.slimsmart.activemq.demo.spring.queue; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConsumerMain { @SuppressWarnings("resource") public static void main(String[] args) { new ClassPathXmlApplicationContext("classpath:queue/consumer.xml"); } }