此文档以SpringSide 发布的为准 http://wiki.springside.org.cn/display/springside/ActiveMQ+Spring+Jencks
ActiveMQ4.1 +Spring2.0的POJO JMS方案(Jencks 版)
作者:cctvx1
此文档仅适用于说明使用 Jecncks 和 使用Spring 2.0(DefaultMessageListenerContainer) 充当MDP Container之间的区别,同时
演示Jecnks 的Spring 2.0 新配置实例。
1.引入ActiveMQ ResourceAdapter 和Jencks 的XSD
在ApplicationContext.xml(Spring的配置文件)中引入ActiveMQ ResourceAdapter 和Jencks 的XML Scheam 配置文件),如下:
ActiveMQ4.1 响应Spring 2.0号召,支持了引入XML Schema namespace的简单配置语法,简化了配置的语句。
在ApplicationContext.xml(Spring的配置文件)中引入ActiveMQ的XML Scheam 配置文件),如下:
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:amq
="http://activemq.org/config/1.0"
xmlns:ampra
="http://activemq.org/ra/1.0"
xmlns:jencks
="http://jencks.org/1.3"
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.xsd
http://activemq.org/config/1.0 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd
http://activemq.org/ra/1.0 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-ra-4.1-incubator-SNAPSHOT.xsd
http://jencks.org/1.3 http://repository.codehaus.org/org/jencks/jencks/1.3/jencks-1.3.xsd"
>
由于ActiveMQ RA和Jencks 那个XSD 仍然有部分错误,因此使用的是自行修改过的XSD。(是xs:any元素引起的错误)
先在ClassPath根目录放一个修改过的activemq-ra-4.1-incubator-SNAPSHOT.xsd和jencks-1.3.xsd。
同样修改 ClassPath 下面META-INF\spring.schemas 增加内容如下。这个spring.schemas是spring自定义scheam的配置文件,请注意"http:\://"部分写法
http\://people.apache.org/repository/org.apache.activemq/xsds/activemq-ra-4.1-incubator-SNAPSHOT.xsd=/activemq-ra-4.1-incubator-SNAPSHOT.xsd
http\://repository.codehaus.org/org/jencks/jencks/1.3/jencks-1.3.xsd=/jencks-1.3.xsd
2. 配置方案
2.1 基础零件
1. 配置ActiveMQ Broker 参见 ActiveMQ+Spring
2. 配置ActiveMQ Resource Adapter
<amqra:managedConnectionFactory id="jmsManagedConnectionFactory" resourceAdapter="#resourceAdapter"/><amqra:resourceAdapter id="resourceAdapter" serverUrl="vm: />
3. 配置Jencks 基础配置
具体的配置可以参见Jencks的XSD
1
<!--
jencks PoolFactory config
-->
2
<
jencks:singlePoolFactory
id
="poolingSupport"
maxSize
="16"
minSize
="5"
blockingTimeoutMilliseconds
="60"
idleTimeoutMinutes
="60"
matchOne
="true"
matchAll
="true"
selectOneAssumeMatch
="true"
/>
<!--
jencks XATransactionFactory
-->
3
<
jencks:xATransactionFactory
id
="transactionSupport"
useTransactionCaching
="true"
useThreadCaching
="true"
/>
4
<!--
jencks ConnectionManagerFactory
-->
5
<
jencks:connectionManagerFactory
id
="connectionManager"
containerManagedSecurity
="false"
poolingSupport
="#poolingSupport"
transactionSupport
="#transactionSupport"
/>
<!--
jencks TransactionContextManagerFactory
-->
6
<
jencks:transactionContextManagerFactory
id
="transactionContextManagerFactory"
/>
7
4. 配置给JmsTemplate使用的connectionFactory (主要是生成者/发送者 使用)
这里注意下,在配置jmsTemplate的使用的targetConnectionFactory就是使用jencks配置的connectionManager
<!--
spring config jms with jca
-->
<
bean
id
="jmsManagerConnectionFactory"
class
="org.springframework.jca.support.LocalConnectionFactoryBean"
>
<
property
name
="managedConnectionFactory"
>
<
ref
local
="jmsManagedConnectionFactory"
/>
</
property
>
<
property
name
="connectionManager"
>
<
ref
local
="connectionManager"
/>
</
property
>
</
bean
>
<!--
Spring JmsTemplate config
-->
<
bean
id
="jmsTemplate"
class
="org.springframework.jms.core.JmsTemplate"
>
<
property
name
="connectionFactory"
>
<!--
lets wrap in a pool to avoid creating a connection per send
-->
<
bean
class
="org.springframework.jms.connection.SingleConnectionFactory"
>
<
property
name
="targetConnectionFactory"
ref
="jmsManagerConnectionFactory"
/>
</
bean
>
</
property
>
<!--
custom MessageConverter
-->
<
property
name
="messageConverter"
ref
="orderMessageConverter"
/>
</
bean
>
5*. 配置Spring 2.0的MessageListenerAdapter,保证不需要用户实现MessageListener*
见ActiveMQ+Spring
6.配置Jecnks 充当MDP的Container
就是把上面的MessageListenerAdapter配置到Jencks里面,完成整个MDP的配置
<!-- Jencks Container-->
<jencks:jcaContainer> <jencks:bootstrapContext>
<jencks:bootstrapContextFactory threadPoolSize="25" />
</jencks:bootstrapContext>
<jencks:connectors>
<!-- use jencks container (use spring MessageListenerAdapter)-->
<jencks:connector ref="messageListener">
<jencks:activationSpec>
<amqra:activationSpec destination="org.apache.activemq.spring.Test.spring.embedded" destinationType="javax.jms.Queue" />
</jencks:activationSpec>
</jencks:connector> </jencks:connectors> <jencks:resourceAdapter>
<amqra:resourceAdapter serverUrl="vm://localhost" />
</jencks:resourceAdapter>
</jencks:jcaContainer>
3. 说明
Spring 2.0本身使用DefaultMessageListenerContainer 可以充当MDP中的Container角色,但是鉴于Jencks是对于JCA标准,它不仅仅能够提供jms的jca整合,包括其他资源比如jdbc都可以做到jca管理
所以,同时完成了这个ActiveMQ+Spring+Jencks 配置演示,更多的针对生产系统的JCA特性展示,会在稍后的开发计划讨论中确定。