java-8jbossfusecamel-blueprint

How can I access the current ZonedDateTime (or Date) in a "route" when processing with a "timer" loop?


Background:

The "configure()" method, in the sample code below, loops at specified time interval at which time I wish to log the current "ZonedDateTime.now() value

Problem:

The ZonedDateTime.now() value is always the same value, despite the interval time difference.

Question:

What technique can I used to access the current ZonedDateTime.now() value at each interval?

(NOTE: I ultimately, wish to use this value as a parameter in a REST call)

Sample code:

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

Sample output...

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  

Solution

  • It turns out that the "timer" inserts a "firedTime" property in the header that can be used (although it is currently as a java.util.Date object, rather than one of the newer Java 8 date objects - e.g., ZonedDateTime).

    So, an alternative might be as follows - i.e., which shows the changing date...

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

    ...and date now changes...

    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