我正在尝试创建一个Web服务,当调用本地目录时,会从那里获取文件并上传到ftp服务器。
我能够创建一个从本地目录中选择文件并上传到ftp服务器的简单路由是代码:
<route>
<from uri="file://D:\\FTPTest?noop=true&delay=2000" />
<to uri="ftp://user@host.in:21/public_html/EnterpriseProject?password=password123#"/>
<to uri="bean:myBean?method=test" />
</route>
但是,我希望在调用通过restlet webservice调用特定路由时调用此文件传输,我尝试使用以下代码,但它没有工作:
<route>
<from uri="direct:fileTransferRoute" />
<to uri="file://D:\\FTPTest?noop=true&delay=2000" />
<to uri="ftp://user@host.in:21/public_html/EnterpriseProject?password=password123#"/>
</route>
上述路由由restlet从以下路由调用:
<route>
<from
uri="restlet:http://0.0.0.0:9080/csitec/{serviceName}?restletMethod=post" />
<process ref="serviceRouteProcessor" />
<toD uri="direct:${in.header.nextRoute}" />
</route>
这是我的serviceRouteProcessor的代码:
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
String serviceName = exchange.getIn().getHeader(Constants.SERVICE_NAME).toString();
String nextRoute = serviceName+Constants.NEXT_ROUTE_APPENDER;
exchange.getOut().setHeader(Constants.NEXT_ROUTE, nextRoute);
exchange.getOut().setBody(body);
}
请帮助我并建议需要进行更改才能使其正常工作。
答案 0 :(得分:3)
您应该尝试content-enricher
的pollEnrich功能在示例部分中,您可以找到有关文件的示例。
你的路线应该是这样的(我只使用camel java dsl,所以这有点xml伪代码):
<route>
<from uri="direct:fileTransferRoute" />
<pollEnrich uri="file://D:\\FTPTest?fileName=data.txt....." />
<to uri="ftp://user@host.in:21/public_html/EnterpriseProject?password=password123#"/>
</route>
答案 1 :(得分:1)
已编辑:
你必须先了解一件事,to
是生产者而不是消费者<to uri="file://D:\\FTPTest?noop=true&delay=2000" />
你能做的是,
@Autowired
private CamelContext context;// if you have more than one camel context use @Qualifier and wire by bean id
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
String serviceName = exchange.getIn().getHeader(Constants.SERVICE_NAME).toString();
context.startRoute(serviceName+Constants.NEXT_ROUTE_APPENDER);// here in nextroute you must give the routeid
}
您的路线必须如
<route id = "<value of serviceName+Constants.NEXT_ROUTE_APPENDER>" autoStartup = "false">
<from uri="file://D:\\FTPTest..." />
<onCompletion onFailureOnly="true">
<choice>
<when>
<simple>${property.CamelBatchComplete}</simple>
<process ref="asyncSelfShutdownProcessor"/>
</when>
</choice>
</onCompletion>
<to uri="ftp://user@host.in:21..."/>
</route>
将asyncSelfShutdownProcessor添加到spring上下文
@Component
public class AsyncSelfShutdownProcessor implements AsyncProcessor {
@Autowired
private CamelContext context
public boolean process(Exchange exchange, AsyncCallback callback){
new Thread(() -> context.stopRoute(exchange.getFromRouteId())).start();
}
}
<强> ############################################ ################################## 强> 旧:
好的我理解你的需要 - 你有一条将文件从文件系统移动到ftp服务器的路由,你需要的只是当你从休息服务触发时才能执行这条路由。我会这样做,
*我将制作路线autoStartup = "false"
并将id = "fs-to-ftp"
指定给路线
<route id = "fs-to-ftp" autoStartup = "false">
<from uri="file://D:\\FTPTest..." />
<onCompletion onFailureOnly="true">
<process ref="asyncSelfShutdownProcessor"/>
</onCompletion>
<to uri="ftp://user@host.in:21..."/>
</route>
**在onComplete
中将自关闭异步进程添加到路由“fs-to-ftp”。 Async Processor
asyncSelfShutdownProcessor= AsyncProcessorConverterHelper.convert(exchange -> {
new Thread(() -> context.stopRoute("fs-to-ftp")).start();
});
***将驼峰上下文依赖项添加到休息服务,并在其余服务中按ID启动路由context.startRoute("fs-to-ftp")