Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
h2. Context Component

...



*Available as of Camel 2.7

...

*

The *context* component allows you to create a CamelContext with a number of routes inside then treat it as a black box and refer to the local endpoints within the black box from other CamelContexts.

...



It is similar to the [Routebox] component, though the Context component tries to be really simple for end users; just a simple convention over configuration approach to refer to local endpoints inside a black box (CamelContext).

...



Maven users will need to add the following dependency to their {{pom.xml}} for this component:

...


{code
:xml
xml
}
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-context</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

Code Block
{code}

h3. URI format

{code}
context:camelContextId:localEndpointName[?options]
{code}

Or you can omit the "context:" prefix.

...



{code
}
camelContextId:localEndpointName[?options]
  • camelContextId is the ID you used to register the CamelContext into the Registry.
  • localEndpointName can be a valid Camel URI evaluated within the black box CamelContext. Or it can be a logical name which is mapped to any local endpoints. For example if you locally have endpoints like direct:invoices and seda:purchaseOrders inside a CamelContext of id supplyChain, then you can just use the URIs supplyChain:invoices or supplyChain:purchaseOrders to omit the physical endpoint kind and use pure logical URIs.

...

{code}

* *camelContextId* is the ID you used to register the CamelContext into the [Registry].
* *localEndpointName* can be a valid Camel URI evaluated within the black box CamelContext. Or it can be a logical name which is mapped to any local endpoints. For example if you locally have endpoints like *direct:invoices* and *seda:purchaseOrders* inside a CamelContext of id *supplyChain*, then you can just use the URIs *supplyChain:invoices* or *supplyChain:purchaseOrders* to omit the physical endpoint kind and use pure logical URIs.

You can append query options to the URI in the following format, {{?option=value&option=value&...}}

h3.

...

 Example

...



First you need to create a CamelContext, add some routes in it, start it and then register the CamelContext into the [Registry] (JNDI, Spring, Guice or OSGi etc).

...



This can be done in the usual Camel way from this [test case|http://svn.apache.org/viewvc/camel/trunk/components/camel-context/src/test/java/org/apache/camel/component/context/JavaDslBlackBoxTest.java?revision=1069442&view=markup] (see the createRegistry() method); this example shows Java and JNDI being used...

...



{code
}
// lets create our black box as a camel context and a set of routes
DefaultCamelContext blackBox = new DefaultCamelContext(registry);
blackBox.setName("blackBox");
blackBox.addRoutes(new RouteBuilder() {
    @Override
    public void configure() throws Exception {
        // receive purchase orders, lets process it in some way then send an invoice
        // to our invoice endpoint
        from("direct:purchaseOrder").
          setHeader("received").constant("true").
          to("direct:invoice");
    }
});
blackBox.start();

registry.bind("accounts", blackBox);
{code}

Notice in the above route we are using pure local endpoints (*direct* and *seda*). Also note we expose this CamelContext using the *accounts* ID. We can do the same thing in Spring via

...

Code Block


{code|xml}
<camelContext id="accounts" xmlns="http://camel.apache.org/schema/spring">
  <route> 
    <from uri="direct:purchaseOrder"/>
    ...
    <to uri="direct:invoice"/>
  </route>
</camelContext>
{code}

Then in another CamelContext we can then refer to this "accounts black box" by just sending to *accounts:purchaseOrder* and consuming from *accounts:invoice*.

...



If you prefer to be more verbose and explicit you could use *context:accounts:purchaseOrder* or even *context:accounts:direct://purchaseOrder* if you prefer. But using logical endpoint URIs is preferred as it hides the implementation detail and provides a simple logical naming scheme.