Spring Jms异步消息推送

为了在用户下单后实现异步发送邮件通知,本文介绍了一种使用Spring JMS结合ActiveMQ的方法。通过这种方式,即使邮件发送失败,也不会影响主要的订单交易流程。配置文件和消息发送的日志记录显示了该系统的运作过程。

最近公司项目业务要求用户下单之后发送邮件通知功能,邮件发送功能我使用的是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;

@Component
public class SenderJMS {
	
	@Autowired
	JmsTemplate 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 stub
				
				
				return 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;

@Component
public class RecieverJMS {
	
	@Autowired
	JmsTemplate 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




源码直接下载地址: https://pan.quark.cn/s/a4b39357ea24 ### Home Bus 协议概述 Home Bus协议,亦被称为家庭总线协议,是由日本研发的一种专门应用于家庭自动化领域的通信标准。该协议在商用空调及其他家用电器的远程操控方面具备广泛的应用范围。借助Home Bus协议,设备之间能够达成高效的通信,使用户能够更加便利地监管家庭中的各类智能设备。 ### ECHONET协议及其背景 Home Bus协议与ECHONET协议存在紧密的联系,实际上,ECHONET代表了一种更为具体的技术规范,其目的在于为家庭自动化领域构建一个开放且统一的通信标准。ECHONET由ECHONET财团负责维护与支持,该组织致力于推动该协议的进步与标准化进程。ECHONET协议的应用范围不仅限于家庭环境,还包括了楼宇自动化以及工业自动化等多个领域。 #### ECHONET的历史版本及修订记录 - **Version 1.0**:发布于2000年3月18日,同年7月对联盟成员公开,最终于同年对公众发布。 - **Version 1.01**:在2001年5月23日,向联盟成员发布,主要进行了补充和勘误工作。 - **Version 2.00**:2001年8月7日,针对联盟成员发布。此版本中将电源线A和电源线B两种方式合并为单一电源线方式(基于电源线A),并对相关描述进行了调整。 - **Version 2.01**:2001年11月9日,进行了一些必要的修正,包括排版错误的修正、术语的标准化等。 - **Version 2.10**:经历预览版和草案阶段后,于2002年3月7日正式对联盟成员开放。此次更新主要涉及第3部分关于状态转换的描述修改。 -...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值