环境:本地需安装activemq,且访问正常,安装方法网上很多,这里省略。
1.添加相关jar包,我这里使用的是maven,你也可以选择其他方式添加。下面是maven相关配置:
<!-- ActiveMQ JMS 相关jar begin --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> <version>5.8.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.14.0</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-j2ee-management_1.1_spec</artifactId> <version>1.0.1</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jms_1.1_spec</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.1.5.RELEASE</version> </dependency> <!-- ActiveMQ JMS 相关jar end -->
2.编写发送和接收处理类
public class MyMessageConverter implements MessageConverter { public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { return session.createObjectMessage((Serializable) object); } public Object fromMessage(Message message) throws JMSException, MessageConversionException { ObjectMessage objMessage = (ObjectMessage) message; return objMessage.getObject(); }
3.编写发送实现类,接口类这里就不贴了。
public class ProducerServiceImpl implements ProducerService { private JmsTemplate jmsTemplate; protected final Logger logger = Logger.getLogger(getClass()); public void sendMessage(final Serializable obj) { logger.debug("---------------生产者向MQ发送消息-----------------"); jmsTemplate.convertAndSend(obj); } public JmsTemplate getJmsTemplate() { return jmsTemplate; } @Resource public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; }
4.编写监听类
public class MyMessageListener implements MessageListener { @Autowired private MyMessageConverter messageConverter; protected final Logger logger = Logger.getLogger(getClass()); public void onMessage(Message message) { if (message instanceof ObjectMessage) { ObjectMessage objMessage = (ObjectMessage) message; try { Mail mail = (Mail) messageConverter.fromMessage(objMessage); logger.debug("接收到一个ObjectMessage,包含Email对象,准备发送邮件!"); new MailUtil().send(mail); } catch (JMSException e) { logger.info("接收到一个ObjectMessage,但处理失败!"); } } }
5.配置XML
<context:component-scan base-package="com.guesslive" /> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory"/> <!-- 消息转换器 --> <property name="messageConverter" ref="emailMessageConverter"/> <property name="defaultDestination" ref="queueDestination" /> <property name="explicitQosEnabled" value="true" /> <!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置为true,默认false--> <property name="deliveryMode" value="2" /> <!-- 发送模式 DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久 默认2-持久--> </bean> <!-- 类型转换器 --> <bean id="emailMessageConverter" class="com.guesslive.admin.common.mq.MyMessageConverter"/> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://127.0.0.1:61616"/> <property name="userName" value="admin"/> <property name="password" value="admin"/> <property name="useAsyncSend" value="true" /> <!-- 如果传输的对象是Obeject 这里必须加上这句 否则会导致对象序列化失败 出现classnotfound异常 详细: http://activemq.apache.org/objectmessage.html --> <property name="trustAllPackages" value="true"/> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!--队列目的地--> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>email</value> </constructor-arg> </bean> <!-- 消息监听器 --> <bean id="consumerMessageListener" class="com.guesslive.admin.common.mq.MyMessageListener"/> <!-- 消息监听容器 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="consumerMessageListener" /> </bean>
6.发送和接收
//调用向MQ推送消息的方法 producerService.sendMessage(mail);