karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [1/2] karaf git commit: [KARAF-5306] Add manual trigger command to karaf scheduler
Date Sun, 13 Aug 2017 05:07:32 GMT
Repository: karaf
Updated Branches:
  refs/heads/master b7bdb3594 -> 0400c2d7a


[KARAF-5306] Add manual trigger command to karaf scheduler


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

Branch: refs/heads/master
Commit: 64c049555085da34f7cb48c484c5a535bd416e71
Parents: b7bdb35
Author: Ciprian Ciubotariu <cheepeero@gmx.net>
Authored: Wed Aug 9 16:43:58 2017 +0300
Committer: Jean-Baptiste Onofré <jbonofre@apache.org>
Committed: Sun Aug 13 06:48:59 2017 +0200

----------------------------------------------------------------------
 .../org/apache/karaf/scheduler/Scheduler.java   |  9 ++++
 .../apache/karaf/scheduler/command/Trigger.java | 55 ++++++++++++++++++++
 .../scheduler/command/support/TriggerJob.java   | 36 +++++++++++++
 .../karaf/scheduler/core/QuartzScheduler.java   | 18 +++++++
 4 files changed, 118 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/64c04955/scheduler/src/main/java/org/apache/karaf/scheduler/Scheduler.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/Scheduler.java b/scheduler/src/main/java/org/apache/karaf/scheduler/Scheduler.java
index 3bf18cb..975e2d4 100644
--- a/scheduler/src/main/java/org/apache/karaf/scheduler/Scheduler.java
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/Scheduler.java
@@ -92,6 +92,15 @@ public interface Scheduler {
     Map<Object, ScheduleOptions> getJobs() throws SchedulerError;
 
     /**
+     * Triggers a scheduled job.
+     *
+     * @param jobName The name of the job.
+     * @return <code>true</code> if the job was triggered, otherwise <code>false</code>
+     * @throws SchedulerError  if the job can't be triggered.
+     */
+    boolean trigger(String jobName) throws SchedulerError;
+
+    /**
      * Create a schedule options to fire a job immediately and only once.
      *
      * @return The corresponding {@link ScheduleOptions}.

http://git-wip-us.apache.org/repos/asf/karaf/blob/64c04955/scheduler/src/main/java/org/apache/karaf/scheduler/command/Trigger.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/command/Trigger.java b/scheduler/src/main/java/org/apache/karaf/scheduler/command/Trigger.java
new file mode 100644
index 0000000..f596345
--- /dev/null
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/command/Trigger.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2017 The Apache Software Foundation.
+ *
+ * Licensed 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.command.support.TriggerJob;
+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.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+
+@Command(scope = "scheduler", name = "trigger", description = "Manually trigger a scheduled
job")
+@Service
+public class Trigger implements Action {
+
+    @Argument(description = "Name of the job to trigger", required = true)
+    String name;
+
+    @Option(name = "-b", aliases = "background", description = "schedule the trigger in the
background", required = false)
+    boolean background = false;
+
+    @Reference
+    Scheduler scheduler;
+
+    @Override
+    public Object execute() throws Exception {
+        if (background) {
+            System.out.println("Scheduling background trigger for job " + name);
+            scheduler.schedule(new TriggerJob(scheduler, name), scheduler.NOW());
+        } else {
+            System.out.println("Triggering job " + name);
+            if (!scheduler.trigger(name)) {
+                System.out.println("Could not find a scheduled job with name " + name);
+            }
+        }
+        return null;
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/64c04955/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/TriggerJob.java
----------------------------------------------------------------------
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/TriggerJob.java
b/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/TriggerJob.java
new file mode 100644
index 0000000..63b149c
--- /dev/null
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/command/support/TriggerJob.java
@@ -0,0 +1,36 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.apache.karaf.scheduler.command.support;
+
+import org.apache.karaf.scheduler.Scheduler;
+import org.apache.karaf.scheduler.SchedulerError;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TriggerJob implements Runnable {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(TriggerJob.class);
+
+    private final Scheduler scheduler;
+    private final String name;
+
+    public TriggerJob(Scheduler scheduler, String name) {
+        this.scheduler = scheduler;
+        this.name = name;
+    }
+
+    @Override
+    public void run() {
+        try {
+            if (!scheduler.trigger(name)) {
+                LOGGER.warn("Could not find a scheduled job with name " + name);
+            }
+        } catch (SchedulerError ex) {
+            LOGGER.error("Failed to trigger job {}", name, ex);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/64c04955/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 f02fcfc..8641a5d 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
@@ -301,4 +301,22 @@ public class QuartzScheduler implements Scheduler {
         }
     }
 
+    @Override
+    public boolean trigger(String jobName) throws SchedulerError {
+        final org.quartz.Scheduler s = this.scheduler;
+        if (jobName != null && s != null) {
+            try {
+                final JobKey key = JobKey.jobKey(jobName);
+                final JobDetail jobdetail = s.getJobDetail(key);
+                if (jobdetail != null) {
+                    this.scheduler.triggerJob(key, jobdetail.getJobDataMap());
+                    return true;
+                }
+            } catch (SchedulerException ex) {
+                throw new SchedulerError(ex);
+            }
+        }
+        return false;
+    }
+
 }


Mime
View raw message