0

背景:

在下面的示例代码中,“configure()”方法以指定的时间间隔循环,此时我希望记录当前的“ZonedDateTime.now() 值

问题:

ZonedDateTime.now() 值始终是相同的值,尽管存在间隔时间差异。

问题:

我可以使用什么技术在每个时间间隔访问当前 ZonedDateTime.now() 值?

(注意:我最终希望将此值用作 REST 调用中的参数)

示例代码:

package aaa.bbb.ccc.dateparmissue;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;

@ContextName("rest-dsl")
public class DateParmIssue extends RouteBuilder {

    public DateParmIssue() {
    }

    private final String codeList = "AA,BB,CC";
    private final int notifyTime = 10;  //<==10 second interval

    @Override
    public void configure() throws Exception {

    org.apache.log4j.MDC.put("app.name", "dateParmIssue");

    System.getProperties().list(System.out);

    onException(Exception.class)
        .log("onException_processing_exception:" +  this.exceptionMessage().toString() + "...send to_error_queue:" + body(String.class).toString())
        .handled(true);

    from("timer://foo?fixedRate=true&period=" + (notifyTime * 1000))
        .setBody(constant(this.codeList))
        .to("seda:node0");

    from("seda:node0")
        .split().tokenize(",")
        .to("seda:node1");

    from("seda:node1")  
          .log("seda:node1...body-code=${body}...zdt=" + simple(ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)));
    }    
}

样本输出...

2017-12-27 12:17:11,649 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=AA...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:11,653 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=BB...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:11,653 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=CC...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:21,630 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=AA...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:21,630 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=BB...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:21,631 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=CC...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:31,633 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=AA...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:31,636 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=BB...zdt=Simple: 2017-12-27T12:17:10.306-05:00
2017-12-27 12:17:31,637 | INFO  | 1 - seda://node1 | route3                           | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | direct:node1...body-code=CC...zdt=Simple: 2017-12-27T12:17:10.306-05:00  
4

1 回答 1

0

事实证明,“计时器”在可以使用的标头中插入了一个“firedTime”属性(尽管它当前作为 java.util.Date 对象,而不是较新的 Java 8 日期对象之一 - 例如 ZonedDateTime) .

因此,替代方案可能如下 - 即显示更改日期...

from("seda:node1")
    .process(new Processor(){
        public void process(Exchange exchange) {
        exchange.getIn().setHeader("code", exchange.getIn().getBody().toString());
        exchange.getIn().setBody((new Date(exchange.getIn().getHeader("firedTime").toString()).toInstant()).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
        }
    })
    .log("seda:node1...body-code=${in.header.code}...zdt=${body}");    

...现在日期改变了...

2017-12-28 15:37:18,017 | INFO  | 7 - seda://node1 | route17                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | seda:node1...body-code=AA...zdt=2017-12-28T15:37:18-05:00
2017-12-28 15:37:18,019 | INFO  | 7 - seda://node1 | route17                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | seda:node1...body-code=BB...zdt=2017-12-28T15:37:18-05:00
2017-12-28 15:37:18,020 | INFO  | 7 - seda://node1 | route17                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | seda:node1...body-code=CC...zdt=2017-12-28T15:37:18-05:00
2017-12-28 15:37:28,017 | INFO  | 7 - seda://node1 | route17                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | seda:node1...body-code=AA...zdt=2017-12-28T15:37:28-05:00
2017-12-28 15:37:28,017 | INFO  | 7 - seda://node1 | route17                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | seda:node1...body-code=BB...zdt=2017-12-28T15:37:28-05:00
2017-12-28 15:37:28,018 | INFO  | 7 - seda://node1 | route17                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | seda:node1...body-code=CC...zdt=2017-12-28T15:37:28-05:00
于 2017-12-28T20:43:54.713 回答