2.6.3. 端点绑定
什么是绑定?
在 Apache Camel 中,绑定 是一种将端点嵌套在 contract contract-sHistoryLimit 例如,通过应用数据格式、内容增强器或验证步骤的方法。条件或转换应用于传入消息的消息,并应用于消息的补充条件或转换。
DataFormatBinding
对于您要定义 marshals 和 unmarshals 特定数据格式的绑定(请参阅 第 2.6.2 节 “marshalling 和 Unmarshalling”,请参阅 在这种情况下,您需要进行创建绑定的所有操作都是创建
DataFormatBinding
实例,在 constructor 中传递对相关数据格式的引用。
例如: 例 2.2 “JAXB Binding” 中的 XML DSL 片断会显示一个绑定(带有 ID、jaxb
)的绑定(使用 ID, jaxb ),当它与 Apache Camel 端点关联时,能够放宽和取消汇总 JAXB 数据格式:
例 2.2. JAXB Binding
<beans ... >
...
<bean id="jaxb" class="org.apache.camel.processor.binding.DataFormatBinding">
<constructor-arg ref="jaxbformat"/>
</bean>
<bean id="jaxbformat" class="org.apache.camel.model.dataformat.JaxbDataFormat">
<property name="prettyPrint" value="true"/>
<property name="contextPath" value="org.apache.camel.example"/>
</bean>
</beans>
将绑定与端点关联
以下的替代方法可用于将绑定与端点关联:
绑定 URI
要将绑定与端点关联,您可以将端点 URI 前缀为 binding:NameOfBinding
,其中 NameOfBinding
是绑定的 bean ID(例如,在 Spring XML 中创建绑定 bean 的 ID)。
例如,以下示例演示了如何将 ActiveMQ 端点与 例 2.2 “JAXB Binding” 中定义的 JAXB 绑定关联。
<beans ...> ... <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="binding:jaxb:activemq:orderQueue"/> <to uri="binding:jaxb:activemq:otherQueue"/> </route> </camelContext> ... </beans>
BindingComponent
您可以做出隐式关联绑定而不是将绑定与端点关联,而不必在 URI 中显示绑定。对于没有隐式绑定的现有端点,实现此目的的最简单方法是使用 BindingComponent
类包装端点。
例如,要将 jaxb
绑定与 activemq
端点关联,您可以定义新的 BindingComponent
实例,如下所示:
<beans ... >
...
<bean id="jaxbmq" class="org.apache.camel.component.binding.BindingComponent">
<constructor-arg ref="jaxb"/>
<constructor-arg value="activemq:foo."/>
</bean>
<bean id="jaxb" class="org.apache.camel.processor.binding.DataFormatBinding">
<constructor-arg ref="jaxbformat"/>
</bean>
<bean id="jaxbformat" class="org.apache.camel.model.dataformat.JaxbDataFormat">
<property name="prettyPrint" value="true"/>
<property name="contextPath" value="org.apache.camel.example"/>
</bean>
</beans>
其中(可选) jaxbmq
的第二个构造器参数定义了 URI 前缀。现在,您可以使用 jaxbmq
ID 作为端点 URI 的方案。例如,您可以使用这个绑定组件定义以下路由:
<beans ...> ... <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="jaxbmq:firstQueue"/> <to uri="jaxbmq:otherQueue"/> </route> </camelContext> ... </beans>
前面的路由等同于以下路由,它使用绑定 URI 方法:
<beans ...> ... <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="binding:jaxb:activemq:foo.firstQueue"/> <to uri="binding:jaxb:activemq:foo.otherQueue"/> </route> </camelContext> ... </beans>
对于实施自定义 Apache Camel 组件的开发人员,可以通过实施从 org.apache.camel.spi.HasBinding
接口继承的端点类来实现这一点。
BindingComponent constructors
BindingComponent
类支持以下 constructors:
public BindingComponent()
- 无参数形式。使用属性注入配置绑定组件实例。
public BindingComponent(Binding binding)
-
将此绑定组件与指定的
Binding
对象关联,并且绑定
。 public BindingComponent(Binding binding, String uriPrefix)
-
将此绑定组件与指定的
Binding
对象、绑定和
URI 前缀关联,uriPrefix
。这是最常用的构造器。 public BindingComponent(Binding binding, String uriPrefix, String uriPostfix)
-
此构造器支持额外的 URI post-fix,
uri
postfix , 参数,该参数会自动附加到使用此绑定组件定义的任何 URI。
实现自定义绑定
除了 DataFormatBinding
之外,可用于汇总和解包数据格式,您可以实施自己的自定义绑定。定义自定义绑定,如下所示:
-
实施
org.apache.camel.Processor
类,对传入到消费者端点的消息执行转换(从
元素推断)。 -
实施补充
org.apache.camel.Processor
类,对从制作者端点(位于元素中)发出的消息执行反向转换。 -
实施
org.apache.camel.spi.Binding
接口,它充当处理器实例的工厂。
绑定接口
例 2.3 “org.apache.camel.spi.Binding 接口” 显示 org.apache.camel.spi.Binding
接口的定义,您必须实现它们来定义自定义绑定。
例 2.3. org.apache.camel.spi.Binding 接口
// Java package org.apache.camel.spi; import org.apache.camel.Processor; /** * Represents a <a href="http://camel.apache.org/binding.html">Binding</a> or contract * which can be applied to an Endpoint; such as ensuring that a particular * <a href="http://camel.apache.org/data-format.html">Data Format</a> is used on messages in and out of an endpoint. */ public interface Binding { /** * Returns a new {@link Processor} which is used by a producer on an endpoint to implement * the producer side binding before the message is sent to the underlying endpoint. */ Processor createProduceProcessor(); /** * Returns a new {@link Processor} which is used by a consumer on an endpoint to process the * message with the binding before its passed to the endpoint consumer producer. */ Processor createConsumeProcessor(); }
何时使用绑定
当您需要应用相同的转换到许多不同类型的端点时,绑定很有用。