activeMq是一个消息中间件,主要是为了解耦,把业务代码和第三方功能代码分离开来,减少服务器响应时间,提高用户的体验度;
activeMq分为两种模式,一种是点对点(queue),第二种是发布订阅(topic);
queue:一个生产者,一个消费者,如果消费者宕机了,没有消费掉消息,则消息会默认保存在activeMq的服务器上,直到消费者将其消费掉,这种模式比较安全,消息不会丢失
topic:发布订阅模式,就是广播模式,一次生产,多个接受,这个模式默认只通知一次,默认只有当时在线的消费者才能消费,如果,通知了,没有被消费者消费的话,消息就会丢失,所有此模式需要进行持久化设置,这样当消费者没有消费消息是,生产的消息会持久化到硬盘上
配置topic的持久化
定义JmsTemplate的Topic类型
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="cachingConnectionFactory" />
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true" />
<!-- 进行持久化 -->
<property name="deliveryMode" value="2" />
<!-- 开启服务质量的开关 开启之后上面的配置才会生效 -->
<property name="explicitQosEnabled" value="true"/>
</bean>
activeMq和spring整合后 生产者的xml配置
<?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 ">
<!-- ActiveMQ 连接工厂 -->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
<!-- <amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> -->
<!--配置activeMq的连接工厂-->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
<property name="userName" value="admin"></property>
<property name="password" value="admin"></property>
</bean>
<!-- Spring 提供的连接工厂 -->
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="mqconnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- spring管理activeMq的连接工厂 -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- 同上,同理 -->
<!-- <constructor-arg ref="amqConnectionFactory" /> -->
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
<!-- 开启服务质量的开关 开启之后上面的配置才会生效 -->
<property name="explicitQosEnabled" value="true"/>
</bean>
<!-- Spring JmsTemplate 的消息生产者 start-->
<!-- 定义JmsTemplate模板的Queue类型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 引入spring提供的连接工厂 -->
<constructor-arg ref="mqconnectionFactory" />
<!-- 非pub/sub模型(发布/订阅),即队列模式 -->
<property name="pubSubDomain" value="false" />
</bean>
<!-- 定义JmsTemplate的Topic类型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="mqconnectionFactory" />
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true" />
<!-- 进行持久化 -->
<property name="deliveryMode" value="2" />
<!-- 开启服务质量的开关 开启之后上面的配置才会生效 -->
<property name="explicitQosEnabled" value="true"/>
</bean>
</beans>
activeMq和spring整合后 消费者的xml配置
<?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 ">
<!-- ActiveMQ 连接工厂 -->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
<!-- <amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> -->
<!--activeMq提供的连接工厂-->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
<property name="userName" value="admin"></property>
<property name="password" value="admin"></property>
</bean>
<!-- Spring 提供的连接工厂 -->
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- 同上,同理 -->
<!-- <constructor-arg ref="amqConnectionFactory" /> -->
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 定义Queue监听器 -->
<jms:listener-container destination-type="queue" container-type="default"
connection-factory="connectionFactory" acknowledge="auto">
<!-- 默认注册bean名称,应该是类名首字母小写 -->
<jms:listener destination="queue_msg" ref="smsConsumer1"/>
<jms:listener destination="queue_msg" ref="smsConsumer2"/>
</jms:listener-container>
<!-- 定义topic监听器 --> <!--destination-type=”durableTopic”配置成持久化Topic类型-->
<jms:listener-container destination-type="durableTopic" container-type="default"
connection-factory="mqConnectionFactory" acknowledge="transacted" transaction-manager="jmsTransactionManager">
<!-- 配置监听目标 和监听的对象 -->
<jms:listener destination="spring_topic" ref="consumerTopic1" subscription="test.Consumer"/>
<jms:listener destination="spring_topic" ref="consumerTopic2" subscription="test.Consumer1"/>
</jms:listener-container>
</beans>
和spring整合后的生产者代码
@ParentPackage("json-default")
@Namespace("/")
@Controller
<!--定义为多例-->
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
<!--注入点对点的模板对象-->
@Resource(name="jmsQueueTemplate")
private JmsTemplate jmsTemplate;
/*获得手机验证码*/
@Action(value="customer_sendMessage")
public String sendsms() throws IOException{
/*得到随机验证码*/
String randomCode = RandomStringUtils.randomNumeric(4);
System.out.println(randomCode);
/*把验证码存入session*/
ServletActionContext.getRequest().getSession().setAttribute(customer.getTelephone(),randomCode);
final String msg = "尊敬的用户您好,本次获取的验证码为:"+ randomCode + ",服务电话:4006184000";
jmsTemplate.send("bos_sms", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage map = session.createMapMessage();
map.setString("telephone", customer.getTelephone());
map.setString("msg", msg);
return map;
}
});
return NONE;
}
和spring整合后消费者代码,需要实现MessageListener接口
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.springframework.stereotype.Component;
@Component("smsConsumer")
public class SmsConsumer implements MessageListener{
@Override
public void onMessage(Message message) {
//存的map集合
MapMessage mapMessage = (MapMessage) message;
//存的文本类型
TextMessage text = (TextMessage) message;
//执行相应的业务业务逻辑
System.out.println(text);
}
}