我有两条独立的路线。两条路线基本相同。不同的是源文件夹和目标文件夹。如果出现错误,两个路由都会抛出相同的异常。我抓住了一个onException块。 在这里我想根据实际路线将文件写入文件夹。所以我想建立一个choiche().... when(...)基于routeId。问题是,如何让routeId将它用于何时。 以下显示了我认为它可以工作的一些代码。但事实并非如此。也许有人有理想。
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");
答案 0 :(得分:1)
我认为您的用例实际上最好使用路由错误处理程序,如下所示:
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");
然而,虽然这在重用方面起作用,但如果除了inputURI,outputURI和errorURI之外你的实现总是相同的话,这种方法很糟糕。我建议制作模板路线,然后制作新的实例。
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);