Apache Camel - 特定路由调用上的文件传输
Apache Camel - File Transfer on Particular Route Call
我正在尝试创建一个 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);
}
请帮助我并建议需要进行更改以使其像这样工作。
已编辑:
你必须先明白一件事,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 context
@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();
});
***在rest服务中添加camel上下文依赖,在rest服务中通过id启动路由context.startRoute("fs-to-ftp")
您应该尝试 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>
我正在尝试创建一个 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);
}
请帮助我并建议需要进行更改以使其像这样工作。
已编辑:
你必须先明白一件事,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 context
@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();
});
***在rest服务中添加camel上下文依赖,在rest服务中通过id启动路由context.startRoute("fs-to-ftp")
您应该尝试 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>