Browse Source

定时任务

Jxing 6 years ago
parent
commit
e7597ef30a

+ 49 - 0
src/main/java/com/sagacloud/route/processors/PeriodTask/OnTimeTaskProcessor.java

@@ -0,0 +1,49 @@
+package com.sagacloud.route.processors.PeriodTask;
+/*
+ * Author: Jxing
+ * Create Time: 2018/7/14
+ */
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+import java.util.ArrayList;
+import java.util.Date;
+
+public class OnTimeTaskProcessor implements Processor {
+    public static int[] time = new int[]{17,0, 18, 0};
+    private static ArrayList<Long> nextTimeStamps = new ArrayList<>();
+    private final int ONEDAY = 86400000;
+    private final int ONEMINUTE = 60000;
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        if(time.length > 0 && nextTimeStamps.size() == 0)
+            init();
+        boolean isTime = false;
+        Date now = new Date();
+        for(int i = nextTimeStamps.size() - 1; i > -1; --i){
+            long timeStamp = nextTimeStamps.get(i);
+            if(now.getTime() > timeStamp)
+            {
+                isTime = true;
+                nextTimeStamps.remove(i);
+            }
+            System.out.println("时间戳:" + timeStamp);
+        }
+        if(!isTime)
+            exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
+    }
+    private void init(){
+        for(int i = 0; i < time.length; i+=2){
+            Date now = new Date();
+            Date nextTime = new Date();
+            nextTime.setHours(time[i]);
+            nextTime.setMinutes(time[i+1]);
+            nextTime.setSeconds(0);
+            if(now.getTime() + ONEMINUTE> nextTime.getTime()){
+                nextTimeStamps.add(nextTime.getTime() + ONEDAY);
+            }else
+                nextTimeStamps.add(nextTime.getTime());
+        }
+    }
+}