Versions Compared

Key

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

...

The AdviceWithRouteBuilder offers the following extra methods

Method

Description

mockEndpoints

Is used to easily mock all endpoints. See more details and examples at Mock.

mockEndpoints(pattern)

Is used to easily mock endpoints using a pattern. See more details and examples at Mock. See below for pattern matching.

weaveById(pattern)

Is used to select node(s) matching by id's, and weave in the following nodes. See below for pattern matching and examples.

weaveByToString(pattern)

Is used to select nodes(s) matching by their toString representation, and weave in the following nodes. See below for pattern matching and examples.

weaveByType(Class)

Camel 2.8: Is used to select node(s) matching by their class type (the classes from the org.apache.camel.model package), and weave in the following nodes. See below for examples.

weaveAddFirst

Camel 2.8: Is a short hand to easily weave in the following nodes in the start of the route.

weaveAddLast

Camel 2.8: Is a short hand to easily weave in the following nodes in the end of the route.

The pattern option is used for matching. It uses the same rules as the Intercept, which is applied in the following order:

...

The weaveById allows you to manipulate the rote, for example by replacing a node with other nodes. The following methods is available:

Method

Description

remove

Removes the selected node(s).

replace

Replaces the selected node(s) with the following nodes.

before

Before the selected node(s), the following nodes is added.

after

After the selected node(s), the following nodes is added.

...

Wiki Markup
{snippet:id=e2|lang=java|title=Remove|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java}

Using selectors

Available os of Camel 2.8

The following methods weaveById(pattern), weaveByToString(pattern) and weaveByType(Class) each match N+ nodes. By using optional selectors you can narrow down the nodes being used. For example if weaveByType(Class) returns 2 nodes. Then you can use a selector to indicate it should only select the first node.

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.

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();
    }
});

Using weaveAddFirst / weaveAddLast

Available os of Camel 2.8

The weaveAddFirst and weaveAddLast is a shorthand to easily add nodes to the route. These methods can only add to an existing routes. If you want to manipulate the route, then there are plenty of methods as already shown on this page.

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");
    }
});