【问题标题】:Adding Apache Camel custom component/endpoint in a Spring application在 Spring 应用程序中添加 Apache Camel 自定义组件/端点
【发布时间】:2021-03-24 17:37:52
【问题描述】:

我正在尝试在 Spring Boot 应用程序中实现自定义端点。

目标是使用路由:from("...").process("...").to("my:...");

现在,我有 3 个类:一个 DefaultConsumer、一个 DefaultEndpoint、一个 DefaultComponent:

package com.my.endpoint;

import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.support.DefaultEndpoint;

public class MyEndpoint extends DefaultEndpoint {

    public MyEndpoint(String uri, MyComponent myComponent) {

    }
    ...
}

package com.my.endpoint;

import org.apache.camel.Endpoint;
import org.apache.camel.Processor;
import org.apache.camel.support.DefaultConsumer;

public class MyConsumer extends DefaultConsumer {

    public MyConsumer(Endpoint endpoint, Processor processor) {

        super(endpoint, processor);
    }
}

package com.my.endpoint;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;

import java.util.Map;

@Component("my")
public class MyComponent extends DefaultComponent {

    public MyComponent(CamelContext camelContext) {

        super(camelContext);
    }
    ...
}

现在:我该如何注册?

在 Spring 配置类中,我有:

  @Override
  public void configure() throws Exception {

        camelContext.addComponent("my", new MyComponent(camelContext));

但不工作:

    Caused by: org.apache.camel.NoSuchEndpointException: No endpoint could be found for: my, please check your classpath contains the needed Camel component jar.

所以,我在 services/org/apache/camel/component/my 中添加了 META-INF 文件:

class=com.my.endpoint.MyComponent

但是这也行不通。

没有关于如何实现这一点的完整教程。

有什么帮助吗?

注意:我正在尝试实现一个端点,因为我需要使用我的数据类型来集成我的系统。我尝试使用 Transformer 但失败了:Set a custom DataType in Apache Camel Processor

之前,我尝试过使用数据类型转换器,但因此失败(标记为重复,因为人们懒得真正理解问题):Enforce type conversion on Rest consumer in Apache Camel

我已完整阅读“Apache Camel In Action,第二版”,但目前我无法继续我的项目,因为?

【问题讨论】:

  • 您是否尝试过使用 UriEndpointComponent 而不是 DefaultComponent?
  • @StepanShcherbakov 没有这样的类:已弃用。使用默认组件
  • 对不起,我没注意到
  • 我不太确定,但您可能应该在组件类中添加 @UriEndpoint 注释并为 uri 参数添加 @UriParam
  • 是的,@StepanShcherbakov。使用 UriEndpoint 注册工作。我在想是可选的,仅用于文档支持,但不是。谢谢。

标签: java spring spring-boot apache-camel


【解决方案1】:

这是因为自定义组件必须使用@UriEndpoint注解。

【讨论】:

    【解决方案2】:

    解决此问题的另一种方法:通过构造函数设置 EndpointUri 或在 MyEndpoint 中实现 createEndpointUri()。

    最简单的方法可能是将构造函数更改为:

    public MyEndpoint(String uri, MyComponent myComponent) {
     super(uri, myComponent);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多