我想创建一个新的驼峰组件MyComponent
,它是不同组件的组合:
例如,不是指定多个端点:
from("file:c:/in")...to...
from("ftp:...")...to...
我想要一个可以检测到正确端点的MyComponent
:
from("myComponent:..")...to...
因此,无论文件来自from("file")
,from("sftp")
,from("stream")
等等,它都可以作为适配器使用...
作为适配器,我们有一个文件作为条目或ftp ..
如何实现这一目标?
答案 0 :(得分:4)
您真的需要驼峰组件,或者您可以通过自定义路由构建器实现这一目标吗?这里只是展示一般想法的原始示例:
自定义路径构建器类而不是组件:
import org.apache.camel.Endpoint;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.FileEndpoint;
import org.apache.camel.component.file.remote.FtpConfiguration;
import org.apache.camel.component.file.remote.FtpEndpoint;
class MyFileReader extends RouteBuilder {
private String name;
private String dir;
private String mask;
private String ftpHost;
private int ftpPort;
private String ftpUser;
private String ftpPass;
private boolean ftpMode;
public MyFileReader(String name, String dir, String mask) {
this.name = name;
this.dir = dir;
this.mask = mask;
this.ftpMode = false;
}
public MyFileReader(String name, String dir, String mask, String ftpHost, int ftpPort, String ftpUser, String ftpPass) {
this.name = name;
this.dir = dir;
this.mask = mask;
this.ftpHost = ftpHost;
this.ftpPort = ftpPort;
this.ftpUser = ftpUser;
this.ftpPass = ftpPass;
this.ftpMode = true;
}
@Override
public void configure() throws Exception {
from(initFromEndpoint()).to("direct:" +name + "_out");
}
private Endpoint initFromEndpoint() {
if (ftpMode) {
FtpEndpoint endpoint = new FtpEndpoint();
FtpConfiguration configuration = endpoint.getConfiguration();
configuration.setHost(ftpHost);
configuration.setPort(ftpPort);
configuration.setUsername(ftpUser);
configuration.setPassword(ftpPass);
configuration.setDirectory(dir);
endpoint.setInclude(mask);
return endpoint;
} else {
FileEndpoint endpoint = new FileEndpoint();
endpoint.getConfiguration().setDirectory(dir);
endpoint.setInclude(mask);
return endpoint;
}
}
}
Spring xml的用法示例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="fileReader" class="MyFileReader">
<constructor-arg name="name" value="myFileReader" />
<constructor-arg name="dir" value="/myfiles" />
<constructor-arg name="mask" value="*.txt" />
</bean>
<bean id="ftpReader" class="MyFileReader">
<constructor-arg name="name" value="myFtpReader" />
<constructor-arg name="dir" value="/myftpfiles" />
<constructor-arg name="mask" value="*.txt" />
<constructor-arg name="ftpHost" value="127.0.0.1" />
<constructor-arg name="ftpPort" value="1234" />
<constructor-arg name="ftpUser" value="myuser" />
<constructor-arg name="ftpPass" value="mypass" />
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring" id="integration-schedule" useMDCLogging="true">
<routeBuilder ref="fileReader" />
<routeBuilder ref="ftpReader" />
<route id="fileRoute">
<from uri="direct:fileReader_out" />
<log message="Do something with files" />
</route>
<route id="ftpRoute">
<from uri="direct:ftpReader_out" />
<log message="Do something with ftp files" />
</route>
</camelContext>
</beans>