Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Tip

If you try to match a pattern on an exact endpoint uri, then mind that URI options ordering may influence, and hence its best to match by wildcard. For example:

Code Block

mockEndpointsAndSkip("activemq:queue:foo?*")

To match the foo queue and disregard any options.

...

Code Block
java
titleRoute
java

from("direct:start")
    .to("mock:foo")
    .to("mock:bar").id("bar")
    .to("mock:result");

...

Selector

Description

selectFirst

Will only select the first matched node.

selectLast

Will only select the last matched node.

selectIndex(index)

Will only select the n'th matched node. The index is zero-based.

selectRange(from, to)

Will only select the matches node within the given range by index (both inclusive). The index is zero-based.

maxDeep(to)Camel 2.14.2/2.15: To limit the selection to at most N level deep in the Camel route tree. The first level is starting from number 1. So number 2 is the children of the 1st level nodes.

For example to remove the first .to node in route you can do as follows:

Code Block

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        // only remove the first to node in the route
        weaveByType(ToDefinition.class).selectFirst().remove();
    }
});

...

For example if you want to send a message to a mock:input endpoint you can do as follows:

Code Block

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        // send the incoming message to mock:input
        weaveAddFirst().to("mock:input");
    }
});

Likewise if you want to easily send a message to a mock:output endpoint you can do as follows:

Code Block

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        // send the outgoing message to mock:output
        weaveAddLast().to("mock:output");
    }
});

You can of course combine those in the same advice with:

Code Block

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        weaveAddFirst().to("mock:input");
        weaveAddLast().to("mock:output");
    }
});

...

You may have routes which consumes messages from endpoints which you want to substitute with another endpoint for easier unit testing. For example a JMS endpoint could be replaced with a SEDA or Direct for unit testing a route, as shown below where we replace the input of the route to a "seda:foo" endpoint:

Code Block

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        replaceFromWith("seda:foo");
    }
});

...