Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel adviceWith behaves differently when changing the order of weave statements

Tags:

apache-camel

I have the following route for demo purposes

from("direct:external")
    .routeId("external")
    .to("http4://www.third-party.com/foo").id("ext");

For testing, I would like to * replace the http4: endpoint with a direct: endpoint * add a mock: endpoint at the end of the route for verification

I've added the following adviceWithRouteBuilder

context.getRouteDefinition("external").adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        weaveAddLast().to("mock:result");
        weaveByToUri(".*http4://.*")
            .replace()
            .to("direct:foo");
    }
});

This one seems to work but if I change the order of the weave* statements, like so

public void configure() throws Exception {
    weaveByToUri(".*http4://.*")
        .replace()
        .to("direct:foo");
    weaveAddLast().to("mock:result");
}

It gives me the following error

java.lang.IllegalArgumentException: There are no outputs which matches: * in the route: Route(external)[[From[direct:external]] -> [pipeline -> [[To[direct:foo]]]]]

I would actually expect to get the same result, independent of the order.

like image 967
helpermethod Avatar asked Feb 02 '17 10:02

helpermethod


People also ask

What is Mockendpoint?

A Mock endpoint which provides a literate, fluent API for testing routes using a JMock style API. The mock endpoint have two set of methods. expectedXXX or expectsXXX - To set pre conditions, before the test is executed. assertXXX - To assert assertions, after the test has been executed.

What is camel context?

The CamelContext is the runtime system, which holds everything together as depicted in the figure below. The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages.


1 Answers

One thing to note here is that the weave* calls are only aware of the original RouteBuilder. So when you perform the weaveByUri() call first, it replaces .to("http4://www.third-party.com/foo") with .to("direct:foo"), which incidentally happens to be the last endpoint in your route. Now, when you perform the weaveAddLast() call, it looks for "http4://www.third-party.com/foo" but doesn't find it, since it was replaced by "direct:foo". This results in an exception being thrown.

So if, hypothetically, there's another endpoint after the "http4..." endpoint such that it's not the last endpoint in your route anymore, your adviceWith() should work. For instance, it will work if your original route looked something like this:

from("direct://external")
  .routeId("external")
  .to("http4://www.third-party.com/foo")
  .id("ext")
  .to("direct://bar")
;

I should note that I think that this is a bug and that the order shouldn't matter.

like image 125
Khaled Avatar answered Sep 22 '22 09:09

Khaled