Apache Camel - 在每条路线之前执行流程

Apache Camel - Execute processes before each route

以下是我创建的路线 -

from("jetty:http://localhost:8181/abc").routeId("abc").choice()
            // Authenticate the request
            .when(authenticator).endChoice()
            // Authorize the request
            .when(authorizer).endChoice()
            // Validate the request
            .when(abcValidator).endChoice()
            .otherwise()
            .process(abcRequestProcessor).process(storeFeatureRequestDetails).process(featureRequestApproverUpdater).split(body()).process(abcApproverMailer).to("direct:toMail");

以上路由是有效的,但我不想为每条路由添加身份验证器和授权器步骤。有没有一种方法可以在每条路线之前将它们配置为 运行。

我尝试了以下 -

from("jetty:http://localhost:8181/*").process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            System.out.println("Triggered");
        }
    });

但它寻找完全匹配。

如果你不想把你的 AAA 放在每一个路由中,为什么不提取它们的功能并将它们放在单独的路由中并在你的路由中调用它们呢?示例代码:

from("jetty:http://localhost:8181/abc").routeId("abc").choice()
            // Authenticate the request
            .to("direct:authenticator")
            // Authorize the request
            .to("direct:authorizer")
            // Validate the request
            .to("direct:abcValidator")            
            .process(abcRequestProcessor).process(storeFeatureRequestDetails).process(featureRequestApproverUpdater).split(body()).process(abcApproverMailer).to("direct:toMail");

基本上,身份验证器现在位于单独的路由中。你用 "direct:authenticator" 来称呼它。授权和验证也是如此。

这样,如果您有其他路由需要使用此功能,您只需调用 AAA 路由即可。

你可以使用 Camel interceptor API...

// intercept all incoming routes and log it
interceptFrom().process(new Processor() {
    @Override
    public void process(Exchange exchange) throws Exception {
        System.out.println("Triggered");
    }
});