300.9.5.6. 使用自定义 CamelContextLifecycle

在下面的代码中,我们使用 beforeStartafterStop 将自定义 bean 列在 Simple Registry 中,并在我们停止时进行清理。

然后,我们需要使用名称"CamelContextLifecycle"在 web.xml 文件中注册本课程。该值必须是 FQN,它引用了实施 org.apache.camel.component.servletlistener.CamelContext Life 周期接口的类。

  <context-param>
    <param-name>CamelContextLifecycle</param-name>
    <param-value>org.apache.camel.component.servletlistener.MyLifecycle</param-value>
  </context-param>

我们使用名称"my Bean "列出 HelloBean Bean,我们在 Camel 路由中引用此 Bean,如下所示:

public class MyBeanRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("seda:foo").routeId("foo")
            .to("bean:myBean")
            .to("mock:foo");
    }
}

重要信息: 如果您使用 org.apache.camel.component.servletlistener.JndiCamelServletContextListener,那么 CamelContext Life 周期也必须使用 JndiRegistry。如果 servlet 为 org.apache.camel.component.servletlistener.SimpleCamelServletContextListener,那么 CamelContextLifecycle 必须使用 SimpleRegistry