apache camel之路由

本文介绍了Apache Camel中如何使用Java DSL定义路由规则,包括基本的路由配置、使用过滤器(Filter)进行条件判断、使用选择器(Choice)进行多条件分支处理、自定义处理器(Processor)的实现方式以及拦截器(Interceptor)的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

路由:

Camel支持用java dsl定义路由规则,使用RouteBuilder。

示例:

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));
 
        from("direct:a").to("direct:b");
    }
};

 从上例可以看出camel使用URI来串起endpoint

URI字符格式:

格式化endpoint的URI

 

from("direct:start").toF("file://%s?fileName=%s", path, name);
 
fromF("file://%s?include=%s", path, pattern).toF("mock:%s", result);

Filters

You can combine simple routes with filters which can be arbitrary Predicate implementations.

RouteBuilder builder = new RouteBuilder() {
     public void configure() {
         errorHandler(deadLetterChannel( "mock:error" ));
 
         from( "direct:a" )
             .filter(header( "foo" ).isEqualTo( "bar" ))
                 .to( "direct:b" );
     }
};

Choices

With a choice you provide a list of predicates and outcomes along with an optional default otherwise clause which is invoked if none of the conditions are met.

RouteBuilder builder = new RouteBuilder() {
     public void configure() {
         errorHandler(deadLetterChannel( "mock:error" ));
 
         from( "direct:a" )
             .choice()
                 .when(header( "foo" ).isEqualTo( "bar" ))
                     .to( "direct:b" )
                 .when(header( "foo" ).isEqualTo( "cheese" ))
                     .to( "direct:c" )
                 .otherwise()
                     .to( "direct:d" );
     }
};
Using a custom processor

Here is an example of using a custom Processor

myProcessor = new Processor() {
     public void process(Exchange exchange) {
         log.debug( "Called with exchange: " + exchange);
     }
};
 
RouteBuilder builder = new RouteBuilder() {
     public void configure() {
         errorHandler(deadLetterChannel( "mock:error" ));
 
         from( "direct:a" )
             .process(myProcessor);
     }
};

You can mix and match custom processors with filters and choices.

RouteBuilder builder = new RouteBuilder() {
     public void configure() {
         errorHandler(deadLetterChannel( "mock:error" ));
 
         from( "direct:a" )
             .filter(header( "foo" ).isEqualTo( "bar" ))
                 .process(myProcessor);
     }
};
Interceptors

Here is an example of adding a few custom InterceptorProcessor objects to a processing pipeline:

RouteBuilder builder = new RouteBuilder() {
     public void configure() {
         errorHandler(deadLetterChannel( "mock:error" ));
 
         from( "direct:a" )
             .filter(header( "foo" ).isEqualTo( 123 ))
                 .to( "direct:b" );
     }
};

When you start defining and interceptor stack with intercept(), you must follow up with the subsequent .target() so that the target of the interceptor stack is properly registered.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值