频道栏目
首页 > 程序开发 > 综合编程 > 其他综合 > 正文
ActiveMQ与Spring的整合之异步发送电子邮件
2016-11-17 09:41:00         来源:zbw18297786698的博客  
收藏   我要投稿

ActiveMQ怎么和Spring整合呢?又该怎么发送异步的电子邮件呢?接下来红黑小编就来介绍一下ActiveMQ与Spring的整合之异步发送电子邮件的方法,希望对大家有所帮助。
1、ActiveMQ与Spring整合,消息生产方的代码

1.1 项目的目录

\

1.2 config.properties

## ActiveMQ Config
activemq.brokerURL=tcp\://192.168.2.104\:61616
activemq.userName=zhangsan
activemq.password=123
activemq.pool.maxConnections=10
#queueName 
activemq.queueName=mailqueue

1.3 spring-activemq.xml




	
	
		
		
		
		
	

	
	
		
		
	

	
	
		
		
	
	
	
	
	
	
		
			${activemq.queueName}
		
	
	
	
	
		
		
		
	

1.4 spring-context.xml



	
	
	
	
	
	

	
	

	

1.5 log4j.properties

log4j.rootLogger=INFO, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=D:/002_developer/workspace_001/zcmoni.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.logger.org.springframework=WARN

1.6 Mail.java的代码
public class Mail {

	/** 发件人 **/
	private String from;
	/** 收件人 **/
	private String to;
	/** 主题 **/
	private String subject;
	/** 邮件内容 **/
	private String content;
	
	public Mail(){}
	
	public Mail(String from, String to, String subject, String content) {
		super();
		this.from = from;
		this.to = to;
		this.subject = subject;
		this.content = content;
	}
	
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
}
1.7 MQProducer.java的代码
@Service("mqProducer")
public class MQProducer {

	private JmsTemplate jmsTemplate;

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	@Autowired
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	/**
	 * 方法名称:发送邮件信息对象

	 * 概要说明:发送邮件信息对象

	 * 
	 * @param mail
	 */
	public void sendMessage(final Mail mail) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(JSONObject.toJSONString(mail));
			}
		});
	}

}

1.8 TestProducer.java的代码

@ContextConfiguration(locations = { "classpath:spring-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class TestProducer {

	@Autowired
	private MQProducer mqProducer;

	@Test
	public void send() {
		Mail mail = new Mail();
		mail.setTo("921722376@qq.com");
		mail.setSubject("异步发送邮件");
		mail.setContent("Hi,This is a message!");

		this.mqProducer.sendMessage(mail);
		System.out.println("发送成功..");

	}

}

1.9 pom.xml的文件


	4.0.0
	activemq
	activemq-provider
	0.0.1-SNAPSHOT
	
	
	
		UTF-8
		4.11
		3.2.3.RELEASE
		4.3.1
	


	
		
			org.springframework
			spring-test
			${spring.version}
			test
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-jms
			${spring.version}
		
		
			org.springframework
			spring-oxm
			${spring.version}
		
		
			org.mybatis
			mybatis
			3.2.8
		
		
			org.mybatis
			mybatis-spring
			1.1.1
		

		
			org.apache.activemq
			activemq-all
			5.11.1
		
		
			org.apache.activemq
			activemq-pool
			5.11.1
		
		
			com.alibaba
			druid
			0.2.9
		
		
			org.aspectj
			aspectjweaver
			1.7.1
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.11
		
		
			commons-fileupload
			commons-fileupload
			1.2.2
		
		
			org.apache.commons
			commons-lang3
			3.3.1
		
		
			commons-io
			commons-io
			2.4
		
		
			org.apache.httpcomponents
			httpclient-cache
			${httpclient.version}
		
		
			org.apache.httpcomponents
			httpclient
			${httpclient.version}
		
		
			org.apache.httpcomponents
			httpcore
			4.3.2
		
		
			com.alibaba
			fastjson
			1.1.26
		
		
			org.slf4j
			slf4j-api
			1.7.10
		
		
			org.slf4j
			jul-to-slf4j
			1.7.6
		
		
			org.slf4j
			jcl-over-slf4j
			1.7.5
		
		
			javax.servlet
			jstl
			1.2
		
		
			javax.servlet
			servlet-api
			2.5
			provided
		


		
			mysql
			mysql-connector-java
			5.1.21
		

		
			junit
			junit
			${junit.version}
			test
		
	

	
		activemq-provider
		
			
				maven-compiler-plugin
				
					1.7
					1.7
					utf8
				
			

		
	

2、ActiveMQ与Spring整合,消息消费方的代码

2.1 项目的目录

\

2.2 Mail.java 见上面

2.3 MailQueueMessageListener.java 实现异步发送消息的监听器,很重要

import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

import bhz.entity.Mail;
import bhz.service.MailService;

import com.alibaba.fastjson.JSONObject;

@Component
public class MailQueueMessageListener implements
		SessionAwareMessageListener {

	@Autowired
	private JmsTemplate jmsTemplate;
	@Autowired
	private Destination mailQueue;
	@Autowired
	private MailService mailService;

	public synchronized void onMessage(Message message, Session session) {
		try {
			TextMessage msg = (TextMessage) message;
			final String ms = msg.getText();
			System.out.println("收到消息:" + ms);
			// 转换成相应的对象
			Mail mail = JSONObject.parseObject(ms, Mail.class);
			if (mail == null) {
				return;
			}
			try {
				// 执行发送业务
				mailService.mailSend(mail);

			} catch (Exception e) {
				// 发送异常,重新放回队列
				// jmsTemplate.send(mailQueue, new MessageCreator() {
				// @Override
				// public Message createMessage(Session session) throws
				// JMSException {
				// return session.createTextMessage(ms);
				// }
				// });
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2.4 log4j.properties 见上面

2.5 config.properties 加入了邮箱相关的配置(用的是163邮箱)

## ActiveMQ Configuration
activemq.brokerURL=tcp\://192.168.2.104\:61616
activemq.userName=zhangsan
activemq.password=123
activemq.pool.maxConnections=10
#queueName
activemq.queueName=mailqueue

## SMTP Configuration
mail.host=smtp.163.com
##mail.port=21
mail.username= 填写自己的163邮箱地址
mail.password= 填写自己的163邮箱密码
mail.smtp.auth=true
mail.smtp.timeout=30000
mail.default.from=填写自己的163邮箱地址

2.6 spring-activemq.xml 与上面的区别,主要在于加入了监听器,用于异步消费消息





	
	
		
		
		
		
	

	
	
		
		
	

	
	
		
		
	

	

	
	
		
		
		
	

	
        
	
		
			${activemq.queueName}
		
	

	
	

	
	
		
		
		
	

2.7 spring-mail.xml 发送邮箱的配置



       
	
	
	JavaMailSenderImpl" id="mailSender">
		
		
		
		
		
			
				${mail.smtp.auth}
				${mail.smtp.timeout}
			
		
	

	
		
			${mail.default.from}
		
	
	
	
	
		
		
		
		
		
		
		
		
	

2.8 spring-context.xml




	
	
	
	
	

	
	

	
		
	
	
	
	

2.9 MailService.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import bhz.entity.Mail;

@Service("mailService")
public class MailService {

	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private SimpleMailMessage simpleMailMessage;
	@Autowired
	private ThreadPoolTaskExecutor threadPool;

	/**
	 * 方法名称:发送邮件

	 * 概要说明:发送邮件

	 * 
	 * @param mail
	 */
	public void mailSend(final Mail mail) {
		threadPool.execute(new Runnable() {
			public void run() {
				try {
					simpleMailMessage.setFrom(simpleMailMessage.getFrom());
					simpleMailMessage.setTo(mail.getTo());
					simpleMailMessage.setSubject(mail.getSubject());
					simpleMailMessage.setText(mail.getContent());
					// mailSender.send(simpleMailMessage);
					//mailSender.send(simpleMailMessage);
					//MimeMessage
					mailSender.send(simpleMailMessage);
				} catch (MailException e) {
					e.printStackTrace();
					throw e;
				}
			}
		});
	}
}

2.10 TestConsumer.java

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestConsumer {

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-context.xml" });
			context.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

3、代码下载

点击复制链接 与好友分享!回本站首页
相关TAG标签 电子邮件
上一篇:ARM Linux:通讯
下一篇:caffe源码解析:-/src/caffe/layers/Pooling_layer(池化层)
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站