Spring Jms异步消息推送

来源:互联网 发布:淘宝十大动漫周边店 编辑:程序博客网 时间:2024/06/11 06:23

最近公司项目业务要求用户下单之后发送邮件通知功能,邮件发送功能我使用的是spring自带的mail功能见我的其他博客有讲解,但是邮件这段加入进去之后不能够影响原有的程序功能,比如:邮件发送失败导致系统报异常影响订单交易程序,因此,我在这里使用了异步消息推送服务,使用的技术:activeMQ+spring+jms,利用spring自带jms功能,实现了异步消息推送,这样即使邮件发送失败也不会影响主程序。好了,废话说多了,见代码。

//配置文件

<!-- started spring-jms config --><context:component-scan base-package="com.maimai.jms"/><!-- connection to amq --><amq:connectionFactory id="connectionFactory" brokerURL="tcp://localhost:61616"/><amq:queue id="queue" physicalName="spitter.queue"></amq:queue><bean id="jsmTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory"></property></bean><jms:listener-container connection-factory="connectionFactory"><jms:listener destination="spitter.queue" ref="jmsreciever" method="getStr"/></jms:listener-container><bean id="jmsreciever" class="com.maimai.jms.RecieverJMS" /><!-- ended spring-jms config -->

//发送消息定义

package com.maimai.jms;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Component;@Componentpublic class SenderJMS {@AutowiredJmsTemplate jmsTemplate;public void send(final String str){this.jmsTemplate.send("spitter.queue", new MessageCreator() {public Message createMessage(Session session) throws JMSException {// TODO Auto-generated method stubreturn session.createTextMessage(str);}});}}

//接收消息定义

package com.maimai.jms;import javax.jms.JMSException;import javax.jms.TextMessage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.stereotype.Component;@Componentpublic class RecieverJMS {@AutowiredJmsTemplate jmsTemplate;public void getStr(String str) throws JMSException{//TextMessage tm = (TextMessage) jmsTemplate.receive();//String str = tm.getText();System.out.println("==================================str="+str);}}

//程序调用jms,关键代码

public String Login() throws NoSuchAlgorithmException, UnsupportedEncodingException{this.jms.send("hello world !");String result = Constant.JSON_EMPTY;}

//打印结果

2015-08-31 17:31:06,138 [http-8080-4] DEBUG [org.springframework.jms.core.JmsTemplate] - Sending created message: ActiveMQTextMessage {commandId = 0, responseRequired = false, messageId = null, originalDestination = null, originalTransactionId = null, producerId = null, destination = null, transactionId = null, expiration = 0, timestamp = 0, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = false, type = null, priority = 0, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = false, readOnlyBody = false, droppable = false, text = hello world !}
 2015-08-31 17:31:06,145 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] DEBUG [org.springframework.jms.listener.DefaultMessageListenerContainer] - Received message of type [class org.apache.activemq.command.ActiveMQTextMessage] from consumer [ActiveMQMessageConsumer { value=ID:wwsoft-PC-63011-1441013456482-0:1:1:1, started=true }] of session [ActiveMQSession {id=ID:wwsoft-PC-63011-1441013456482-0:1:1,started=true}]
 ==================================str=hello world !
2015-08-31 17:31:06,174 [http-8080-4] DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession




0 0
原创粉丝点击