Reputation: 341
I have two seperate Routes. Both routes are basicly the same. The different are the source and the destination folder. In case of an error both routes throw the same exception. This I catch in an onException block. In this I want to write the file to an folder based on the actual route. So I would like to build in a choiche()....when(...) based on the routeId. The question is, how can I get the routeId to use it for the when. The following shows a little bit of code how I thought it could work. But is doesn't. Maybe someone has an idear.
onException(SomeValidationException.class)
.handled(true)
.useOriginalMessage()
.choice()
.when(exchangeProperty("routeId").convertToString().isEqualTo("Route1"))
.to("file:data/error/Route1")
.when(exchangeProperty("routeId").convertToString().isEqualTo("Route2"))
.to("file:data/error/Route2")
.otherwise()
.log(LoggingLevel.ERROR, "I always get here")
.end();
from("file:data/in/Route1")
.routeId("Route1")
.routePolicy(getRoutingPolicy())
.to("direct:RouteWhichWillThrowException")
.to("file:data/out/Route1");
from("file:data/in/Route2")
.routeId("Route2")
.routePolicy(getRoutingPolicy())
.to("direct:RouteWhichWillThrowException")
.to("file:data/out/Route2");
Upvotes: 0
Views: 263
Reputation: 3870
I think that your use case is actually best to use an in route error handler like follows:
from("file:data/in/Route1").routeId("Route1")
.onException(SomeValidationException.class)
.handled(true)
.useOriginalMessage()
.to("file:data/error/Route1")
.log(LoggingLevel.ERROR, "I always get here")
.end()
.routePolicy(getRoutingPolicy())
.to("direct:RouteWhichWillThrowException")
.to("file:data/out/Route1");
from("file:data/in/Route2").routeId("Route2")
.onException(SomeValidationException.class)
.handled(true)
.useOriginalMessage()
.to("file:data/error/Route2")
.log(LoggingLevel.ERROR, "I always get here")
.end()
.routePolicy(getRoutingPolicy())
.to("direct:RouteWhichWillThrowException")
.to("file:data/out/Route2");
However while this works in terms of reuse this kind of sucks if your implementation will always be the same except the inputURI, outputURI, and the errorURI. I would recommend making a template route and then making new instances of it.
class MyCustomRouteBuilder extends RouteBuilder {
private String myRouteId;
private String inputURI;
private String outputURI;
@Override
public void configure() {
from(inputURI).routeId(myRouteId)
.onException(SomeValidationException.class)
.handled(true)
.useOriginalMessage()
.to(errorURI)
.log(LoggingLevel.ERROR, "I always get here")
.end()
.routePolicy(getRoutingPolicy())
.to("direct:RouteWhichWillThrowException")
.to(outputURI);
}
}
//different route builder configure
MyCustomRouteBuilder route1 = new MyCustomRouteBuilder();
route1.setRouteId("");
route1.setInputURI("");
route1.setOutputURI("");
route1.setErrorURI("");
context.addRoutes(route1);
Upvotes: 1