如何触发Apache Camel中的现有路由?

有一条路由会在30分钟后从SFTP读取

 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");

我想手动触发此路线。我们如何使用Apache Camel做到这一点?

//到目前为止,我们正在重新加载或取消部署,部署或重新启动服务器,以在负载时触发路由或将时间减少到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");
    }

}
asdqaz2008 回答:如何触发Apache Camel中的现有路由?

您可以使用Content Enricher EIP按需设置路线。骆驼提供enrich()pollEnrich()作为Java DSL的一部分来实现它。

Here是一个单元测试用例,其功能几乎相同。在端点上使用消息传递来触发sftp操作。

顺便说一句,上面的示例使用了vm:组件,但是您可以在from()结构中使用direct:seda:vm:组件,根据您的特定用例。它们的差异在another answer here中进行了说明。我认为seda:最适合这种情况。

路由实现应类似于
seda:trigger -> pollEnrich(sftp) -> rest of the processing 准备就绪后,您可以使用ProducerTemplate将消息传递到seda:trigger以启动SFTP下载。何时使用ProducerTemplate完全取决于您进行实施(触发文件/其余端点。)

本文链接:https://www.f2er.com/3096844.html

大家都在问