301.9. ServletListener 组件


从 Camel 2.11 开始提供

此组件用于在 web 应用程序中引导 Camel 应用程序。例如,之前人员必须找到自己引导 Camel 的方式,或依赖 Spring 等第三方框架来实现它。

注意

边栏 此组件支持 Servlet 2.x 以后,这意味着它也在较旧的 Web 容器中工作;这是此组件的目标。虽然 Servlet 2.x 需要使用 web.xml 文件作为配置。对于 Servlet 3.x 容器,您可以使用注释驱动的配置来利用 @WebListener 提升 Camel,并实施您自己的类,您可以在其中 boostrap Camel。这样做仍然给最终用户带来相应的挑战,从而使最终用户能够轻松配置 Camel,这可让您免费使用旧的 school web.xml 文件。

Maven 用户需要将以下依赖项添加到此组件的 pom.xml 中:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-servletlistener</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

301.9.1. 使用

您需要选择抽象类 org.apache.camel.component.servletlistener.CamelServletContextListener 的一个实现。

  • JndiCamelServletContextListener,它使用 JndiRegistry 将 JNDI 用于其 registry。
  • SimpleCamelServletContextListener,它使用 SimpleRegistryjava.util.Map 用作其 registry。

要使用此功能,您需要在 WEB-INF/web.xml 文件中配置 org.apache.camel.component.servletlistener.CamelServletContextListener,如下所示:

301.9.2. 选项

org.apache.camel.component.servletlistener.CamelServletContextListener 支持以下选项,可在 web.xml 文件中配置为 context-param。

Expand
选项类型描述

propertyPlaceholder.XXX

 

要在 Camel 中配置属性占位符。您应该为选项添加 "propertyPlaceholder.",例如配置位置,使用 propertyPlaceholder.location 作为 name。您可以配置 Properties 组件中的所有选项。

jmx.XXX

 

配置 JMX。您应该为选项添加 "jmx." 前缀,例如禁用 JMX,使用 jmx.disabled 作为名称。您可以配置 org.apache.camel.spi.ManagementAgent 中的所有选项。以及 JMX 页面提到的选项。

name

字符串

配置 CamelContext 的名称。

messageHistory

布尔值

Camel 2.12.2: 是否启用或禁用消息历史(默认启用)。

streamCache

布尔值

是否启用流缓存。

trace

布尔值

是否启用 Tracer。

delayer

Long

为 Delay Interceptor 设置延迟值。

handleFault

布尔值

是否启用处理错误。

errorHandlerRef

字符串

指的是要使用的上下文范围 Error Handler。

autoStartup

布尔值

启动 Camel 时是否启动所有路由。

useMDCLogging

布尔值

是否使用 MDC 日志记录。

useBreadcrumb

布尔值

是否使用面包屑导航栏.

managementNamePattern

字符串

要为 JMX MBeans 设置自定义命名模式。

threadNamePattern

字符串

要为线程设置自定义命名模式。

properties.XXX

 

要在 CamelContext.getProperties 上设置自定义属性。这很少被使用。

routebuilder.XXX

 

配置要使用的路由。详情请查看以下信息。

CamelContextLifecycle

 

指的是 org.apache.camel.component.servletlistener.CamelContextLifecycle 的 FQN 类名称。这允许在 CamelContext 启动或停止之前和之后执行自定义代码。详情请查看以下信息。

XXX

 

要在 CamelContext 上设置任何选项。

301.9.3. 例子

请参阅 Servlet Tomcat No Spring Example

301.9.4. 访问创建的 CamelContext

从 Camel 2.14/2.13.3/2.12.5 提供

创建的 CamelContext 作为键为 "CamelContext" 的属性存储在 ServletContext 上。如果您可以拥有 ServletContext,您可以获得 CamelContext,如下所示:

ServletContext sc = ...
CamelContext camel = (CamelContext) sc.getAttribute("CamelContext");
Copy to Clipboard Toggle word wrap

301.9.5. 配置路由

您需要配置在 web.xml 文件中使用哪些路由。您可以通过多种方法执行此操作,但所有参数都必须以"routeBuilder"作为前缀。

301.9.5.1. 使用 RouteBuilder 类

默认情况下,Camel 将假设 param-value 是 Camel RouteBuilder 类的 FQN 类,如下所示:

  <context-param>
    <param-name>routeBuilder-MyRoute</param-name>
    <param-value>org.apache.camel.component.servletlistener.MyRoute</param-value>
  </context-param>
Copy to Clipboard Toggle word wrap

您可以在相同的 param-value 中指定多个类,如下所示:

  <context-param>
    <param-name>routeBuilder-routes</param-name>
    <!-- we can define multiple values separated by comma -->
    <param-value>
      org.apache.camel.component.servletlistener.MyRoute,
      org.apache.camel.component.servletlistener.routes.BarRouteBuilder
    </param-value>
  </context-param>
Copy to Clipboard Toggle word wrap

参数的名称在运行时没有意义。它只需要是唯一的,并以"routeBuilder"开头。在上例中,我们有 "routeBuilder-routes"。但是,您也可以将其命名为"routeBuilder.foo"。

301.9.5.2. 使用软件包扫描

您还可以告知 Camel 使用软件包扫描,这意味着它将查看给定软件包中所有 RouteBuilder 类型类,并自动将它们添加为 Camel 路由。要做到这一点,您需要使用 "packagescan:" 为值添加前缀,如下所示:

  <context-param>
    <param-name>routeBuilder-MyRoute</param-name>
    <!-- define the routes using package scanning by prefixing with packagescan: -->
    <param-value>packagescan:org.apache.camel.component.servletlistener.routes</param-value>
  </context-param>
Copy to Clipboard Toggle word wrap

301.9.5.3. 使用 XML 文件

您还可以使用 XML DSL 定义 Camel 路由,但我们不使用 Spring 或 Blueprint XML 文件只能包含 Camel 路由。

在 web.xml 中,您可以引用来自 "classpath", "file" 或 "http" url 的 XML 文件,如下所示:

  <context-param>
    <param-name>routeBuilder-MyRoute</param-name>
    <param-value>classpath:routes/myRoutes.xml</param-value>
  </context-param>
Copy to Clipboard Toggle word wrap

XML 文件为:

routes/myRoutes.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- the xmlns="http://camel.apache.org/schema/spring" is needed -->
<routes xmlns="http://camel.apache.org/schema/spring">

  <route id="foo">
    <from uri="direct:foo"/>
    <to uri="mock:foo"/>
  </route>

  <route id="bar">
    <from uri="direct:bar"/>
    <to uri="mock:bar"/>
  </route>

</routes>
Copy to Clipboard Toggle word wrap

请注意,在 XML 文件中,root 标签为 <routes>,它必须使用命名空间 "http://camel.apache.org/schema/spring"。此命名空间的名称中包含 spring,但由于历史原因,Spring 是第一个且唯一的 XML DSL。在运行时不需要 Spring JAR。在 Camel 3.0 中,命名空间可以重命名为通用名称。

301.9.5.4. 配置正确的占位符

以下是用于设置属性占位符从 classpath 加载 myproperties.properties 的 web.xml 配置的片段

  <!-- setup property placeholder to load properties from classpath -->
  <!-- we do this by setting the param-name with propertyPlaceholder. as prefix and then any options such as location, cache etc -->
  <context-param>
    <param-name>propertyPlaceholder.location</param-name>
    <param-value>classpath:myproperties.properties</param-value>
  </context-param>
  <!-- for example to disable cache on properties component, you do -->
  <context-param>
    <param-name>propertyPlaceholder.cache</param-name>
    <param-value>false</param-value>
  </context-param>
Copy to Clipboard Toggle word wrap

301.9.5.5. 配置 JMX

以下是用于配置 JMX 的 web.xml 配置片段,如禁用 JMX。

  <!-- configure JMX by using names that is prefixed with jmx. -->
  <!-- in this example we disable JMX -->
  <context-param>
    <param-name>jmx.disabled</param-name>
    <param-value>true</param-value>
  </context-param>
Copy to Clipboard Toggle word wrap

JNDI 或 Simple as Camel Registry ^^^^^^^^^^^^^^^^^^^^

此组件使用 JNDI 或 Simple 作为 Registry。
这样,您可以在 JNDI 中查找 Bean和其他服务,也可绑定和取消绑定您自己的 Bean

这通过实施 org.apache.camel.component.servletlistener.CamelContextLifecycle 从 Java 代码完成。

301.9.5.6. 使用自定义 CamelContextLifecycle

在以下代码中,我们使用 StartafterStop 在 Simple Registry 中对自定义 bean 进行回调,并在停止时进行清理。

然后,我们需要使用参数名称 "CamelContextLifecycle" 在 web.xml 文件中注册此类。该值必须是 FQN,它引用实施 org.apache.camel.component.servletlistener.CamelContextLifecycle 接口的类。

  <context-param>
    <param-name>CamelContextLifecycle</param-name>
    <param-value>org.apache.camel.component.servletlistener.MyLifecycle</param-value>
  </context-param>
Copy to Clipboard Toggle word wrap

我们使用名称"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");
    }
}
Copy to Clipboard Toggle word wrap

重要: 如果您使用 org.apache.camel.component.servletlistener.JndiCamelServletContextListener,则 CamelContextLifecycle 必须使用 JndiRegistry。如果 servlet 是 org.apache.camel.component.servletlistener.SimpleCamelServletContextListener,则 CamelContextLifecycle 必须使用 SimpleRegistry

301.9.6. 另请参阅

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat