2
我創建了一個ActiveMQ MessageListener並使用Spring進行配置。我正在託管Tomcat的聽衆。當我啓動Web應用程序(僅包含偵聽器)時,偵聽器是否應該自動啓動?或者我需要額外的配置?在Tomcat中啓動ActiveMQ消息偵聽器?
這是我的。首先,更新web.xml中允許彈簧本身的啓動配置,
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
</context-param>
</web-app>
然後創建applicationContext.xml中配置ActiveMQ的聽衆,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.somepackage"/>
<context:property-placeholder location="classpath:env.properties"/>
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<bean id="documentListener" class="com.somepackage.SomeListener" />
<bean id="container"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="cachingConnectionFactory"/>
<property name="messageListener" ref="documentListener"/>
<property name="destinationName" value="STOCKS.MSFT" />
</bean>
<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="jmsFactory" />
<property name="sessionCacheSize" value="1" />
</bean>
</beans>
就是這樣。根據我在網絡上看到的情況,我無法分辨這是我需要的嗎?也許我需要在Tomcat中進行其他配置才能啓動監聽器?