使用 Java 在 apache camel 中为自定义组件配置消费者

Configuring consumer for custom component in apache camel using Java

假设我有一个 Jetty 组件 comp1 和自定义组件 comp2,其中 comp1 产生一个交换而 comp2 使用它。 如何在 comp2 的消费者中获取 Jetty 组件的交换。

到目前为止,我观察到我们可以在消费者的 poll() 方法中获取它,如下所示 - SomeEndpoint 端点 = camelContext.getEndpoint("someURI", SomeEndpoint.class);

但是在 someURI 和 someEndpoint.class 上配置什么?

如果我提到 someURI = "jetty:..",那么我的消费者将不会使用来自任何其他端点的消息,那么如何为通用配置它?

Spring DSL 允许你做这样的事情: 在消费者和生产者之间,您可以添加自定义生产者

<route>
  <from uri="component1 uri"/>
   your other process code
  <to uri="component2 uri">
</route>

您必须先使用

为您的自定义组件创建骨架代码
mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-component  
-DarchetypeVersion=2.14.1 -DarchetypeRepository=https://repository.apache.org/content/groups/snapshots-group  
-DgroupId=org.apache.camel.component -DartifactId={YourArtifactId} 

您的组件前缀文件位于此位置

src/main/resources/META-INF/services/org/apache/camel/component/

此文件的名称是您的组件前缀。您在第一步生成项目时提供此名称。

假设它的名字是comp2。现在您只需要以这种方式配置您的路由:

from("jetty:abc").to("comp2:xyz");   

您的组件的 jar 必须作为配置 Camel Route 的应用程序的依赖项提供。

如果需要,您需要实施组件 Class、端点 class、消费者和生产者 class。