环境:本地需安装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);

springMVC下集成active MQ发送邮件的更多相关文章

  1. 使用active mq

    1 windows下使用active mq 1.1 下载active mq 1.2 点击根目录\bin\win64\activemq.bat运行 1.3 登陆查看 http://localhost:8 ...

  2. 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】

    基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...

  3. 在spring+springMvc+mabatis框架下集成swagger

    我是在ssm框架下集成swagger的,具体的ssm搭建可以看这篇博文: Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构 本项目的GitHu ...

  4. Active MQ C++实现通讯记录

    Active MQ  C++实现通讯 背景知识: ActiveMQ是一个易于使用的消息中间件. 消息中间件 我们简单的介绍一下消息中间件,对它有一个基本认识就好,消息中间件(MOM:Message O ...

  5. springmvc学习笔记--ueditor和springmvc的集成

    前言: 在web开发中, 富文本的编辑器真心很重要. 有电商店铺的打理, 新闻稿/博客文章/论坛帖子的编辑等等, 这种所见即所的编辑方式, 大大方便了非技术人员从事互利网相关的工作. 因为手头有个小项 ...

  6. 烂泥:ubuntu下配置msmtp+mutt发送邮件

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 本文主要介绍如何在Linux下使用msmtp+mutt发送邮件,当然Linux下发送邮 ...

  7. ABP入门系列——使用ABP集成的邮件系统发送邮件

    ABP中对邮件的封装主要集成在Abp.Net.Mail和Abp.Net.Mail.Smtp命名空间下,相应源码在此. #一.Abp集成的邮件模块是如何实现的 分析可以看出主要由以下几个核心类组成: E ...

  8. 使用Active MQ在.net和java系统之间通信

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现 一.特性列表 ⒈ 多种语言和 ...

  9. Active MQ C#实现

    原文链接: Active MQ C#实现 内容概要 主要以源码的形式介绍如何用C#实现同Active MQ 的通讯.本文假设你已经正确安装JDK1.6.x,了解Active MQ并有一定的编程基础. ...

随机推荐

  1. Apache Kafka – KIP 32,33 Time Index

    32, 33都是和时间相关的, KIP-32 - Add timestamps to Kafka message 引入版本,0.10.0.0 需要给kafka的message加上时间戳,这样更方便一些 ...

  2. array题目合集

    414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...

  3. 手动删除portal中托管服务。

    在portal中将server作为托管联合服务器,然后发布了托管服务.若中间取消了托管联合服务器,再重新连接,那么会出现之前的托管服务无法删除的现象. 下文为怎样手动删除这些服务的方法,(不过貌似之后 ...

  4. SMTP Error: Could not connect to SMTP host

    PHPMailer是一个非常棒的开源邮件类,使用也非常简单,但是对于虚拟主机来说,往往要受到各种限制.刚才我在虚拟主机上使用PHPMailer就遇到一个“SMTP Error: Could not c ...

  5. Java-马士兵动态代理模式

    Java-马士兵动态代理模式 模拟jdk的动态代理的实现原理, 这些东西没有必要写出来,写项目的时候一般用不上,主要是为了面试和理解原理: java动态代理有什么作用 作用非常大,在很多底层框架中都会 ...

  6. jQuery 一句代码返回顶部

    兼容各大主流浏览器,jQuery返回顶部,一句代码搞定 <a class="top" href="javascript:;" style="po ...

  7. +Load和+initialize方法解析

    http://www.cnblogs.com/ider/archive/2012/09/29/objective_c_load_vs_initialize.html

  8. PHP中include引用导致不能再次相对引用文件的一个小问题

    今天遇到一个问题:php,incldue_once引入多次文件,多层引用,所以,不同目录下的文件要使用一个文件得先确定目录,比如(下例是提取出来的精简例子,组织的有点乱乱的): /homepage/i ...

  9. 视觉机器学习------K-means算法

    K-means(K均值)是基于数据划分的无监督聚类算法. 一.基本原理       聚类算法可以理解为无监督的分类方法,即样本集预先不知所属类别或标签,需要根据样本之间的距离或相似程度自动进行分类.聚 ...

  10. smarty中使用truncate出现乱码,用mb_substr解决