ActiveMQ 消息队列
. , .
---> , ,
--> --> --> .
,
.
, , ,
, . , , ,
. .
,
,
案例分析:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.0</version>
</dependency>
1. 配置生产者
1.1 spring和activeMQ整合最重要的是依赖JmsTemplate模板, 配置文件编写
<?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:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" />
<bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<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>
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="mqConnectionFactory" />
<property name="pubSubDomain" value="true" />
</bean>
</beans>
1.2 生产者代码编写
@Autowired
@Qualifier("jmsQueueTemplate")
private JmsTemplate jmsTemplate;
/**
* 为用户手机发送短信验证码
* @return
*/
@Action(value = "customer_sendSms")
public String sendMsg() {
/**
* 生成验证码, 第一步发送给手机, 第二步保存在session中
*/
String num = RandomStringUtils.randomNumeric(4);
ServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(), num);
System.out.println("手机验证码为:" + num);
final String msg = "尊敬的用户您好!本次获取的验证码为" + num + ",服务电话为333-666-999";
jmsTemplate.send("bos_sms", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
message.setString("telephone", model.getTelephone());
message.setString("msg", msg);
return message;
}
});
return NONE;
}
2.配置消费者
2.1 消费者配置文件(监听器)
<?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.xsd ">
<context:component-scan base-package="cn.itcast.bos.mq" />
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" />
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<property name="sessionCacheSize" value="100" />
</bean>
<jms:listener-container destination-type="queue" container-type="default"
connection-factory="connectionFactory" acknowledge="auto">
<jms:listener destination="bos_sms" ref="smsConsumer"/>
</jms:listener-container>
</beans>
2.2 消费者代码
package cn.itcast.bos.mq;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
@Service("smsConsumer")
public class SmsConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
MapMessage mapMessage =(MapMessage) message;
String result = "000/xxx";
try {
if(result.startsWith("000")){
System.out.println("短信发送成功, 手机号为" + mapMessage.getString("telephone") + "验证码," +
mapMessage.getString("msg"));
}else {
throw new RuntimeException("短信发送失败, 信息码为 " + result);
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}