JMS之activemq消息持久化

JMS 即 java message service 是为java提供了一种创建、发送、接收消息的通用方法。可以将复杂的系统进行业务分离,变成灵活的高度解耦合的布局。同时对我们的日常业务需求开发,提供了非常灵活的业务解决方案。比如缴费还款送积分,送积分的业务逻辑不能影响到缴费还款的业务逻辑,所以最好的,就是缴费/还款逻辑执行完成之后,通过一种方式告诉积分系统,给用户发送积分,发送积分的结果不要影响到复杂的缴费还款的过程。这种情况下,采用jms进行异步处理,便是一个很好的选择。

要使用消息的方式来进行系统交互,我们需要一个消息中间平台,来进行消息的接受转发,同时处理复杂的消息持久化等问题。本文我们采用activemq来做实验。这样的架构下,我们的系统通常会变成如下架构:

 消息产生者 -> 消息中心 -> 消息消费者

1、消息的两种传播方式

JMS支持两种消息传播:PTP 和 PUB/SUB

PTP : 点对点发送。消息的发送方将消息放入管道中,消息的接收方从管道中取出消息并处理。

PUB/SUB : 发布/订阅方式。消息的发布者将自己的主题放入消息中心,同时进行消息投递,消息订阅者只获取自己订阅的消息。

jms为了支持上述两种模式,提供了两套针对同样接口的实现,对照关系如下:

 

ConnectionFacatory:被管理的对象,由客户端(发布者/接受者)使用,用来创建一个链接。

Connection:提供一个JMS消息的活动链接。

Destination:封装了消息目的地,或者主题类型。

Session:一个用来发送和接受消息的线上上下文。

MessageProducer:由session创建的,用来发送消息的对象。

MessageConsumer:由session创建的用来接受消息的对象。

2、jms消息模型

Jms的消息分为三部分:消息头、消息属性、消息体

消息头:包含了消息的客户端和提供者用来路由和识别消息的数据。

 消息头包含的字段:

 JMSDestination:包含了消息发往的目的地或者主题信息。

 JMSDeliveryMode:消息传送模式。spring提供额jms模板提供了2种模式(有默认模式):DEFAULT_DELEVERY_MODE 默认模式、DEFAULT_PRIORITY、DEFAULT_TIME_TO_LIVE

 JMSMessageID:消息标示,唯一性,每个消息都不同,即便是承载者相同消息体的消息。

 JMSTimestamp:发送时间

 JMSCorrelationID:与当前消息关联的其他消息的标示

 JMSReplyTo:回复消息的目的地。带有这样属性的消息通常是发送方希望有一个响应,这个响应是可选的。

 JMSRedelivered:带有该字段的消息通常过去发送过但是没有被确认,如果要再次发送,提供者必须设置该字段。如果true,则消息接受者必须进行消息重复处理的逻辑。

 JMSType:消息类型标示。官方文档的解释:

 

JMSType头字段包含了由客户端在发送消息时提供的消息类型标识。一些消息提供者使用消息库来存储由应用发送的消息定义。type头字段可以引用提供者库中的消息定义。JMS没有定义一个标准的消息定义库,也没有定义这个库中所包含的各种定义的命名策略。一些消息系统要求每个被创建的应用消息都必须有一个消息类型定义,并且每个消息都指定它的类型。为了能够使JMS工作于这些消息系统提供者,无论应用是否使用,JMS客户端最好赋值JMSType ,这样可以保证为需要该头字段的提供者提供了正确的设置。为了保证移植性,JMS客户端应使用安装时在提供者消息库中定义的语义值作为JMSType的值。 

 

 JMSExpiration :过期时间。

 JMSPriority:优先级。

消息属性:包括了标准投资段之外的额外添加给消息的可选的字段。比如 应用指定的属性。

消息体:消息携带的内容。

3、消息传输编程步骤

 1)使用jndi获取一个ConnectionFacatory对象;

 2)使用jndi获取一个或者多个Destination对象;

 3)使用ConnectionFactory创建一个JMS连接;

 4)使用连接创建Jms session;

 5)使用session和destination创建MessageProducers和MessageConsumers

 6)使用Connection进行传输消息;


上述是jms的基础知识,简单了解可以便于下面的应用。jms本身提供了jar可以下载并使用相关配置,结合消息系统来完成消息的发送和接受等操作。但是一种便捷的方式,为加快开发,可以使用spring提供的jms模板,即JmsTemplate,这个类似于jdbcTemplate。

我们演示PTP和PUB/SUB两种模式的配置。

先看下基础公用的类:

我们定义:消息发送者、消息接受者、消息转换器

  1. /** 
  2.  * message sender 
  3.  * @description <p></p> 
  4.  * @author quzishen 
  5.  * @project NormandyPositionII 
  6.  * @class MessageSender.java 
  7.  * @version 1.0 
  8.  * @time 2011-1-11 
  9.  */  
  10. public class MessageSender {  
  11.     // ~~~ jmsTemplate  
  12.     public JmsTemplate jmsTemplate;  
  13.       
  14.     /** 
  15.      * send message 
  16.      */  
  17.     public void sendMessage(){  
  18.         jmsTemplate.convertAndSend("hello jms!");  
  19.     }  
  20.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  21.         this.jmsTemplate = jmsTemplate;  
  22.     }  
  23. }  

  1. /** 
  2.  * message receiver 
  3.  * @description <p></p> 
  4.  * @author quzishen 
  5.  * @project NormandyPositionII 
  6.  * @class MessageReceiver.java 
  7.  * @version 1.0 
  8.  * @time 2011-1-11 
  9.  */  
  10. public class MessageReceiver implements MessageListener {  
  11.     /* (non-Javadoc) 
  12.      * @see javax.jms.MessageListener#onMessage(javax.jms.Message) 
  13.      */  
  14.     public void onMessage(Message m) {  
  15.         System.out.println("[receive message]");  
  16.           
  17.         ObjectMessage om = (ObjectMessage) m;  
  18.         try {  
  19.             String key1 = om.getStringProperty("key1");  
  20.               
  21.             System.out.println(key1);  
  22.               
  23.             System.out.println("model:"+om.getJMSDeliveryMode());  
  24.             System.out.println("destination:"+om.getJMSDestination());  
  25.             System.out.println("type:"+om.getJMSType());  
  26.             System.out.println("messageId:"+om.getJMSMessageID());  
  27.             System.out.println("time:"+om.getJMSTimestamp());  
  28.             System.out.println("expiredTime:"+om.getJMSExpiration());  
  29.             System.out.println("priority:"+om.getJMSPriority());  
  30.         } catch (JMSException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34. }  

  1. /** 
  2.  * message converter 
  3.  * @description <p></p> 
  4.  * @author quzishen 
  5.  * @project NormandyPositionII 
  6.  * @class MessageConvertForSys.java 
  7.  * @version 1.0 
  8.  * @time 2011-1-11 
  9.  */  
  10. public class MessageConvertForSys implements MessageConverter {  
  11.     /* (non-Javadoc) 
  12.      * @see org.springframework.jms.support.converter.MessageConverter#toMessage(java.lang.Object, javax.jms.Session) 
  13.      */  
  14.     public Message toMessage(Object object, Session session)  
  15.             throws JMSException, MessageConversionException {  
  16.         System.out.println("[toMessage]");  
  17.         ObjectMessage objectMessage = session.createObjectMessage();  
  18.           
  19.         objectMessage.setJMSExpiration(1000);  
  20.         objectMessage.setStringProperty("key1", object+"_add");  
  21.           
  22.         return objectMessage;  
  23.     }  
  24.     /* (non-Javadoc) 
  25.      * @see org.springframework.jms.support.converter.MessageConverter#fromMessage(javax.jms.Message) 
  26.      */  
  27.     public Object fromMessage(Message message) throws JMSException,  
  28.             MessageConversionException {  
  29.         System.out.println("[fromMessage]");  
  30.         ObjectMessage objectMessage = (ObjectMessage) message;  
  31.           
  32.         return objectMessage.getObjectProperty("key1");  
  33.     }  
  34. }  

第一种,PTP方式的配置:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.     http://www.springframework.org/schema/context   
  9.     http://www.springframework.org/schema/context/spring-context-2.5.xsd "  
  10.     default-autowire="byName">  
  11.       
  12.     <!-- JMS PTP MODEL -->  
  13.     <!-- PTP链接工厂 -->  
  14.     <bean id="queueConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">  
  15.         <property name="brokerURL" value="tcp://127.0.0.1:61616" />  
  16.         <!-- <property name="brokerURL" value="vm://normandy.notify" /> -->  
  17.         <property name="useAsyncSend" value="true" />  
  18.     </bean>  
  19.     <!-- 定义消息队列 -->  
  20.     <bean id="dest" class="org.apache.activemq.command.ActiveMQQueue">  
  21.         <constructor-arg value="queueDest" />  
  22.     </bean>  
  23.     <!-- PTP jms模板 -->  
  24.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  25.         <property name="connectionFactory" ref="queueConnectionFactory"></property>  
  26.         <property name="defaultDestination" ref="dest" />  
  27.         <property name="messageConverter" ref="messageConvertForSys" />  
  28.         <property name="pubSubDomain" value="false" />  
  29.     </bean>  
  30.       
  31.     <!-- 消息转换器 -->  
  32.     <bean id="messageConvertForSys" class="com.normandy.tech.test.MessageConvertForSys"></bean>  
  33.       
  34.     <!-- 消息发送方 -->  
  35.     <bean id="messageSender" class="com.normandy.tech.test.MessageSender"></bean>  
  36.     <!-- 消息接收方 -->  
  37.     <bean id="messageReceiver" class="com.normandy.tech.test.MessageReceiver"></bean>  
  38.       
  39.     <!-- 消息监听容器 -->  
  40.     <bean id="listenerContainer"    
  41.         class="org.springframework.jms.listener.DefaultMessageListenerContainer">    
  42.         <property name="connectionFactory" ref="queueConnectionFactory" />    
  43.         <property name="destination" ref="dest" />    
  44.         <property name="messageListener" ref="messageReceiver" />    
  45.     </bean>  
  46. </beans>  

第二种:PUB/SUB方式的配置

我们配置两个消息订阅者,分别订阅不同的消息,这样用于判断是否成功执行了消息的发布和消息的订阅

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.     http://www.springframework.org/schema/context   
  9.     http://www.springframework.org/schema/context/spring-context-2.5.xsd "  
  10.     default-autowire="byName">  
  11.       
  12.     <!-- JMS TOPIC MODEL -->  
  13.     <!-- TOPIC链接工厂 -->  
  14.     <bean id="topicSendConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">  
  15.         <property name="brokerURL" value="tcp://127.0.0.1:61616" />  
  16.         <property name="useAsyncSend" value="true" />  
  17.     </bean>  
  18.       
  19.     <bean id="topicListenConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">  
  20.         <property name="brokerURL" value="tcp://127.0.0.1:61616" />  
  21.     </bean>  
  22.       
  23.     <!-- 定义主题 -->  
  24.     <bean id="myTopic"  class="org.apache.activemq.command.ActiveMQTopic">  
  25.       <constructor-arg value="normandy.topic"/>  
  26.     </bean>  
  27.       
  28.     <bean id="myTopic2"  class="org.apache.activemq.command.ActiveMQTopic">  
  29.       <constructor-arg value="normandy.topic2"/>  
  30.     </bean>  
  31.       
  32.     <!-- 消息转换器 -->  
  33.     <bean id="messageConvertForSys" class="com.normandy.tech.test.MessageConvertForSys"></bean>  
  34.       
  35.     <!-- TOPIC send jms模板 -->  
  36.     <bean id="topicSendJmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  37.         <property name="connectionFactory" ref="topicSendConnectionFactory"></property>  
  38.         <property name="defaultDestination" ref="myTopic" />  
  39.         <property name="messageConverter" ref="messageConvertForSys" />  
  40.         <!-- 开启订阅模式 -->  
  41.         <property name="pubSubDomain" value="true"/>  
  42.     </bean>  
  43.       
  44.     <!-- 消息发送方 -->  
  45.     <bean id="topicMessageSender" class="com.normandy.tech.test.MessageSender">  
  46.         <property name="jmsTemplate" ref="topicSendJmsTemplate"></property>  
  47.     </bean>  
  48.       
  49.     <!-- 消息接收方 -->  
  50.     <bean id="topicMessageReceiver" class="com.normandy.tech.test.MessageReceiver">  
  51.     </bean>  
  52.       
  53.     <!-- 主题消息监听容器 -->  
  54.     <bean id="listenerContainer"    
  55.         class="org.springframework.jms.listener.DefaultMessageListenerContainer">    
  56.         <property name="connectionFactory" ref="topicListenConnectionFactory" />    
  57.         <property name="pubSubDomain" value="true"/><!-- default is false -->  
  58.         <property name="destination" ref="myTopic" />  <!-- listen topic: myTopic -->  
  59.         <property name="subscriptionDurable" value="true"/>  
  60.         <property name="clientId" value="clientId_001"/>  
  61.         <property name="messageListener" ref="topicMessageReceiver" />    
  62.     </bean>  
  63.       
  64.     <!-- 主题消息监听容器2 -->  
  65.     <bean id="listenerContainer2"    
  66.         class="org.springframework.jms.listener.DefaultMessageListenerContainer">    
  67.         <property name="connectionFactory" ref="topicListenConnectionFactory" />    
  68.         <property name="pubSubDomain" value="true"/><!-- default is false -->  
  69.         <property name="destination" ref="myTopic2" />  <!-- listen topic: myTopic2 -->  
  70.         <property name="subscriptionDurable" value="true"/>  
  71.         <property name="clientId" value="clientId_002"/>  
  72.         <property name="messageListener" ref="topicMessageReceiver" />    
  73.     </bean>  
  74. </beans>  

 

单元测试代码:

  1. public class TechTest extends TestCase {  
  2.     ApplicationContext ptpApplicationContext;  
  3.     ApplicationContext topicApplicationContext;  
  4.       
  5.     @Override  
  6.     protected void setUp() throws Exception {  
  7.         super.setUp();  
  8.         ptpApplicationContext = new ClassPathXmlApplicationContext(  
  9.                 "com/normandy/tech/test/ptpContext.xml");  
  10.         topicApplicationContext = new ClassPathXmlApplicationContext(  
  11.                 "com/normandy/tech/test/topicContext.xml");  
  12.     }  
  13.       
  14.     protected Object getPtpBean(String name) {  
  15.         return ptpApplicationContext.getBean(name);  
  16.     }  
  17.       
  18.     protected Object getTopicBean(String name) {  
  19.         return topicApplicationContext.getBean(name);  
  20.     }  
  21. }  

  1. /** 
  2.  * 测试消息发送 
  3.  * @description <p></p> 
  4.  * @author quzishen 
  5.  * @project NormandyPositionII 
  6.  * @class JmsQueueTest.java 
  7.  * @version 1.0 
  8.  * @time 2011-1-11 
  9.  */  
  10. public class JmsQueueTest extends TechTest {  
  11.       
  12.     /** 
  13.      * 测试消息发送 
  14.      */  
  15.     public void testQueueSend() {  
  16.         long beginTime = System.currentTimeMillis();  
  17.         // PTP  
  18. //      MessageSender messageSender = (MessageSender) getPtpBean("messageSender");  
  19. //      messageSender.sendMessage();  
  20.           
  21.         // TOPIC  
  22.         MessageSender messageSender = (MessageSender) getTopicBean("topicMessageSender");  
  23.         messageSender.sendMessage();  
  24.         System.out.println("cost time:"+ (System.currentTimeMillis() - beginTime));  
  25.     }  
  26. }  

测试结果执行便可。

在这里,消息系统我们采用的是activemq,试想一个问题,如果消息过多,这个时候发生了宕机,消息是否会丢失?

这里就涉及到了一个新问题,即消息持久化。

activemq的消息持久化分成两种:文件和数据库(支持MySQL/oracle)。可以再其配置文件中进行配置,activemq配置文件采用的是spring的方式,所以配置起来非常的方便。

通常下载了activemq后,会有一系列的配置文件demo,可以参照其中的样例修改即可。

这里我们使用mysql作为消息持久化的数据库服务器。

将mysql的驱动包,拷贝到activemq的lib目录,配置如下:

conf/activemq.xml

  1. <persistenceAdapter>  
  2.             <!--<kahaDB directory="${activemq.base}/data/kahadb"/>-->  
  3.              <jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#mysql-ds"/>  
  4.         </persistenceAdapter>  

  1. <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  2.         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
  3.         <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>  
  4.         <property name="username" value="root"/>  
  5.         <property name="password" value="root"/>  
  6.         <property name="maxActive" value="200"/>  
  7.         <property name="poolPreparedStatements" value="true"/>  
  8.     </bean>  

特别注意的是,这里指定的数据库名称,需要事先在mysql中创建好schema。

运行activemq,可以发现自动创建了三张表:

activemq_acks

activemq_lock

activemq_msgs

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ActiveMQ是一个开源的消息中间件,它实现了JMS规范,能够在分布式系统中进行消息传递和异步通信。其工作原理如下: 1. Producer发送消息:Producer通过连接到ActiveMQ的Broker来发送消息,Producer可以使用JMS API或者其他协议来连接到ActiveMQ。 2. 消息存储:ActiveMQ消息存储在一个称为Destination的目标中,Destination可以是Queue或者Topic。Queue是点对点模式,消息只能被一个Consumer消费;Topic是发布/订阅模式,消息可以被多个Consumer消费。 3. Consumer接收消息:Consumer通过连接到ActiveMQ的Broker来接收消息,Consumer可以使用JMS API或者其他协议来连接到ActiveMQ。 4. 消息过滤:ActiveMQ支持根据消息的属性、内容和目标进行过滤,只有符合条件的消息才会被Consumer接收。 5. 消息确认:Consumer接收到消息后,需要发送确认消息ActiveMQ,表示已经接收到消息。如果Consumer没有发送确认消息ActiveMQ会认为消息没有被正确处理,重新将消息发送给其他Consumer。 6. 消息持久化ActiveMQ可以将消息持久化到磁盘上,即使Broker崩溃,消息也不会丢失。同时,ActiveMQ还支持消息的事务处理,保证消息的一致性和可靠性。 综上所述,ActiveMQ通过Broker来实现Producer和Consumer之间的消息传递和异步通信,支持点对点模式和发布/订阅模式,同时还提供了消息过滤、消息确认、消息持久化等功能,保证了消息的可靠性和一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值