LOG4J2-89 add CronScheduledFuture

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a7977f5a
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a7977f5a
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a7977f5a

Branch: refs/heads/master
Commit: a7977f5a14797df2e6fb4455822838acf0e68d17
Parents: 2498a2e
Author: Ralph Goers <[email protected]>
Authored: Tue Nov 17 06:55:06 2015 -0700
Committer: Ralph Goers <[email protected]>
Committed: Tue Nov 17 06:55:06 2015 -0700

----------------------------------------------------------------------
 .../core/config/ConfigurationScheduler.java     | 15 +++-
 .../log4j/core/config/CronScheduledFuture.java  | 76 ++++++++++++++++++++
 2 files changed, 88 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a7977f5a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java
index 2542443..fcd81d9 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java
@@ -110,9 +110,12 @@ public class ConfigurationScheduler extends 
AbstractLifeCycle {
      * @param command The Runnable to run,
      * @return a ScheduledFuture representing the next time the command will 
run.
      */
-    public ScheduledFuture<?> scheduleWithCron(CronExpression cronExpression, 
Runnable command) {
+    public CronScheduledFuture<?> scheduleWithCron(CronExpression 
cronExpression, Runnable command) {
         CronRunnable runnable = new CronRunnable(command, cronExpression);
-        return schedule(runnable, nextFireInterval(cronExpression), 
TimeUnit.MILLISECONDS);
+        ScheduledFuture<?> future = schedule(runnable, 
nextFireInterval(cronExpression), TimeUnit.MILLISECONDS);
+        CronScheduledFuture<?> cronScheduledFuture = new 
CronScheduledFuture<>(future);
+        runnable.setScheduledFuture(cronScheduledFuture);
+        return cronScheduledFuture;
     }
 
 
@@ -149,19 +152,25 @@ public class ConfigurationScheduler extends 
AbstractLifeCycle {
 
         private final CronExpression cronExpression;
         private final Runnable runnable;
+        private CronScheduledFuture<?> scheduledFuture;
 
         public CronRunnable(Runnable runnable, CronExpression cronExpression) {
             this.cronExpression = cronExpression;
             this.runnable = runnable;
         }
 
+        public void setScheduledFuture(CronScheduledFuture<?> future) {
+            this.scheduledFuture = future;
+        }
+
         public void run() {
             try {
                 runnable.run();
             } catch(Throwable ex) {
                 LOGGER.error("Error running command", ex);
             } finally {
-                schedule(this, nextFireInterval(cronExpression), 
TimeUnit.MILLISECONDS);
+                ScheduledFuture<?> future = schedule(this, 
nextFireInterval(cronExpression), TimeUnit.MILLISECONDS);
+                scheduledFuture.setScheduledFuture(future);
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a7977f5a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/CronScheduledFuture.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/CronScheduledFuture.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/CronScheduledFuture.java
new file mode 100644
index 0000000..47e07f3
--- /dev/null
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/CronScheduledFuture.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.config;
+
+import java.util.concurrent.Delayed;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ *
+ */
+public class CronScheduledFuture<V> implements ScheduledFuture<V> {
+
+    private volatile ScheduledFuture<?> scheduledFuture;
+
+    public CronScheduledFuture(ScheduledFuture<V> future) {
+        this.scheduledFuture = future;
+    }
+
+    void setScheduledFuture(ScheduledFuture<?> future) {
+        this.scheduledFuture = future;
+    }
+
+    @Override
+    public long getDelay(TimeUnit unit) {
+        return scheduledFuture.getDelay(unit);
+    }
+
+    @Override
+    public int compareTo(Delayed delayed) {
+        return scheduledFuture.compareTo(delayed);
+    }
+
+    @Override
+    public boolean cancel(boolean mayInterruptIfRunning) {
+        return scheduledFuture.cancel(mayInterruptIfRunning);
+    }
+
+    @Override
+    public boolean isCancelled() {
+        return scheduledFuture.isCancelled();
+    }
+
+    @Override
+    public boolean isDone() {
+        return scheduledFuture.isDone();
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public V get() throws InterruptedException, ExecutionException {
+        return (V) scheduledFuture.get();
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public V get(long timeout, TimeUnit unit) throws InterruptedException, 
ExecutionException, TimeoutException {
+        return (V) scheduledFuture.get(timeout, unit);
+    }
+}

Reply via email to