You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2014/05/13 13:59:58 UTC

git commit: [KARAF-751] Add two commands for scheduling scripts and unscheduling jobs

Repository: karaf
Updated Branches:
  refs/heads/master eb61abc89 -> a0596c3f4


[KARAF-751] Add two commands for scheduling scripts and unscheduling jobs

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

Branch: refs/heads/master
Commit: a0596c3f46d2a7c545aa11d4ee559c8bfbd45853
Parents: eb61abc
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Tue May 13 13:59:47 2014 +0200
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Tue May 13 13:59:47 2014 +0200

----------------------------------------------------------------------
 scheduler/pom.xml                               |  2 +-
 .../karaf/scheduler/command/Schedule.java       | 96 ++++++++++++++++++++
 .../karaf/scheduler/command/Unschedule.java     | 41 +++++++++
 .../scheduler/command/support/ScriptJob.java    | 49 ++++++++++
 .../scheduler/core/InternalScheduleOptions.java | 14 +--
 .../karaf/scheduler/core/QuartzScheduler.java   |  1 +
 6 files changed, 195 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/a0596c3f/scheduler/pom.xml
----------------------------------------------------------------------
diff --git a/scheduler/pom.xml b/scheduler/pom.xml
index 6b061dd..9ea1cf0 100644
--- a/scheduler/pom.xml
+++ b/scheduler/pom.xml
@@ -71,7 +71,7 @@
                         </Import-Package>
                         <Private-Package>
                             org.apache.karaf.scheduler.core,
-                            org.apache.karaf.scheduler.command,
+                            org.apache.karaf.scheduler.command.*,
                             org.apache.karaf.util.tracker,
                             org.quartz,
                             org.quartz.core.*,

http://git-wip-us.apache.org/repos/asf/karaf/blob/a0596c3f/scheduler/src/main/java/org/apache/karaf/scheduler/command/Schedule.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/command/Schedule.java b/scheduler/src/main/java/org/apache/karaf/scheduler/command/Schedule.java
new file mode 100644
index 0000000..c17055f
--- /dev/null
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/command/Schedule.java
@@ -0,0 +1,96 @@
+/*
+ * 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.karaf.scheduler.command;
+
+import java.util.Date;
+
+import javax.xml.bind.DatatypeConverter;
+
+import org.apache.karaf.scheduler.ScheduleOptions;
+import org.apache.karaf.scheduler.Scheduler;
+import org.apache.karaf.scheduler.command.support.ScriptJob;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Function;
+import org.apache.karaf.shell.api.console.Session;
+
+@Command(scope = "scheduler", name = "schedule", description = "Schedule a script execution")
+@Service
+public class Schedule implements Action {
+
+    @Option(name = "--name", description = "Name of this job")
+    String name;
+
+    @Option(name = "--concurrent", description = "Should jobs run concurrently or not (defaults to false)")
+    boolean concurrent;
+
+    @Option(name = "--cron", description = "The cron expression")
+    String cron;
+
+    @Option(name = "--at", description = "Absolute date in ISO format (ex: 2014-05-13T13:56:45)")
+    String at;
+
+    @Option(name = "--times", description = "Number of times this job should be executed")
+    int times = -1;
+
+    @Option(name = "--period", description = "Time during executions (in seconds)")
+    long period;
+
+    @Argument(name = "script", required = true, description = "The script to schedule")
+    Function script;
+
+    @Reference
+    Scheduler scheduler;
+
+    @Reference
+    Session session;
+
+    @Override
+    public Object execute() throws Exception {
+        if (cron != null && (at != null || times != -1 || period != 0)) {
+            throw new IllegalArgumentException("Both cron expression and explicit execution time can not be specified");
+        }
+        ScheduleOptions options;
+        if (cron != null) {
+            options = scheduler.EXPR(cron);
+        } else {
+            Date date;
+            if (at != null) {
+                date = DatatypeConverter.parseDateTime(at).getTime();
+            } else {
+                date = new Date();
+            }
+            if (period > 0) {
+                options = scheduler.AT(date, times, period);
+            } else {
+                options = scheduler.AT(date);
+            }
+        }
+        if (name != null) {
+            options.name(name);
+        }
+        if (concurrent) {
+            options.canRunConcurrently(concurrent);
+        }
+        scheduler.schedule(new ScriptJob(session, script), options);
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/a0596c3f/scheduler/src/main/java/org/apache/karaf/scheduler/command/Unschedule.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/command/Unschedule.java b/scheduler/src/main/java/org/apache/karaf/scheduler/command/Unschedule.java
new file mode 100644
index 0000000..3395af7
--- /dev/null
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/command/Unschedule.java
@@ -0,0 +1,41 @@
+/*
+ * 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.karaf.scheduler.command;
+
+import org.apache.karaf.scheduler.Scheduler;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+
+@Command(scope = "scheduler", name = "unschedule", description = "Unschedule a job")
+@Service
+public class Unschedule implements Action {
+
+    @Argument(name = "name")
+    String name;
+
+    @Reference
+    Scheduler scheduler;
+
+    @Override
+    public Object execute() throws Exception {
+        scheduler.unschedule(name);
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/a0596c3f/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/ScriptJob.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/ScriptJob.java b/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/ScriptJob.java
new file mode 100644
index 0000000..47bd176
--- /dev/null
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/ScriptJob.java
@@ -0,0 +1,49 @@
+/*
+ * 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.karaf.scheduler.command.support;
+
+import java.util.Collections;
+
+import org.apache.karaf.scheduler.Job;
+import org.apache.karaf.scheduler.JobContext;
+import org.apache.karaf.shell.api.console.Function;
+import org.apache.karaf.shell.api.console.Session;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ScriptJob implements Job {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptJob.class);
+
+    private final Session session;
+    private final Function script;
+
+    public ScriptJob(Session session, Function script) {
+        this.session = session;
+        this.script = script;
+    }
+
+    @Override
+    public void execute(JobContext context) {
+        try {
+            script.execute(session, Collections.<Object>singletonList(context));
+        } catch (Exception e) {
+            LOGGER.warn("Error executing script", e);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/a0596c3f/scheduler/src/main/java/org/apache/karaf/scheduler/core/InternalScheduleOptions.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/core/InternalScheduleOptions.java b/scheduler/src/main/java/org/apache/karaf/scheduler/core/InternalScheduleOptions.java
index 6651ea8..c5d9dbf 100644
--- a/scheduler/src/main/java/org/apache/karaf/scheduler/core/InternalScheduleOptions.java
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/core/InternalScheduleOptions.java
@@ -17,11 +17,12 @@
 package org.apache.karaf.scheduler.core;
 
 import java.io.Serializable;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.Map;
-import java.util.TimeZone;
+
+import javax.xml.bind.DatatypeConverter;
 
 import org.apache.karaf.scheduler.ScheduleOptions;
 import org.quartz.CronExpression;
@@ -152,9 +153,8 @@ public class InternalScheduleOptions implements ScheduleOptions {
         if (date == null) {
             return "null";
         }
-        TimeZone tz = TimeZone.getTimeZone("UTC");
-        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
-        df.setTimeZone(tz);
-        return df.format(date);
+        Calendar c = GregorianCalendar.getInstance();
+        c.setTime(date);
+        return DatatypeConverter.printDateTime(c);
     }
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/a0596c3f/scheduler/src/main/java/org/apache/karaf/scheduler/core/QuartzScheduler.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/core/QuartzScheduler.java b/scheduler/src/main/java/org/apache/karaf/scheduler/core/QuartzScheduler.java
index a25d4e4..6b7309e 100644
--- a/scheduler/src/main/java/org/apache/karaf/scheduler/core/QuartzScheduler.java
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/core/QuartzScheduler.java
@@ -240,6 +240,7 @@ public class QuartzScheduler implements Scheduler {
             name = opts.name;
         } else {
             name = job.getClass().getName() + ':' + UUID.randomUUID();
+            opts.name = name;
         }
 
         final Trigger trigger = opts.trigger.withIdentity(name).build();