activeMQ内置在Tomcat(6.0)中:
activemq.xml
<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>
web.xml
<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>
tomcat的conf的context.xml(添加JNDI resouce)
<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"/>
spring 的 applicationContext.xml
<!--##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>
BizPojo.java
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是你的队列的索引文件。
每次发生消息,可以看到在增长。