gpt4 book ai didi

java - 除非有对 Thread.sleep(...) 的调用,否则简单的 spring 应用程序将终止

转载 作者:搜寻专家 更新时间:2023-11-01 03:38:07 29 4
gpt4 key购买 nike

我在一个使用 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>

这是一个只有一条路线的 Camel 语境。据我所知,创建这个 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>

最佳答案

您没有在您的问题中提供您的 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

或许,您可以尝试此设置并测试它是否有效。

关于java - 除非有对 Thread.sleep(...) 的调用,否则简单的 spring 应用程序将终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23802484/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com