【发布时间】:2011-01-13 15:50:49
【问题描述】:
我创建了一个 ActiveMQ MessageListener 并使用 Spring 对其进行了配置。我在 Tomcat 中托管监听器。当我启动 Web 应用程序(仅具有侦听器)时,侦听器是否应该自动启动?还是我需要额外的配置?
这就是我所拥有的。首先,更新 web.xml 以允许 spring 在启动时自行配置,
<?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 中进行一些其他配置来启动监听器?
【问题讨论】: