Route started race condition

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Route started race condition

Minh Tran

I have a camel route that consumes from a topic and sends to a queue.


I've written some unit tests around it and sometimes it passes and on rare occasions it fails. Unit tests simply just publishes to mytopic and then checks myqueue to see if the message is there.

        public void testSend() throws JMSException {
                logger.debug("unit test start");
                producerTemplate.sendBody("hello world");
                TextMessage mesg = (TextMessage) jmsTemplate.receive("myqueue");
                assertEquals("hello world", mesg.getText());
Now when I look at the logs, I notice that even though Camel reports the route has started, it doesn't actually consume from the topic until after this. This means that potentially the unit test could publish the message to the topic before the route had registered its own consumer to it.

Here's a snippet of the logs showing the consumer being created after the route has declared it has started.

2014-04-11 12:12:46,522 [main] INFO  org.apache.camel.spring.SpringCamelContext - Route: route1 started and consuming from: Endpoint[activemq://topic:mytopic?lazyCreateTransactionManager=true&transacted=true]
2014-04-11 12:12:46,522 [main] INFO  org.apache.camel.spring.SpringCamelContext - Total 1 routes, of which 1 is started.
2014-04-11 12:12:46,537 [main] DEBUG - unit test start
2014-04-11 12:12:46,982 [ActiveMQ VMTransport: vm://localhost#1-1] DEBUG - localhost adding consumer: ID:imacporky.local-57940-1397182366748-3:2:1:1 for destination: topic://mytopic

Important thing to note is that the consumer is created AFTER the route has started and AFTER the unit test has already begun. This is the cause of the race condition. Is this expected behaviour of routes? Shouldn't the consumer be created before declaring the route has started? Btw, this problem only occurs with topics, queues won't see this problem.

I'm using 2.13.0. Thanks.