1.在maven中引入依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.2.0</version>
</dependency>
2.spring配置文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd ">
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.128:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"/>
<property name="sessionCacheSize" value="100" />
</bean>
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="mqConnectionFactory" />
<property name="pubSubDomain" value="false" />
</bean>
</beans>
3.生产者
@Resource(name = "jmsQueueTemplate")
private JmsTemplate jmsTemplate;
jmsTemplate.send("bos.regist", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("telephone", entity.getTelephone());
mapMessage.setString("checkcode", checkCode);
return mapMessage;
}
});
4.消费者
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.springframework.stereotype.Service;
import cn.itcast.activemq.utils.MailUtils4Prop;
@Service
public class MailMessage implements MessageListener {
@Override
public void onMessage(Message message) {
try {
MapMessage mapMessage = (MapMessage) message;
String telephone = mapMessage.getString("telephone");
String activecode = mapMessage.getString("activecode");
String tomail = mapMessage.getString("tomail");
String emailMsg =
"【速运快递】亲,您正在激活您的账号,验证码有效期为24小时,点击这里激活:<br/><a href='http://localhost:8083/customerAction_active?telephone="
+ telephone + "&activecode=" + activecode + "'>⇒点我</a>";
MailUtils4Prop.sendMail(tomail, emailMsg);
} catch (Exception e) {
e.printStackTrace();
}
}
}