当柳上原的风吹向天际的时候...

真正的快乐来源于创造

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks

JMS消息传递是一种异步过程,如果没有接受者JMS Provider应该选择将消息持久化,策略主要有文件持久化和基于数据库的持久化两种,下文是关于将未消费的消息持久化到MySql数据库的。

首先,我们要做的是将MySql数据库的驱动包放置到ActiveMQ的安装目录下的lib里。

其次,我们需要改写activemq.xml文件,它在ActiveMQ的conf目录中。具体修改部分请见下文中粗体部分:

<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at
   
    http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<beans
  
xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq
="http://activemq.apache.org/schema/core"
  xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"
>

    
<!-- Allows us to use system properties as variables in this configuration file -->
    
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
<property name="locations">
            
<value>file:${activemq.base}/conf/credentials.properties</value>
        
</property>      
    
</bean>

    
<!-- 
        The <broker> element is used to configure the ActiveMQ broker. 
    
-->
    
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data">
        
        
<!-- 
            The managementContext is used to configure how ActiveMQ is exposed in 
            JMX. By default, ActiveMQ uses the MBean server that is started by 
            the JVM. For more information, see: 
            
            http://activemq.apache.org/jmx.html 
        
-->
        
<managementContext>
            
<managementContext createConnector="false"/>
        
</managementContext>

        
<!-- 
            Configure message persistence for the broker. The default persistence
            mechanism is the KahaDB store (identified by the kahaDB tag). 
            For more information, see: 
            
            http://activemq.apache.org/persistence.html 
        
-->
        
<persistenceAdapter>
            
<jdbcPersistenceAdapter dataSource="#mysqlDataSource"/>
        
</persistenceAdapter>        
        
<!--
            For better performances use VM cursor and small memory limit.
            For more information, see:
            
            http://activemq.apache.org/message-cursors.html
            
            Also, if your producer is "hanging", it's probably due to producer flow control.
            For more information, see:
            http://activemq.apache.org/producer-flow-control.html
        
-->
              
        
<destinationPolicy>
            
<policyMap>
              
<policyEntries>
                
<policyEntry topic=">" producerFlowControl="true" memoryLimit="1mb">
                  
<pendingSubscriberPolicy>
                    
<vmCursor />
                  
</pendingSubscriberPolicy>
                
</policyEntry>
                
<policyEntry queue=">" producerFlowControl="true" memoryLimit="1mb">
                  
<!-- Use VM cursor for better latency
                       For more information, see:
                       
                       http://activemq.apache.org/message-cursors.html
                       
                  <pendingQueuePolicy>
                    <vmQueueCursor/>
                  </pendingQueuePolicy>
                  
-->
                
</policyEntry>
              
</policyEntries>
            
</policyMap>
        
</destinationPolicy> 
 
         
<!--
            The systemUsage controls the maximum amount of space the broker will 
            use before slowing down producers. For more information, see:
            
            http://activemq.apache.org/producer-flow-control.html
             
        <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage limit="20 mb"/>
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="1 gb" name="foo"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="100 mb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>
        
-->
          
        
<!-- 
            The transport connectors expose ActiveMQ over a given protocol to
            clients and other brokers. For more information, see: 
            
            http://activemq.apache.org/configuring-transports.html 
        
-->
        
<transportConnectors>
            
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
        
</transportConnectors>

    
</broker>

    
<!-- MySql dataSource -->
    
<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        
<property name="url" value="jdbc:mysql://localhost/test?relaxAutoCommit=true"/>
        
<property name="username" value="root"/>
        
<property name="password" value="hy"/>
        
<property name="poolPreparedStatements" value="true"/>
    
</bean>

    
<!-- 
        Uncomment to enable Camel
        Take a look at activemq-camel.xml for more details
         
    <import resource="camel.xml"/>
    
-->

    
<!-- 
        Enable web consoles, REST and Ajax APIs and demos
        Take a look at activemq-jetty.xml for more details 
    
-->
    
<import resource="jetty.xml"/>
    
</beans>


如上,在persistenceAdapter节点中指定mySql数据源,在broker节点外配置了bean:

    <!-- MySql dataSource -->
    
<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        
<property name="url" value="jdbc:mysql://localhost/test?relaxAutoCommit=true"/>
        
<property name="username" value="root"/>
        
<property name="password" value="hy"/>
        
<property name="poolPreparedStatements" value="true"/>
    
</bean>

上面加粗的一句指明了消息存储在mysql的test数据库中,当然你可以指定成任何你想要的数据库,注意在启动ActiveMq之前这个数据库是要存在的,否则报错。另外需要赘述的一点是activemq.xml中不要有中文,看来白狗子对非英语的抵触真是无处不在,我们所有中国人应该奋发图强恢复汉唐明荣光,让异族不得不仰视我们,当然这是后话。

下面就可以启动ActiveMQ了,向队列发送几条消息后,我们可以看看数据库里发生了什么变化,如下:


上图是ActiveMQ为了持久化消息而设立的三张表。


上图是持久化到数据库的未被消费掉的消息。

如果这些待消费消息被接收后,这张表就会空空如也,如下:


你可以使用这个程序(注意包要自己导入一下)来测试一下。

以上就是将ActiveMQ中未消费的消息持久化到MySql数据库的过程,但愿对你有所帮助。
posted on 2009-10-27 11:23 何杨 阅读(1241) 评论(0)  编辑  收藏