2017-12-27 50 views
0

背景:如何在使用「timer」循環處理時訪問「路由」中的當前ZonedDateTime(或Date)?

「配置()」的方法,在下面的示例代碼,循環在規定的時間間隔,此時我希望記錄當前的「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 
+0

順便說一句,調用'ZonedDateTime.now()'隱式應用JVM的當前默認時區。當前的默認值可以在運行時隨時更改。我建議總是明確地將你想要/預期的時區作爲'ZoneId'對象傳遞,比如'ZonedDateTime.now(ZoneId.of(「Africa/Tunis」))''。如果您想要當前的默認時區,請通過顯式調用['ZoneId.systemDefault()'](https://docs.oracle.com/javase/9​​/docs/api)讓您的代碼更加自我記錄以顯示意圖/java/time/ZoneId.html#systemDefault--)。 –

+0

雖然不是我想要解決的問題,但我確實確實採納了你的觀點。謝謝羅勒! :-) – sairn

回答

0

事實證明,「計時器」在可以使用的頭中插入「firedTime」屬性(雖然它當前是一個java.ut il.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 
相關問題