title: Simple spring application terminates unless there is a call to Thread.sleep(...)
body: <p>I am seeing some strange behaviour with a very simple Java application which uses Spring and Apache Camel.</p>
  
  <p>I have defined the below spring bean in my application context:</p>
  
  <pre><code>&lt;camel:camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring"&gt;
      &lt;camel:endpoint id="incoming" uri="activemq:queue:${myqueue}?jmsMessageType=Text" /&gt;
      &lt;camel:route id="handleIncomingEvents" autoStartup="true" trace="true"&gt;
          &lt;camel:from ref="incoming" /&gt;
          &lt;camel:bean ref="transform" method="toEvent" /&gt;
          &lt;camel:bean ref="eventConsumer" method="consumeEvent" /&gt;
      &lt;/camel:route&gt;
  &lt;/camel:camelContext&gt;
  </code></pre>
  
  <p>This is a camel context with a single route. As far as I know, when this the bean is created, a new thread will be started which listens for incoming messages and routes them to my eventConsumer. </p>
  
  <p>My starting point for the application is simply:</p>
  
  <pre><code>public static void main(String[] args) {     
      ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
  }
  </code></pre>
  
  <p>The problem is:</p>
  
  <p>When I run the simple application above, the log output shows that the camel context and route are initialised successfully, but then the application terminates immediately.</p>
  
  <p>However, if I add a Thread.sleep(1000) after initialising the application-context, the application never terminates. This is the behaviour I expected, since there is a new thread created which listens for incoming messages.</p>
  
  <p>Why do I need to call Thread.sleep to achieve the expected behaviour?</p>
  
  <p>This is clearly not the correct way to do it - what am I doing wrong?</p>
  
  <hr>
  
  <p>Edit[1]: There is nothing unusual in the logs. The below log output is produced both with and without the call to Thread.sleep(1000).</p>
  
  <pre><code>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
  </code></pre>
  
  <hr>
  
  <p>Edit[2]: I was using this ActiveMQ configuration:</p>
  
  <pre><code>&lt;bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager"&gt;
      &lt;property name="connectionFactory" ref="pooledConnectionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
      destroy-method="stop"&gt;
      &lt;property name="maxConnections" value="8" /&gt;
      &lt;property name="maximumActive" value="500" /&gt;
      &lt;property name="connectionFactory"&gt;
          &lt;bean class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
              &lt;property name="brokerURL" value="tcp://localhost:61616" /&gt;
              &lt;property name="closeTimeout" value="10" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"&gt;
      &lt;property name="connectionFactory" ref="pooledConnectionFactory" /&gt;
      &lt;property name="transacted" value="true" /&gt;
      &lt;property name="transactionManager" ref="jmsTransactionManager" /&gt;
      &lt;property name="concurrentConsumers" value="10" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent"&gt;
      &lt;property name="configuration" ref="jmsConfig" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsConfigRetry" class="org.apache.camel.component.jms.JmsConfiguration"&gt;
      &lt;property name="connectionFactory" ref="pooledConnectionFactory" /&gt;
      &lt;property name="transacted" value="true" /&gt;
      &lt;property name="transactionManager" ref="jmsTransactionManager" /&gt;
      &lt;property name="concurrentConsumers" value="1" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="activemqretry" class="org.apache.activemq.camel.component.ActiveMQComponent"&gt;
      &lt;property name="configuration" ref="jmsConfigRetry" /&gt;
  &lt;/bean&gt;
  </code></pre>