activeMQ 与TOMCAT 结合

[i]activeMQ内置在Tomcat(6.0)中:[/i]

[b]activemq.xml[/b]

<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
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">


<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" persistent="false" useJmx="false">

<persistenceAdapter>
<amqPersistenceAdapter directory="activemq-data" maxFileLength="6mb"/>
</persistenceAdapter>

<transportConnectors>
<transportConnector uri="tcp://localhost:61616"/>
<transportConnector uri="stomp://localhost:61613"/>
</transportConnectors>

</broker>

</beans>


[b]web.xml[/b]

<context-param>
<param-name>brokerURI</param-name>
<param-value>/WEB-INF/activemq.xml</param-value>
</context-param>

<listener>
<listener-class>org.apache.activemq.web.SpringBrokerContextListener</listener-class>
</listener>




[b]tomcat的conf的context.xml(添加JNDI resouce)[/b]


<Resource
name="jms/NormalConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="localhost"
useEmbeddedBroker="false"/>

<Resource name="jms/queue/MyQueue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="MY.TEST.FOO.QUEUE"/>



[b]spring 的 applicationContext.xml[/b]


<!--##START using jndi to get resouce on tomcat server -->
<!--JNDI resource defined in 'tomcat/conf/context.xml' -->
<bean id="connectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jms/NormalConnectionFactory</value>
</property>

</bean>
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jms/queue/MyQueue</value>
</property>
</bean>
<!--##END using jndi to get resouce on tomcat server -->






<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="destination" />
<property name="receiveTimeout" value="6000" />
</bean>

<!-- WORK POJO -->
<bean id="rantzMdp"
class="com.test.activemq.BizPojo" />

<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="purePojoMdp" />
</bean>

<bean id="purePojoMdp"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="rantzMdp" />
<property name="defaultListenerMethod"
value="processMessage" />
</bean>



[b]BizPojo.java[/b]

public class BizPojo {

public void processMessage(Map map){
//business code
//Do time-consuming job ,sleep 3s in here.
// try {
// Thread.sleep(3000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println("spring adapter(message listener) get message: "+(String)map.get("message"));
}


默认在tomcat的bin下生成active-data文件夹,你的队列的数据存放在activemq-data\kr-store\data\data-queue-data-1中
hash-index-queue-data_queue#3a#2f#2fMY.TEST.FOO.QUEUE是你的队列的索引文件。
每次发生消息,可以看到在增长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值