搜索

38.3. 自动诊断和配置

download PDF

38.3.1. 设置自动诊断

概述

自动发现是一种可让您动态地将组件添加到 Apache Camel 应用程序的机制。组件 URI 前缀用作按需加载组件的密钥。例如,如果 Apache Camel 遇到端点 URI,activemq://MyQName 并且 ActiveMQ 端点尚未加载,则 Apache Camel 会搜索 activemq 前缀标识的组件,并动态加载该组件。

组件类可用性

在配置自动发现前,您必须确保您的自定义组件类可从您当前的 classpath 访问。通常,您可以将自定义组件类捆绑到 JAR 文件中,并将 JAR 文件添加到 classpath 中。

配置自动发现

要启用组件的自动发现,请创建一个名为组件前缀的 Java 属性文件,组件前缀,并将该文件存储在以下位置:

/META-INF/services/org/apache/camel/component/component-prefix

component-prefix 属性文件必须包含以下属性设置:

class=component-class-name

其中 component-class-name 是自定义组件类的完全限定名称。您还可以在此文件中定义额外的系统属性设置。

Example

例如,您可以通过创建以下 Java 属性文件来为 Apache Camel FTP 组件启用自动发现:

/META-INF/services/org/apache/camel/component/ftp

它包含以下 Java 属性设置:

class=org.apache.camel.component.file.remote.RemoteFileComponent
注意

FTP 组件的 Java 属性文件已在 JAR 文件 camel-ftp-Version.jar 中定义。

38.3.2. 配置组件

概述

您可以通过在 Apache Camel Spring 配置文件 META-INF/spring/camel-context.xml 中配置组件来添加组件。要查找组件,组件的 URI 前缀与 Spring 配置中 bean 元素的 ID 属性匹配。如果组件前缀与 bean 元素 ID 匹配,Apache Camel 会实例化引用类并注入 Spring 配置中指定的属性。

注意

此机制优先于自动发现。如果 CamelContext 找到带有 requisite ID 的 Spring bean,它不会尝试使用自动发现来查找组件。

在组件类中定义 bean 属性

如果要注入组件类的任何属性,请将其定义为 bean 属性。例如:

public class CustomComponent extends
  DefaultComponent<CustomExchange> {
    ...
    PropType getProperty() { ... }
    void setProperty(PropType v) { ... }
}

getProperty() 方法和 setProperty() 方法访问 属性 的值。

在 Spring 中配置组件

要在 Spring 中配置组件,请编辑配置文件 META-INF/spring/camel-context.xml,如 例 38.1 “在 Spring 中配置组件” 所示。

例 38.1. 在 Spring 中配置组件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <package>RouteBuilderPackage</package>
  </camelContext>

  <bean id="component-prefix" class="component-class-name">
    <property name="property" value="propertyValue"/>
   </bean>
</beans>

带有 ID component-prefixbean 元素配置 component-class-name 组件。您可以使用 property 元素将属性注入组件实例中。例如,上例中的 property 元素将通过在组件上调用 setProperty() 将值 attribute Value 注入到 property 属性中。

例子

例 38.2 “JMS 组件 Spring 配置” 演示了如何通过定义 ID 等于 jms 的 bean 元素来配置 Apache Camel 的 JMS 组件。这些设置添加到 Spring 配置文件 camel-context.xml 中。

例 38.2. JMS 组件 Spring 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <package>org.apache.camel.example.spring</package> 1
  </camelContext>

  <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> 2
    <property name="connectionFactory"3
       <bean class="org.apache.activemq.ActiveMQConnectionFactory">
         <property name="brokerURL"
                   value="vm://localhost?broker.persistent=false&amp;broker.useJmx=false"/> 4
       </bean>
    </property>
  </bean>
</beans>
1
CamelContext 会自动实例化它在指定的 Java 软件包 org.apache.camel.example.spring 中找到的 RouteBuilder 类。
2
带有 ID 为 jms 的 bean 元素配置 JMS 组件。bean ID 与组件的 URI 前缀对应。例如,如果路由指定了 URI 为 jms://MyQName 的端点,则 Apache Camel 会使用 jms bean 元素中的设置自动加载 JMS 组件。
3
JMS 只是消息传递服务的打包程序。您必须通过在 JmsComponent 类上设置 connectionFactory 属性来指定消息传递系统的 concrete 实现。
4
在本例中,JMS 消息传递服务的具体实施是 Apache ActiveMQ。brokerURL 属性初始化与 ActiveMQ 代理实例的连接,其消息代理嵌入到本地 Java 虚拟机(JVM)中。如果 JVM 中尚未存在代理,ActiveMQ 将使用选项 broker.persistent=false (代理不持久保留消息)和 broker.useJmx=false (代理不打开 JMX 端口)对其进行实例化。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.