【问题标题】:Simple spring application terminates unless there is a call to Thread.sleep(...)除非调用 Thread.sleep(...) 否则简单的 spring 应用程序将终止
【发布时间】:2014-05-22 09:12:58
【问题描述】:

我看到一个使用 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");
}

问题是:

当我运行上面的简单应用程序时,日志输出显示骆驼上下文和路由已成功初始化,但应用程序立即终止。

但是,如果我在初始化应用程序上下文后添加 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>

【问题讨论】:

  • 如果在 main() 方法中的 ApplicationContext 代码之后添加以下内容会发生什么情况:context.getBean(org.apache.camel.CamelContext.class); 并取出 Thread.sleep()
  • 这似乎没有效果。应用程序在骆驼和路线开始后立即终止。
  • 这应该在没有sleep 的情况下工作。日志中有什么特别之处吗?
  • 请看我更新的帖子,我已经包含了日志输出。
  • 听起来工作线程是一个守护线程,或者在非守护线程启动和主返回之间存在竞争条件。监听器的上下文中是否有一个对象可以阻止直到关闭?

标签: java multithreading spring apache-camel


【解决方案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

也许,你可以试试这个设置并测试它是否有效。

【讨论】:

  • 谢谢,有了这个配置,我不需要 Thread.sleep。我在上面发布了我使用的配置......仍然不确定它有什么问题!
  • 您是否尝试设置更高的closeTimeout
  • 是的,这似乎没有什么不同。也许我会坚持你的配置,我认为我现在不需要一个池连接工厂。很高兴知道是什么导致了这个问题!
  • 我仍然不确定正确的方法是什么。使用您的 ActiveMQ 配置,它可以正常工作,但我有一个警告“资源泄漏:'context' 永远不会关闭”。为了解决这个问题,我添加了一个 context.close();在我的主要方法结束时。这将删除警告,但应用程序现在再次立即终止。那么 - 通过 Spring 初始化骆驼路线并让路线无限期运行的正确方法是什么?
  • 我认为将 daemon=true 参数添加到 transportConnector 会阻止 ActiveMQ 在应用程序上下文关闭时关闭,但它没有......
【解决方案2】:

看起来正确的做法是将骆驼路线作为一个独立的应用程序启动:

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();
    }

}

【讨论】:

    【解决方案3】:

    同样的事情也发生在我身上。在我的应用程序中,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) 一个。如您所见,在日志行的末尾,两个线程被标记为daemon。这意味着当主线程完成时,所有其他 Camel 线程都不会阻止应用程序停止,因为它们作为守护进程运行。在您的情况下,Thread.sleep(1000) 可能允许一些其他非守护线程启动,从而避免应用程序关闭。

    这不是解决您问题的方法,而是尝试回答您的问题:

    为什么我需要调用 Thread.sleep 来实现预期的行为?

    【讨论】:

      猜你喜欢
      • 2017-01-10
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多