您是否真的需要駱駝組件,或者您可以通過自定義路線生成器來實現?這裏只是原料爲例,說明總體思路:
自定義路徑生成器類,而不是組件:
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>
我編輯您的文章(它在隊列)稍微澄清一下,但是我不能說出你在這一行中的含義:「作爲一個適配器,我們有一個文件作爲entry或者ftp ..」你可以試着去理解它嗎? – gkubed
@gkubed我的意思是我不想使用不同的入口點作爲第一個例子:from(「file」)from(「sftp」)from(「stream」)我希望mu組件完成所有工作並檢測當我從(「mycomponent」)做它的權利入口點,它可以是一個文件或一個流或任何東西 –
對我來說,這看起來像YAGNI。 – gtonic