Mail类:
package com.yncp.mq.entity;
/***
* 邮件实体
* @author Administrator
*
*/
public class Mail {
/** 发件人 **/
private String from;
/** 收件人 **/
private String to;
/** 主题 **/
private String subject;
/** 邮件内容 **/
private String 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;
}
}
消费者session监听类:
package com.yncp.mq.listener;
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.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.yncp.mq.entity.Mail;
import com.yncp.mq.mail.MailTest;
@Component
public class ConsumerSessionAwareMessageListener implements
SessionAwareMessageListener {
private static final Log log = LogFactory
.getLog(ConsumerSessionAwareMessageListener.class);
@Autowired
private MailTest mailTest;
public synchronized void onMessage(Message message, Session session) {
try {
ActiveMQTextMessage msg = (ActiveMQTextMessage) message;
final String ms = msg.getText();
log.info("==>receive message:" + ms);
Mail mail = JSONObject.parseObject(ms, Mail.class);
if (mail == null) {
return;
}
try {
mailTest.sendMail(mail);
} catch (Exception e) {
log.error("==>MailException:", e);
}
} catch (Exception e) {
log.error("==>", e);
}
}
}
package com.yncp.mq.mail;
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.yncp.mq.entity.Mail;
@Component("mailTest")
public class MailTest {
@Autowired
private JavaMailSender mailSender;
@Autowired
private SimpleMailMessage simpleMailMessage;
@Autowired
private ThreadPoolTaskExecutor threadPool;
/**
* 发送邮件
* @param mail
*/
public void sendMail(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);
} catch (MailException e) {
throw e;
}
}
});
}
}
${mail.smtp.timeout}
${mail.smtp.auth}
${mail.smtp.starttls.enable}
${mail.port}
javax.net.ssl.SSLSocketFactory
false
${mail.default.from}
${queueName}
mail.host=smtp.qq.com
mail.port=465
mail.username= //开通了smtp服务的QQ邮箱
mail.password=//授权码
mail.smtp.auth=true
mail.smtp.timeout=30000
mail.smtp.starttls.enable=true
[email protected]
[email protected]
mq.brokerURL=tcp\://127.0.0.1\:61616
mq.userName=admin
mq.password=admin
mq.pool.maxConnections=10
#queueName
queueName=mq.maintest
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MQMailTest {
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring-context.xml");
context.start();
} catch (Exception e) {
System.exit(0);
}
}
}
package com.yncp.mq.client;
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.Service;
import com.alibaba.fastjson.JSONObject;
import com.yncp.mq.entity.Mail;
@Service("mqProducer")
public class MQProducer {
@Autowired
private JmsTemplate activeMqJmsTemplate;
/**
* 发送消息.
*
* @param mail
*/
public void sendMessage(final Mail mail) {
activeMqJmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(JSONObject.toJSONString(mail));
}
});
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yncp.mq.client.MQProducer;
import com.yncp.mq.entity.Mail;
public class MQClientTest {
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
context.start();
MQProducer mqProducer = (MQProducer) context.getBean("mqProducer");
// 发送邮件
Mail mail = new Mail ();
mail.setTo("[email protected]");//邮件接收方
mail.setSubject("hello");//邮件标题
mail.setContent("hello,this is first email");//邮件内容
mqProducer.sendMessage(mail);
context.stop();
} catch (Exception e) {
System.exit(0);
} finally {
System.exit(0);
}
}
}
运行MQClientTest就可以发送邮件啦。
当我们服务器端没有运行时,此时运行MQClientTest。
我们会发现此时的activemq有一个消息挂起。
当启动服务器时,消息会发送到服务器端,服务器端接受消息后会发送邮件。