Tomcat内嵌activeMQ是参考《ActiveMQ In Action(Manning-2011).pdf》但是失败了。后面发现是书中描述不全。现将最佳实践提炼出来。
内嵌activeMQ5.4.0,只需要:
1. 增加以下jar包,PS:更正,经过测试,其实spring开头的包只要有spring-2.5.6.jar(2881K)的就可以了
2并在context.xml中配置连接工厂和消息队列的JNDI资源,要注意brokerURL的设置,它只识别"/"。大家最好先参照网上的一些例子,运行下,对原理和概念了解一点,这样才能心知肚明,否则就是苍蝇乱撞了,JMS是个很强大的货,将来会写个专题。
<Resource auth="Container"
brokerName="MyActiveMQBroker"
brokerURL="vm://localhost?brokerConfig=xbean:file:${activeMQ.home}/conf/activemq.xml"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
name="ConnectionFactory"
type="org.apache.activemq.ActiveMQConnectionFactory"/>
<Resource auth="Container"
description="My Message Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
name="queue/QueueLog"
physicalName="QueueLog"
type="org.apache.activemq.command.ActiveMQQueue"/>
<!-- physicalName="QueueLog" equals Queue queue = new ActiveMQQueue("QueueLog");
because OperLogUtils has been Hardware configuration-->
<Resource auth="Container"
description="My Message Topic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
name="queue/TopicEFrame"
physicalName="TopicEFrame"
type="org.apache.activemq.command.ActiveMQTopic"/>
3然后将activeMQ.xml文件拷贝指定路径
<beans
xmlns="http://www.springframework.org/schema/beans"
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">
<list>
<value>file:${catalina.home}/conf/shine/shine.properties</value> <!--支持系统属性配置-->
<value>file:${catalina.home}/conf/shine/shine-default.properties</value>
</list>
</property>
</bean>
<broker useJmx="false" brokerName="jdbcBroker" xmlns="http://activemq.apache.org/schema/core">
<persistenceAdapter>
<jdbcPersistenceAdapter dataDirectory="${catalina.home}/activemq/data" dataSource="#oracle-ds"/>
</persistenceAdapter>
<transportConnectors>
<transportConnector name="default" uri="tcp://${activeMQ.binging.address}:${activeMQ.binging.port}"/>
</transportConnectors>
</broker>
<!-- Oracle DataSource Sample Setup -->
<bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@${com.shine.pub.database.es_system.address}:${com.shine.pub.database.es_system.port}:${com.shine.pub.database.es_system.sid}"/>
<property name="username" value="${com.shine.pub.database.es_system.user}"/>
<property name="password" value="es_system"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
</beans>