我看到一些使用Spring和Apache Camel的非常简单的Java应用程序的奇怪行为。
我在应用程序上下文中定义了以下spring bean:
<camel:camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
<camel:endpoint id="incoming" uri="activemq:queue:${myqueue}?jmsMessageType=Text" />
<camel:route id="handleIncomingEvents" autoStartup="true" trace="true">
<camel:from ref="incoming" />
<camel:bean ref="transform" method="toEvent" />
<camel:bean ref="eventConsumer" method="consumeEvent" />
</camel:route>
</camel:camelContext>
这是一个单一路线的驼峰环境。据我所知,当创建bean时,将启动一个新线程,该线程侦听传入的消息并将它们路由到我的eventConsumer。
我的应用程序的起点很简单:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
}
问题是:
当我运行上面的简单应用程序时,日志输出显示camel上下文和路由已成功初始化,但随后应用程序立即终止。
但是,如果在初始化应用程序上下文后添加Thread.sleep(1000),则应用程序永远不会终止。这是我期望的行为,因为创建了一个新的线程来监听传入的消息。
为什么我需要调用Thread.sleep来实现预期的行为?
这显然不是正确的做法 - 我做错了什么?
编辑[1]:日志中没有任何异常。无论是否调用Thread.sleep(1000),都会生成以下日志输出。
29 [main] INFO org.apache.camel.spring.handler.CamelNamespaceHandler - OSGi environment not detected.
1473 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.9.0 (CamelContext: camelContext) is starting
1474 [main] INFO org.apache.camel.management.ManagementStrategyFactory - JMX enabled. Using ManagedManagementStrategy.
1541 [main] INFO org.apache.camel.management.DefaultManagementLifecycleStrategy - StatisticsLevel at All so enabling load performance statistics
1618 [main] INFO org.apache.camel.impl.converter.AnnotationTypeConverterLoader - Found 3 packages with 15 @Converter classes to load
1637 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 168 core type converters (total 168 type converters)
1650 [main] INFO org.apache.camel.impl.converter.AnnotationTypeConverterLoader - Found 2 packages with 3 @Converter classes to load
1651 [main] WARN org.apache.camel.impl.converter.DefaultTypeConverter - Overriding type converter from: StaticMethodTypeConverter: public static org.apache.activemq.command.ActiveMQDestination org.apache.activemq.camel.converter.ActiveMQConverter.toDestination(java.lang.String) to: StaticMethodTypeConverter: public static org.apache.activemq.command.ActiveMQDestination org.apache.camel.component.activemq.ActiveMQConverter.toDestination(java.lang.String)
1664 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded additional 3 type converters (total 171 type converters) in 0.025 seconds
1946 [main] INFO org.apache.camel.spring.SpringCamelContext - Route: handleIncomingEvents started and consuming from: Endpoint[activemq://queue:myqueue?jmsMessageType=Text]
1958 [main] INFO org.apache.camel.spring.SpringCamelContext - Total 1 routes, of which 1 is started.
1958 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.9.0 (CamelContext: camelContext) started in 0.485 seconds
编辑[2]:我正在使用这个ActiveMQ配置:
<bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="pooledConnectionFactory" />
</bean>
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="maxConnections" value="8" />
<property name="maximumActive" value="500" />
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
<property name="closeTimeout" value="10" />
</bean>
</property>
</bean>
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConnectionFactory" />
<property name="transacted" value="true" />
<property name="transactionManager" ref="jmsTransactionManager" />
<property name="concurrentConsumers" value="10" />
</bean>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig" />
</bean>
<bean id="jmsConfigRetry" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConnectionFactory" />
<property name="transacted" value="true" />
<property name="transactionManager" ref="jmsTransactionManager" />
<property name="concurrentConsumers" value="1" />
</bean>
<bean id="activemqretry" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfigRetry" />
</bean>
答案 0 :(得分:1)
您未在问题中提供ActiveMQ设置。对于测试,我使用以下设置:
<!-- This creates an embedded ActiveMQ broker -->
<broker xmlns="http://activemq.apache.org/schema/core" useJmx="true" persistent="false">
<transportConnectors>
<transportConnector uri="tcp://localhost:61616" />
</transportConnectors>
</broker>
<!-- Lets connect the Camel ActiveMQ component to the embedded broker -->
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="failover:tcp://localhost:61616" />
</bean>
如果配置成功,您应该看到以下日志:
[ActiveMQ Task-1] FailoverTransport INFO Successfully connected to tcp://localhost:61616
也许,您可以尝试此设置并测试它是否有效。
答案 1 :(得分:0)
看起来正确的方法是将camel路由作为一个独立的应用程序启动:
public class Main {
public static void main(String[] args) throws Exception {
org.apache.camel.spring.Main camelMain = new org.apache.camel.spring.Main();
camelMain.setApplicationContextUri("application-context.xml");
camelMain.run();
}
}
答案 2 :(得分:0)
同样的事情发生在我身上。在我的应用中,Camel
嵌入了Spring
。为了进一步调查,我在Spring上下文中注册了StartupListener
,其中一段代码在CamelContext
启动后立即吐出线程列表。这是结果......
44:09.166 INFO [main ] Thread 'main' in group 'main' is daemon ? false.
44:09.167 INFO [main ] Thread 'TIBCO EMS TCPLink Reader (Server-80)' in group 'main' is daemon ? true.
44:09.167 INFO [main ] Thread 'Camel (camel-1) thread #0 - JmsConsumer[test.treds_2.input]' in group 'main' is daemon ? true.
现在......正如您所看到的,有两个与Camel
相关的主题:TIBCO EMS TCPLink Reader
一个和Camel (camel-1)
一个。如您所见,在日志行的末尾,两个线程被标记为守护程序。这意味着当主线程完成时,所有其他Camel线程都不会阻止应用程序因为它们作为deamon运行而停止。在您的情况下,可能Thread.sleep(1000)
允许其他一些非守护程序线程启动,从而避免应用程序关闭。
这不是您问题的解决方案,但会尝试回答您的问题:
为什么我需要调用Thread.sleep来实现预期的行为?