Camel 未在注册表中找到 "sftp" 组件并正在关闭

Camel not finding "sftp" component in registry and shutting down

我正在使用 camel-sftp 组件将文件上传到 SFTP 服务器。 代码很简单:

File source = new File(path);
final String sftpUri = "sftp://" + userId  + "@" + serverAddress + "/" + remoteDirectory+"?password="+pwd;
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
     @Override
     public void configure() throws Exception {
        from("file:/" + path).to(sftpUri);
     }
});
context.start();
Thread.sleep(10000);
context.stop();

但是,camel 在查找 sftp 组件时遇到问题。在 Camel 中激活调试日志它抱怨:

| 62 - org.apache.camel.camel-core - 2.13.2 | Using ComponentResolver: org.apache.camel.impl.DefaultComponentResolver@29668a43 to resolve component with name: sftp
| 62 - org.apache.camel.camel-core - 2.13.2 | Found component: sftp in registry: null
| 62 - org.apache.camel.camel-core - 2.13.2 | Apache Camel 2.13.2 (CamelContext: camel-16) is shutting down

知道为什么 Camel 会这样吗?事实上,运行 独立应用程序中的这段代码(Java class 和 main 方法)可以正常工作。 我可以看到:

11:22:13.237 [main] DEBUG o.a.c.impl.DefaultComponentResolver - Found component: sftp in registry: null
11:22:13.239 [main] DEBUG o.a.c.impl.DefaultComponentResolver - Found component: sftp via type: org.apache.camel.component.file.remote.SftpComponent via: META-INF/services/org/apache/camel/component/sftp

但是在 Karaf 内部,只有第一行出现,出于某种原因它找不到 META-INF/services/org/apache/camel/component/sftp,因此找不到 sftp 组件。

如果您 运行 OSGi 中的 Camel,您应该使用来自 camel-core-osgi 的 OSGi CamelContext。然后还有几个步骤来为 OSGi 设置它。

尽管使用 OSGi 蓝图应用程序和 bootstrap 蓝图 xml 文件中的 Camel 通常更容易,但它可以正确地做到这一点。

但是对于 Java 代码是一些手动过程。在即将发布的 Camel 2.15 版本中,有一个新的 camel-scr 组件用于与 OSGi 和 SCR(声明式服务)一起使用,这使得使用 camel-scr 在 OSGi 中使用 Camel 编写 java 代码变得更加容易。

我建议查看其当前源代码以获取灵感,了解如何从 Java 代码

在 OSGi 中设置 Camel

https://github.com/apache/camel/tree/master/components/camel-scr