maven
URI
Spring XML
通过Spring集成ActiveMQ有很多种方式,这里只拿其中一种来说明。
maven
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.10.0</version>
</dependency>
spring集成ActiveMQ只需要poo一个包就可以满足基本需求。
URI
URI是ActiveMQ创建的关键 。format如下:
activemq:[queue:|topic:]destinationName
activemq是你所创建的ActiveMQ的名称。
queue|topic是创建模式,队列|订阅。可选
destinationName是接受者名称。
Spring XML
下面是创建ActiveMQ的Broker的基本配置
<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://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
</camelContext>
<bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="tcp://somehost:61616"/>
</bean>
</beans>
下面的配置加入了参数设置,可以对ActiveMQ的行为做限制
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<bean id="pooledConnectionFactory"
class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="8" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConnectionFactory"/>
<property name="concurrentConsumers" value="10"/>
</bean>
<bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig"/>
</bean>
对比可以看到,下面的设置指明了start和stop方法,这是非常重要的,其次设置了连接池的大小,消费者的数目。