首页 > 解决方案 > 在路由骆驼apache之间传递参数

问题描述

from("quartz2://dailyCamelScheduler?cron=" +
          "0+0/1+*+*+*+?+*"  )
    .log(LoggingLevel.INFO, "ReconciliationBatchRoute", "Daily camel route called")
    .routeId("dailyCamelRoute")
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            camelCronExpression = "sftp://"
    //                + username + "@"
                      + hostAddress
                      + ":22"
                      + "/POLICE_BELGE_MUTABAKAT_"
                      + "DAILY"
    //                + "&" +
                      +"?username=" + username
                      + "&password=" + password
                      + "&fileName=" + fileName
                      + "&autoCreate=false"
                      + "&strictHostKeyChecking=no"
                      + "&preferredAuthentications=publickey,password";
            exchange.setProperty("xxx", camelCronExpression);
        }
    })
    .setProperty("typeOfRoute").constant(DocumentPolicyJobTypeEnum.DAILY)
    .to("direct:ReconciliationBatchRoute.getFileFromSFTP");
    
from("direct:ReconciliationBatchRoute.getFileFromSFTP")
    .pollEnrich()
    .simple(camelCronExpression)
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            logger.info("MutabakatLog Strat Mutabakat");
            List<String> strings = IOUtils.readLines(exchange.getIn().getBody(InputStream.class));
            logger.info("qqsize: {}", strings.size());
        }
    })
    .stop();

第一条路线是每天预定的路线。调用时,它将调用第二条路由从 SFTP 转到每日文件夹。还有其他路线,例如每月,每周。因此,他们将有不同的路线和不同的 crons,就像第一个一样。但他们会用准备好的 cron 表达式调用第二个。

每个文件夹都不一样。

这是全球性的:

private String camelCronExpression;

我还尝试将那条路线放在第一个交换位置。我还输入了开始代码。

exchange.setProperty("xxx", camelCronExpression);

我试图这样得到它:因为设置为类的全局变量可能不好并给出错误:

private String expression;

from("direct:ReconciliationBatchRoute.getFileFromSFTP")
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            expression = exchange.getProperty("xxx")
        }
    })
    .pollEnrich()
    .simple(expression) //here expression seems null

但我无法在 .simple() 中得到表达式

你觉得这些方法好吗?我怎样才能让它不为空?

我无法同时获取和投票:

from("direct:ReconciliationBatchRoute.getFileFromSFTP")
    .pollEnrich()
    .simple(camelCronExpression)

标签: javacronapache-camelspring-camel

解决方案


您可以使用以下语法之一通过简单的语言取回您的交换属性。

  • 交换属性.foo
  • 交换属性[foo]
  • 交换属性.foo.OGNL

在 Java DSL 中,

from("direct:ReconciliationBatchRoute.getFileFromSFTP")
    .pollEnrich()
    .simple("${exchangeProperty.xxx}")

exchangeProperty 语言,它也可以通过

from("direct:ReconciliationBatchRoute.getFileFromSFTP")
    .pollEnrich()
    .exchangeProperty("xxx")

推荐阅读