简介-Apache Camel生命周期及管理

在Camel Spring Boot中CamelAutoConfiguration注解来实现 Camel Context的加载
https://camel.apache.org/manual/route-controller.html

ROUTEPOLICY

CamelContext

运行时管理

Camel Comtext提供以下方法来实现Routes路由的运行时管理

  • build
  • init
  • start
  • stop
  • suspend
  • resume

Route路由管理

动态添加route、删除、修改route路由规则

  • java代码方式
InputStream is = new ByteArrayInputStream(routeXml.getBytes("UTF-8"));
RoutesDefinition xmlDefinition = camelContext.adapt(ModelCamelContext.class).loadRoutesDefinition(is);
camelContext.adapt(ModelCamelContext.class).addRouteDefinitions(xmlDefinition.getRoutes());
  • xml方式
RouteBuilder routeBuilder = new RouteBuilder() {
    @Override
    public void configure() throws Exception {
        from("seda:a")
            .filter(header("foo").isEqualTo("bar"))
            .to("seda:b");
    }
};

try {
    camelContext.addRoutes(routeBuilder);
} catch (Exception e) {
    e.printStackTrace();
}

全局策略管理

全局策略用于支持全局错误、异常等统一处理机制的扩充:

  • Error Handler
  • OnException
  • OnCompletion
  • Intercept

OnCompletion

Camel 提供 Unit of Work概念增强Exchange. 在exchange调用完成时The unit of work 提供同步的接口调用。回调接口定义在org.apache.camel.spi.Synchronization中,org.apache.camel.spi.SynchronizationRouteAware用于定义回调的消息定义。

public interface SynchronizationRouteAware extends Synchronization {
    void onBeforeRoute(Route route, Exchange exchange);

    void onAfterRoute(Route route, Exchange exchange);
}

OnCompletion EIP支持一下几个特性:

  • level: context or route (route level override global level)
  • triggered either always, only if completed with success, or only if failed
  • onWhen predicate to only trigger if matched
  • mode to define whether to run either before or after route consumer writes response back to callee (if its InOut) (default AfterConsumer)
  • parallelProcessing whether to run async or sync (use a thread pool or not) (default false)

    
    
    
    
        
        
    
    
    

当然也支持定义全局的onCompletion



    
    



    
    
    

当同时定义了全局的onCompletion和局部(route内)的onCompletion时,全局的onCompletion将不会起作用

The onCompletion supports running the completion task in either synchronous or asynchronous mode (using a thread pool) and also whether to run before or after the route consumer is done. The reason is to give more flexibility. For example to specify to run synchronous and before the route consumer is done, which allows to modify the exchange before the consumer writes back any response to the callee. You can use this to for example add customer headers, or send to a log to log the response message, etc.

可以使用异步线程池处理逻辑,保持异步线程处理


  
  1000
  OnComplete:${body}

在全局处理完成后,如果需要对返回的结构进行修改,可以使用modeBeforeConsumer。这个机制典型的应用场景是在做链路追踪时,比如对restful接口,返回后所有的返回值自动填充上全局的traceId。开发人员可以方便的使用返回的traceId排查日志,分析问题。


  
    Someone
  

UNITOFWORK API

org.apache.camel.Exchang中调用getUnitOfWork()方法,来获取org.apache.camel.spi.UnitOfWork对象

参考资料

  • https://camel.apache.org/manual/lifecycle.html
  • https://camel.apache.org/manual/registry.html
  • https://camel.apache.org/manual/route-controller.html
  • https://camel.apache.org/manual/route-configuration.html

你可能感兴趣的:(简介-Apache Camel生命周期及管理)