Triggers 如何在Apache Camel中触发现有路由?
有一条路线在30分钟后从SFTP读取Triggers 如何在Apache Camel中触发现有路由?,triggers,apache-camel,Triggers,Apache Camel,有一条路线在30分钟后从SFTP读取 public class ApplicationRoutesBuilder extends RouteBuilder { "sftp://10.10.10.10/emp" + "?username=xxx" + "&password=yyy" // Stored on wildfly server + "&do
public class ApplicationRoutesBuilder extends RouteBuilder {
"sftp://10.10.10.10/emp"
+ "?username=xxx"
+ "&password=yyy" // Stored on wildfly server
+ "&download=true" //Shall be read chunk by chunk to avoid heap space issues. Earlier download=true was used:
+ "&useList=true"
+ "&stepwise=false"
+ "&disconnect=true"
+ "&passiveMode=true"
+ "&reconnectDelay=1800000"
+ "&bridgeErrorHandler=true"
+ "&delay=30000"
//+ "&fileName=" + sftpFileName
+ "&include="+ sftpFileName
+ "&preMove=$simple{file:onlyname}.$simple{date:now:yyyy-MM-dd'T'hh-mm-ss}.processing"
+ "&move=$simple{file:onlyname.noext}.$simple{date:now:yyyy-MM-dd'T'hh-mm-ss}.success"
+ "&moveFailed=$simple{file:onlyname.noext}.$simple{date:now:yyyy-MM-dd'T'hh-mm-ss}.failed"
+ "&readLock=idempotent-changed"
+ "&idempotentRepository=#infinispan"
+ "&readLockRemoveOnCommit=true"
//ROUTE IS
from("ABOVE SFTP STRING")
.onException(GenericFileOperationFailedException.class)
.onWhen(exchange -> {
Throwable cause = exchange.getException(GenericFileOperationFailedException.class).getCause();
return (cause != null && cause.toString().equalsIgnoreCase("2: No such file"));
})
.handled(true)
.logExhausted(true)
.logExhaustedMessageHistory(true)
// For troubleshooting. TODO: remove on final deploy
.log("Could not find file")
.end()
.to(archiveReceivedFile(sftpFileName))
.log("Archived Successfully in HRM Archive Directory")
.bean("service1", "enrichFromAd")
.log("Loaded IFS and AD Successfully into Memory")
.split(body().tokenizeXML("EmploymentRequest", "EmploymentRequests")) // Split and tokenize the requests, streaming individual requests to message queue
.unmarshal(new JaxbDataFormat(JAXBContext.newInstance(EmploymentRequest.class)))
.bean("service1", "updateEmployeeData")
.marshal(new JaxbDataFormat(JAXBContext.newInstance(EmploymentRequest.class)))
.inOnly(EMPLOYEES_QUEUE)
.choice()
.when(header("CamelSplitComplete"))
.log("Download xml file completed");
我想手动触发这条路线。我们如何使用ApacheCamel实现这一点
//到目前为止,我们正在重新加载或取消部署,并部署或重新启动服务器,以在加载时触发路由,或将时间减少到30秒。但我不想要这个。我可以创建trig文件或重命名,也可以使用JSP页面创建trig。但是什么代码会触发这个线程呢
以下是上下文初始化:
@ApplicationScoped
public class ApplicationCamelContext extends DefaultCamelContext {
private static final Logger LOGGER = LoggerFactory.getLogger( ApplicationCamelContext.class.getName() );
@PostConstruct
void customize() {
LOGGER.info("Started ApplicationCamelContext: customize");
setName("employee-import");
getShutdownStrategy().setTimeout(2L);
LOGGER.info("Shutdown ApplicationCamelContext: customize");
}
}
您可以使用按需发送路线。Camel提供了
rich()
和pollrich()
作为JavaDSL的一部分来实现它
是一个单元测试用例,它完成了几乎相同的事情。在端点使用消息传递触发sftp
操作
另一方面,上面的示例使用了vm:
组件,但您可以根据具体的用例使用from()
构造中的组件。它们之间的差异在本文中有解释。我认为seda:
最适合这种情况
路由实现应该类似于seda:trigger->pollEnrich(sftp)->处理的其余部分
一旦准备就绪,您可以使用发送消息到seda:trigger
以启动SFTP下载。何时使用ProducerTemplate完全取决于您的实现(触发器文件/rest端点..)