Search code examples
javaactivemq-classicspring-camel

SpringBoot Camel ActiveMQ Embedded broker stopping and restarting unexpectedly


I have an issue with my SpringBoot Camel App.

It works very nicely with non embedded broker. Here is its configuration: THe URL is given as a String pointing either to standalone ActiveMQ serveur or embedded broker URL (vm://dpcgbroker?broker.persistent=false)

@Configuration
public class CamelConfiguration {

@Value("${broker.mqURL}")
String mqURL;


@Bean
CamelContextConfiguration contextConfiguration() {
    return new CamelContextConfiguration() {
        @Override
        public void beforeApplicationStart(CamelContext context) {


            ActiveMQComponent activeMQComponent =  ActiveMQComponent.activeMQComponent(mqURL);
            activeMQComponent.setConfiguration(getJmsConfiguration());

            context.addComponent("jms", activeMQComponent);

            DefaultShutdownStrategy shutdownStrategy = new DefaultShutdownStrategy();
        shutdownStrategy.setTimeUnit(TimeUnit.SECONDS);
            shutdownStrategy.setTimeout(20);
            context.setShutdownStrategy(shutdownStrategy);
        }



        @Override
        public void afterApplicationStart(CamelContext camelContext) {

        }


    };
}


public PooledConnectionFactory getPooledConnectionFactory() {
    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(getConnectionFactory());

    pooledConnectionFactory.setMaxConnections(30);

    return pooledConnectionFactory;
}

public ActiveMQConnectionFactory getConnectionFactory() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    connectionFactory.setBrokerURL(mqURL);
    connectionFactory.setTrustAllPackages(true);
    return connectionFactory;
}


public JmsConfiguration getJmsConfiguration() {
    JmsConfiguration jmsConfiguration = new JmsConfiguration();
    jmsConfiguration.setDisableTimeToLive(true);
    jmsConfiguration.setTransacted(true);
    jmsConfiguration.setLazyCreateTransactionManager(false);
    jmsConfiguration.setConnectionFactory(getConnectionFactory());
    jmsConfiguration.setCacheLevelName("CACHE_CONSUMER");
    return jmsConfiguration;
}



@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public PlatformTransactionManager getTransactionManager() {

    PlatformTransactionManager platformTransactionManager = new JmsTransactionManager(getConnectionFactory());
    return platformTransactionManager;
}

When I switch to embedded broker the behaviour is different and Apache ActiveMQ is starting then stopping, then restarting. I have no idea why and there are no exceptions

Here is an extract of the logs

15967 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Using Persistence Adapter: MemoryPersistenceAdapter
15991 INFO  - 5063 -  [JMX connector] org.apache.activemq.broker.jmx.ManagementContext     - JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
16106 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) is starting
16111 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) started
16111 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - For help or more information please see: http://activemq.apache.org
16132 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.TransportConnector     - Connector vm://dpcgbroker started
16205 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.TransportConnector     - Connector vm://dpcgbroker stopped
16205 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) is shutting down
16213 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) uptime 0.274 seconds
16213 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) is shutdown
16618 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.camel.processor.aggregate.AggregateProcessor     - Defaulting to MemoryAggregationRepository
16621 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.camel.processor.aggregate.AggregateProcessor     - Using CompletionTimeout to trigger after 10 millis of inactivity.
16647 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.camel.spring.SpringCamelContext     - Skipping starting of route jobStatusRoute as its configured with autoStartup=false
16656 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Using Persistence Adapter: MemoryPersistenceAdapter
16656 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:2) is starting
16657 INFO  - 5063 -  [JMX connector] org.apache.activemq.broker.jmx.ManagementContext     - JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
16657 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:2) started

The issue is that some messages are sent in the first instance of the broker that are lost when it stops, then second instance starts with empty list of messages. I have no idea why it is stopping.

PS: I do not want to use persistent embedded broker, this is useless workaround


Solution

  • After a while I understood what is happening. The whole thing is that the embedded vm broker has a special behaviour which is described in the documentation that states

    Once all VM connections to the broker have been closed, the embedded broker will automatically shutdown.

    My application was starting, then sending a message and then the consumers were started. When I was sending the message, the connection was closed, causing the broker to shutdown.

    So the solution was simply to start the consumers first, then allow producer to publish messages.

    PS: a workaround that might be useful is to use a PooledConnectionFactory. I tested and it also works, since I guess the pool maintains the connections alive. With that tricks you can start procuders and consumer in whatever order you like