问题
I'm using the camel-sftp component to upload a file to an SFTP server. The code is simple:
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();
However, camel has problems finding the sftp component. Activating the debug logs in Camel it complains:
| 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
Any ideas why Camel is behaving this way? In fact, running this code in a standalone application (a Java class with a main
method) works correctly.
And I can see:
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
Inside of Karaf, though, only the first line appears, for some reason or other it does not find META-INF/services/org/apache/camel/component/sftp
and as a result the sftp component is not found.
回答1:
If you run Camel inside OSGi, you should use the OSGi CamelContext from camel-core-osgi. And then there is a few more steps to setup this for OSGi.
Though its often easier to use a OSGi blueprint application and bootstrap Camel in the blueprint xml file, which does this correctly.
But for Java code its some manual process. In the upcoming Camel 2.15 release there is a new camel-scr component for working with OSGi and SCR (Declarative Services) which makes it easier to do java code with Camel in OSGi using camel-scr.
I would suggest to check its current source code for inspiration how you can setup Camel in OSGi from Java code
https://github.com/apache/camel/tree/master/components/camel-scr
来源:https://stackoverflow.com/questions/28295850/camel-not-finding-sftp-component-in-registry-and-shutting-down