Apache camel 将多个camel组件组合成一个自定义组件

Apache camel 将多个camel组件组合成一个自定义组件,apache-camel,Apache Camel,我想创建一个新的camel组件MyComponent,它是不同组件的组合: 例如,不是指定多个端点,而是: from("file:c:/in")...to... from("ftp:...")...to... 我想要一个能够检测正确端点的MyComponent: from("myComponent:..")...to... 因此,无论文件是否来自(“文件”),来自(“sftp”),来自(“流”)等,它都可以作为适配器使用 作为一个适配器eater,我们有一个文件作为条目或ftp 如何实现这

我想创建一个新的camel组件
MyComponent
,它是不同组件的组合:

例如,不是指定多个端点,而是:

from("file:c:/in")...to...

from("ftp:...")...to...
我想要一个能够检测正确端点的
MyComponent

from("myComponent:..")...to...
因此,无论文件是否来自(“文件”),
来自(“sftp”)
来自(“流”)
等,它都可以作为适配器使用

作为一个适配器eater,我们有一个文件作为条目或ftp


如何实现这一点?

您真的需要驼峰组件,还是可以使用自定义管线生成器实现这一点?下面是一个简单的示例,展示了总体思路:

自定义管线生成器类而不是组件:

import org.apache.camel.Endpoint;
导入org.apache.camel.builder.RouteBuilder;
导入org.apache.camel.component.file.FileEndpoint;
导入org.apache.camel.component.file.remote.FtpConfiguration;
导入org.apache.camel.component.file.remote.FtpEndpoint;
类MyFileReader扩展了RouteBuilder{
私有字符串名称;
私有字符串目录;
私有字符串掩码;
私有字符串ftpHost;
私人国际金融支持;
私有字符串ftpUser;
私有字符串ftpPass;
私有布尔ftpMode;
公共MyFileReader(字符串名称、字符串目录、字符串掩码){
this.name=名称;
this.dir=dir;
这个面具=面具;
this.ftpMode=false;
}
公共MyFileReader(字符串名称、字符串目录、字符串掩码、字符串ftpHost、int-ftpPort、字符串ftpUser、字符串ftpPass){
this.name=名称;
this.dir=dir;
这个面具=面具;
this.ftpHost=ftpHost;
this.ftpPort=ftpPort;
this.ftpUser=ftpUser;
this.ftpPass=ftpPass;
this.ftpMode=true;
}
@凌驾
public void configure()引发异常{
from(initFromEndpoint()).to(“direct:+name+”_out”);
}
私有端点initFromEndpoint(){
if(ftpMode){
FtpEndpoint端点=新的FtpEndpoint();
FtpConfiguration configuration=endpoint.getConfiguration();
配置.setHost(ftpHost);
配置。设置端口(ftpPort);
设置用户名(ftpUser);
设置密码(ftpPass);
setDirectory(dir);
setInclude(掩码);
返回端点;
}否则{
FileEndpoint=新的FileEndpoint();
endpoint.getConfiguration().setDirectory(dir);
setInclude(掩码);
返回端点;
}
}
}
SpringXML的使用示例:


您真的需要驼峰组件,还是可以通过自定义管线生成器来实现?下面是一个简单的示例,展示了总体思路:

自定义管线生成器类而不是组件:

import org.apache.camel.Endpoint;
导入org.apache.camel.builder.RouteBuilder;
导入org.apache.camel.component.file.FileEndpoint;
导入org.apache.camel.component.file.remote.FtpConfiguration;
导入org.apache.camel.component.file.remote.FtpEndpoint;
类MyFileReader扩展了RouteBuilder{
私有字符串名称;
私有字符串目录;
私有字符串掩码;
私有字符串ftpHost;
私人国际金融支持;
私有字符串ftpUser;
私有字符串ftpPass;
私有布尔ftpMode;
公共MyFileReader(字符串名称、字符串目录、字符串掩码){
this.name=名称;
this.dir=dir;
这个面具=面具;
this.ftpMode=false;
}
公共MyFileReader(字符串名称、字符串目录、字符串掩码、字符串ftpHost、int-ftpPort、字符串ftpUser、字符串ftpPass){
this.name=名称;
this.dir=dir;
这个面具=面具;
this.ftpHost=ftpHost;
this.ftpPort=ftpPort;
this.ftpUser=ftpUser;
this.ftpPass=ftpPass;
this.ftpMode=true;
}
@凌驾
public void configure()引发异常{
from(initFromEndpoint()).to(“direct:+name+”_out”);
}
私有端点initFromEndpoint(){
if(ftpMode){
FtpEndpoint端点=新的FtpEndpoint();
FtpConfiguration configuration=endpoint.getConfiguration();
配置.setHost(ftpHost);
配置。设置端口(ftpPort);
设置用户名(ftpUser);
设置密码(ftpPass);
setDirectory(dir);
setInclude(掩码);
返回端点;
}否则{
FileEndpoint=新的FileEndpoint();
endpoint.getConfiguration().setDirectory(dir);
setInclude(掩码);
返回端点;
}
}
}
SpringXML的使用示例:



我编辑了你的帖子(它在队列中)以澄清一点,但我不知道你在这一行中的意思:“作为适配器阅读器,我们有一个文件作为入口或ftp…”你能试着理解一下吗?@gkubed我的意思是我不想使用不同的入口点作为第一个示例:from(“文件”)from(“sftp”)from(“流”)我想让mu组件完成所有工作,并在我从(“mycomponent”)开始时检测到正确的入口点。它可以是一个文件或流或任何东西。对我来说,这看起来像YAGNI。我编辑了你的帖子(它在队列中)以澄清一点,但我不知道你在这行中的意思:“并且作为适配器生成器工作,我们有一个文件作为ent