CXF Async Example

This example shows how to use the new feature in Camel 2.1 which is support for non blocking asynchronous producers by ToAsync.

Currently camel-jetty implements this to the fullest as its JettyHttpProducer supports non blocking request/reply natively in Jetty. However in cases its not natively supported in the Producer then Camel core will fallback to simulate non blocking request/response where it handles the sending the request in another thread pool. This ensures the original thread will still not block and it appears as asynchronous request/reply.

This example shows a client and a server in action. The client sends 100 webservice messages to the server over CXF which the server processes and returns a reply.

The client is working using a single threaded to route the messages to the point where they are send to the webserver. As we use non blocking asynchronous Request reply this single thread will terminate its current task and be ready immediately to route the next message. This allows us to have higher throughput as the single thread can go as fast as it can, it does not have to wait for the webserver to reply (i.e. its not blocking).

You can see the difference if you change the async=true option to async=false in the src/main/resources/META-INF/spring/camel-client.xml file.


You will need to compile this example first:

  mvn compile

The example should run if you type:

  mvn exec:java -PCamelServer

  mvn exec:java -PCamelClient

To stop the server hit ctrl + c

Sample output

When the client is running it outputs all requests and responses on the screen.

As the client is single threaded it will send the messages in order, e.g. from 0 to 99.

[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO  Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@df2925]]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO  Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@e89199]]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO  Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@437154]]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO  Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@ec7a35]]

As the HTTP server is simulating some time to process each message its replies will likely come after all the client have send all 100 messages. When they arrive they come back out of order

[    Camel thread 7: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:7 = OK]
[    Camel thread 3: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:9 = OK]
[    Camel thread 8: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:0 = OK]
[    Camel thread 1: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:2 = OK]
[    Camel thread 2: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:1 = OK]
[    Camel thread 5: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:5 = OK]
[    Camel thread 4: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:6 = OK]
[    Camel thread 6: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:8 = OK]
[    Camel thread 0: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO  Exchange[BodyType:String, Body:3 = OK]

And as you can see they are being handled by different threads, as we have configured using a poolSize=10 option.

Running synchronous

If we on the other hand change to synchronous mode, that means we will use the single thread for the entire routing and it will be blocked while waiting for the reply from the webserver. To see this in action change the async="true" to async="false".

The output is then as expected a request, reply and so forth. And of course the throughput is much lower as we are only handle a single message at the time and blocked while waiting for the webserver reply.

[          org.apache.camel.example.client.CamelClient.main()] +++  reply  +++      INFO  Exchange[BodyType:String, Body:97 = OK]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO  Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@2566c1]]
[          org.apache.camel.example.client.CamelClient.main()] +++  reply  +++      INFO  Exchange[BodyType:String, Body:98 = OK]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO  Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@26ec29]]
[          org.apache.camel.example.client.CamelClient.main()] +++  reply  +++      INFO  Exchange[BodyType:String, Body:99 = OK]
... Send done

See Also

© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram