ActiveMQ的安装与使用\ActiveMQ的使用代码样例(消费者)

阅读更多

	4.0.0

	
		wusc.edu.common
		edu-common-parent
		1.0-SNAPSHOT
		../edu-common-parent
	

	com.fsbay.mqtest
	edu-demo-mqconsumer
	1.0-SNAPSHOT
	war

	edu-demo-mqconsumer
	http://maven.apache.org

	
		edu-demo-mqconsumer
		
			
				${project.build.directory}/classes
				src/main/resources
				true
				
					**/*.xml
					**/*.properties
				
			
		
	

	

		
		
			antlr
			antlr
		
		
			aopalliance
			aopalliance
		
		
			org.aspectj
			aspectjweaver
		
		
			cglib
			cglib
		
		
			net.sf.json-lib
			json-lib
			jdk15
			compile
		
		
			ognl
			ognl
		
		
			oro
			oro
		
		
			commons-beanutils
			commons-beanutils
		
		
			commons-codec
			commons-codec
		
		
			commons-collections
			commons-collections
		
		
			commons-digester
			commons-digester
		
		
			commons-fileupload
			commons-fileupload
		
		
			commons-io
			commons-io
		
		
			org.apache.commons
			commons-lang3
		
		
			commons-logging
			commons-logging
		
		
			commons-validator
			commons-validator
		
		
			dom4j
			dom4j
		
		
			net.sf.ezmorph
			ezmorph
		
		
			javassist
			javassist
		
		
			log4j
			log4j
		
		
			org.slf4j
			slf4j-api
		
		
			org.slf4j
			slf4j-log4j12
		
		
			com.alibaba
			fastjson
		

		

		
		
			org.springframework
			spring-aop
		
		
			org.springframework
			spring-aspects
		
		
			org.springframework
			spring-beans
		
		
			org.springframework
			spring-context
		
		
			org.springframework
			spring-context-support
		
		
			org.springframework
			spring-core
		
		
			org.springframework
			spring-jms
		
		
			org.springframework
			spring-orm
		
		
			org.springframework
			spring-oxm
		
		
			org.springframework
			spring-test
			test
		
		
			org.springframework
			spring-tx
		

		


		
			org.apache.activemq
			activemq-all
		
		
			org.apache.activemq
			activemq-pool
		

		
			javax.mail
			mail
			1.4.7
		


	






package com.fsbay.demo.mqtest.biz;

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.Component;

import com.fsbay.demo.mqtest.params.MailParam;


/**
 * 
 * @描述: 邮件发送业务逻辑类 .
 * @作者: john .
 * @创建时间: 2015-3-18,上午1:08:22 .
 * @版本号: V1.0 .
 */
@Component("mailBiz")
public class MailBiz {

	@Autowired
	private JavaMailSender mailSender;// spring配置中定义
	@Autowired
	private SimpleMailMessage simpleMailMessage;// spring配置中定义
	@Autowired
	private ThreadPoolTaskExecutor threadPool;

	/**
	 * 发送模板邮件
	 * 
	 * @param mailParamTemp需要设置四个参数
	 *            templateName,toMail,subject,mapModel
	 * @throws Exception
	 * 
	 */
	public void mailSend(final MailParam mailParam) {
		threadPool.execute(new Runnable() {
			public void run() {
				try {
					simpleMailMessage.setFrom(simpleMailMessage.getFrom()); // 发送人,从配置文件中取得
					simpleMailMessage.setTo(mailParam.getTo()); // 接收人
					simpleMailMessage.setSubject(mailParam.getSubject());
					simpleMailMessage.setText(mailParam.getContent());
					mailSender.send(simpleMailMessage);
				} catch (MailException e) {
					throw e;
				}
			}
		});
	}
}


package com.fsbay.demo.mqtest.listener;

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

import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

import com.fsbay.demo.mqtest.biz.MailBiz;
import com.fsbay.demo.mqtest.params.MailParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @描述: 队列监听器 .
 * @作者: John .
 * @创建时间: 2015-3-17,下午11:21:23 .
 * @版本号: V1.0 .
 */
@Component
public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener {

	private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);

	@Autowired
	private JmsTemplate activeMqJmsTemplate;
	@Autowired
	private Destination sessionAwareQueue;
	@Autowired
	private MailBiz bailBiz;

	public synchronized void onMessage(Message message, Session session) {
		try {
			ActiveMQTextMessage msg = (ActiveMQTextMessage) message;
			final String ms = msg.getText();
			log.info("==>receive message:" + ms);
			MailParam mailParam = JSONObject.parseObject(ms, MailParam.class);// 转换成相应的对象
			if (mailParam == null) {
				return;
			}

			try {
				bailBiz.mailSend(mailParam);
			} catch (Exception e) {
				// 发送异常,重新放回队列
//				activeMqJmsTemplate.send(sessionAwareQueue, new MessageCreator() {
//					public Message createMessage(Session session) throws JMSException {
//						return session.createTextMessage(ms);
//					}
//				});
				log.error("==>MailException:", e);
			}
		} catch (Exception e) {
			log.error("==>", e);
		}
	}
}



package com.fsbay.demo.mqtest.params;

/**
 * 
 * @描述: 邮件参数封装类 .
 * @作者: john .
 * @创建时间: 2015-3-18,上午1:08:42 .
 * @版本号: V1.0 .
 */
public class MailParam {

	/** 发件人 **/
	private String from;
	/** 收件人 **/
	private String to;
	/** 主题 **/
	private String subject;
	/** 邮件内容 **/
	private String content;

	public MailParam() {
	}

	public MailParam(String to, String subject, String content) {
		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;
	}
}



package com.fsbay.demo.mqtest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @描述: ActiveMQ测试启动类  .
 * @作者: john .
 * @创建时间: 2015-3-17,上午2:25:20 .
 * @版本号: V1.0 .
 */
public class MQConsumer {
	private static final Log log = LogFactory.getLog(MQConsumer.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
			context.start();
		} catch (Exception e) {
			log.error("==>MQ context start error:", e);
			System.exit(0);
		}
	}
}




	

	
	

	
	

	
	

	
		
	
	
	
	





	
	
		
		
		
		
		
			
				${mail.smtp.auth}
				${mail.smtp.timeout}
			
		
	

	
		
			${mail.default.from}
		
	
	
	
	
		
		
		
		
		
		
		
		
	






	
	
		
        
        
         
	

    
	
		
		
	

	
	
		
		
	
	
	
	
	
	  
	      
	      
	    
	 
	
	
	
		
			${queueName}
		
	

	
	

	
		
		
		
	



#SMTP服务配置
mail.host=smtp.qq.com
mail.port=25
[email protected]
mail.password=XXXX
mail.smtp.auth=true
mail.smtp.timeout=30000
[email protected]


## MQ
mq.brokerURL=tcp\://192.168.4.101\:61616
mq.userName=john
mq.password=john.123
mq.pool.maxConnections=10
#queueName
queueName=wusc.edu.mqtest.v1

你可能感兴趣的:(spring,jms,mail)