You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Ravi Nallappan <ra...@gmail.com> on 2016/01/12 05:24:06 UTC

Starting order for Consumers and Producers

Hi,

Is it by design that all producers starts before starting consumers?

I have created custom server and client endpoints. Custom server endpoints
supposed to start and listen to a port, followed by custom client producer
supposed to bind to the port. From my observation, client producer always
starts first hence it fails to bind. Both these listening and binding
happening in "@Override protected void doStart() throws Exception {...}". 

Is it possible to force the custom server consumer to start before custom
client producer?

The need for this is to allow me to write Junit test-cases without depending
on external servers.

Thanks and regards,
Ravi Nallappan

Following are steps how I observed this behaviour:

Generate Camel custom Endpoints
===========================
mvn archetype:generate                            \
  -DarchetypeGroupId=org.apache.camel.archetypes  \
  -DarchetypeArtifactId=camel-archetype-component \
  -DarchetypeVersion=2.16.0                       \
  -DgroupId=com.acme.mediation                    \
  -DartifactId=acme-customserver                  \
  -Dname=Custom                                   \
  -Dscheme=custom-server                          \
  -DinteractiveMode=false

mvn archetype:generate                            \
  -DarchetypeGroupId=org.apache.camel.archetypes  \
  -DarchetypeArtifactId=camel-archetype-component \
  -DarchetypeVersion=2.16.0                       \
  -DgroupId=com.acme.mediation                    \
  -DartifactId=acme-customclient                  \
  -Dname=Custom                                   \
  -Dscheme=custom-client                          \
  -DinteractiveMode=false
  
Test Case
=========
public class CustomComponentTest extends CamelTestSupport {
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            public void configure() {
                from("custom-server://host1")
                  .to("custom-server://host1")
                  .to("mock:result1")
	;

                from("custom-client://host1")
                  .to("custom-client://host1")
                  .to("mock:result2")
	;
            }
        };
    }

    @Test
    public void testCustom() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result1");
        mock.expectedMinimumMessageCount(1);       
        assertMockEndpointsSatisfied();
    }
}

Run Test Case
==============
cd acme-customserver/; mvn install; cd ..
cd acme-customclient/; >/tmp/testCustom.log; mvn
-Dtest=CustomComponentTest#testCustom test | tee -a /tmp/testCustom.log
egrep "Starting consumer|Starting producer" /tmp/testCustom.log
[                          main] CustomProducer                 DEBUG
Starting producer: Producer[custom-server://bar]
[                          main] MockEndpoint$1                 DEBUG
Starting producer: Producer[mock://result1]
[                          main] CustomProducer                 DEBUG
Starting producer: Producer[custom-client://bar]
[                          main] MockEndpoint$1                 DEBUG
Starting producer: Producer[mock://result2]
[                          main] DefaultCamelContext            DEBUG
Starting consumer (order: 1000) on route: route1
[                          main] CustomConsumer                 DEBUG
Starting consumer: Consumer[custom-server://foo]
[                          main] DefaultCamelContext            DEBUG
Starting consumer (order: 1001) on route: route2
[                          main] CustomConsumer                 DEBUG
Starting consumer: Consumer[custom-client://foo]
Note that Producer[custom-client] starts before Consumer[custom-server].