配置嵌入式的Activemq服务

activemq提供了嵌入式的服务,即在一个JVM内部使用jms服务。我们用它来做异步发送消息。

JMS自身没有提供异步发送消息的机制,只能同步发送消息。这带来一个问题,比如外部(远端)的JMS服务器因为各种原因无法工作,那么发送消息的应用可能也连带的无法工作,这时消息会丢失。

可以使用activemq的嵌入式的服务,先同步发送消息到本地(嵌入式activemq服务),然后再用一个接收线程接收本地消息,发送到远端的JMS服务,这样即使远端JMS服务失效,消息也不会丢失,当远端服务器恢复后还能继续收到消息。

spring文件的头部要求有:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
   xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.3.0.xsd">

对xmlns:amq的schema声明,分两部分:

xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

和:

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.3.0.xsd

另外,因为使用到了xbean的功能,需要引入包,比如maven下:

<dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
    <version>3.7</version>
</dependency>

在spring中和ConnectionFactory相关的配置:

<amq:broker useJmx="false" persistent="true" brokerName="localhost">
    <amq:persistenceAdapter>
        <amq:amqPersistenceAdapter directory="activemq-data"
            indexPageSize="64kb" indexBinSize="4096" maxFileLength="2147483647" />
    </amq:persistenceAdapter>
    <amq:transportConnectors>
        <amq:transportConnector name="vm" uri="vm://localhost" />
    </amq:transportConnectors>
</amq:broker>
<amq:connectionFactory id="embeddedConnectionFactory"
    brokerURL="vm://localhost" />

 

这里使用了amq的persistent adapter,文件最大值是int型的,最大值被限制在int.MAX_VALUE,只能到2147483647,这里使用了这个最大值,不到2GB。

如果需要加大文件值,需要使用比如kapa persistent adapter,它的最大值是long型的。

PDF下載    发送文章为PDF   

这篇文章上的评论的 RSS feed TrackBack URI

Leave a Reply