Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Apache Camel Developer's Cookbook

You're reading from  Apache Camel Developer's Cookbook

Product type Book
Published in Dec 2013
Publisher Packt
ISBN-13 9781782170303
Pages 424 pages
Edition 1st Edition
Languages

Table of Contents (20) Chapters

Apache Camel Developer's Cookbook
Credits
About the Authors
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. Structuring Routes 2. Message Routing 3. Routing to Your Code 4. Transformation 5. Splitting and Aggregating 6. Parallel Processing 7. Error Handling and Compensation 8. Transactions and Idempotency 9. Testing 10. Monitoring and Debugging 11. Security 12. Web Services Index

Spanning Camel contexts within a single Java process


Sometimes it is convenient to share routing logic between integrations hosted in the same container, for example, as web apps or OSGi bundles. Camel provides two components that allow you to do this, synchronously through a Direct VM Component, or asynchronously through a VM Component.

This recipe will show you how a Camel route can call another route running in a different Camel context.

Getting ready

In order for messages to be passed between routes using the vm: or direct-vm: transports, the exact same instance of the camel-core library must be available within the classloader hierarchy of both applications.

In an application server such as Apache Tomcat, this means placing the camel-core.jar file in the /ext directory of the server. Applications deployed onto the server should not contain camel-core within their WAR files.

When using an OSGi container such as Apache Karaf or Apache ServiceMix, it is simply a case of ensuring that the camel-core bundle is installed and running.

No additional work is necessary to use these transports if you intend to communicate between Camel contexts in the same application.

The Java code for this recipe is located in the org.camelcookbook.structuringroutes.vm package. The Spring XML files are located under src/main/resources/META-INF/spring and prefixed with vm.

How to do it...

Create a route to be shared consuming (from) with the vm: endpoint, and reference it from other routes using the same endpoint name.

  1. Split out the integration logic that you want to share into a route and select a VM Component to consume from that best suits your threading requirements. If your route should handle requests using the same thread as the caller, choose direct-vm:, otherwise if you want the exchange to be processed asynchronously by a different thread, choose vm:. Give the endpoint a name that will be unique across the JVM.

    In the XML DSL, this is written as follows:

    <route>
      <from uri="vm:logMessageToBackendSystem"/>
      <to uri="..."/>
    </route>

    In the Java DSL, you express the same thing as:

    from("vm:logMessageToBackendSystem").to(...);
  2. Invoke the shared route from within the top-level route that needs to make use of this logic by using the same URI prefix.

    In the XML DSL, write the following:

    <route>
      <from uri="..."/>
      <to uri="vm:logMessageToBackendSystem"/>
    </route>

    In the Java DSL, this is written as:

    from(...).to("vm:
    logMessageToBackendSystem");

How it works...

The VM Component is equivalent in functionality to SEDA, but works across applications within the same JVM. All of the same configuration options for a seda: endpoint also apply to the vm: endpoint. As in seda: endpoints, care should be taken that the endpoint name used in the top-level route matches that of the shared route. Otherwise, the exchange is placed onto a named in-memory queue that will never be consumed from.

Aside from working across applications, direct-vm: is functionally equivalent to direct:. The endpoint uses the same thread between the two applications, meaning that it can participate in the same transactions. This should be tested thoroughly to ensure that applications have been correctly configured. For this to work, both applications should make use of the same transactional resources (for example, JDBC DataSource or JMS ConnectionFactory) with the same transaction manager.

Note

When using direct-vm: you should pay particular attention to the order in which the applications or contexts are started up. You can use the block=true option on direct-vm: so that it will block until there is an active consumer associated with it. If the top-level route starts sending messages to the shared route before it is available, an exception similar to the following will be thrown by the Camel runtime:

org.apache.camel.CamelExchangeException : No consumers available on endpoint: Endpoint[direct-vm://someMissingEndpoint]

See also

  • VM Component: http://camel.apache.org/vm.html

  • Direct-VM Component: http://camel.apache.org/direct-vm.html

You have been reading a chapter from
Apache Camel Developer's Cookbook
Published in: Dec 2013 Publisher: Packt ISBN-13: 9781782170303
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}