karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [karaf] branch master updated: [KARAF-6022] Add times support in the scheduler
Date Tue, 04 Dec 2018 14:02:09 GMT
This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/master by this push:
     new 426c1c9  [KARAF-6022] Add times support in the scheduler
     new d39c6c7  [KARAF-6022] This closes #607
426c1c9 is described below

commit 426c1c9a53e0233de2d9f61c3af889b50f7a8cd0
Author: Miroslav Beranič <miroslav.beranic@mibesis.si>
AuthorDate: Mon Sep 10 19:21:46 2018 +0200

    [KARAF-6022] Add times support in the scheduler
---
 examples/karaf-scheduler-example/README.md         |  2 +-
 .../karaf/examples/scheduler/RunnableService.java  |  1 +
 manual/src/main/asciidoc/user-guide/scheduler.adoc | 23 ++++++++++++++++++++++
 .../java/org/apache/karaf/scheduler/Scheduler.java |  7 +++++++
 .../karaf/scheduler/core/WhiteboardHandler.java    | 20 ++++++++++++++++++-
 5 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/examples/karaf-scheduler-example/README.md b/examples/karaf-scheduler-example/README.md
index 74d1aff..cbb58d2 100644
--- a/examples/karaf-scheduler-example/README.md
+++ b/examples/karaf-scheduler-example/README.md
@@ -46,7 +46,7 @@ mvn clean install
 On a running Karaf instance, register the features repository using:
 
 ```
-karaf@root()> feature:repo-add mvn:org.apache.karaf.examples/karaf-scheduler-example-features/4.2.1-SNAPSHOT/xml
+karaf@root()> feature:repo-add mvn:org.apache.karaf.examples/karaf-scheduler-example-features/4.2.2-SNAPSHOT/xml
 ```
 
 Then, you can install the runnable service feature:
diff --git a/examples/karaf-scheduler-example/karaf-scheduler-example-runnable/src/main/java/org/apache/karaf/examples/scheduler/RunnableService.java
b/examples/karaf-scheduler-example/karaf-scheduler-example-runnable/src/main/java/org/apache/karaf/examples/scheduler/RunnableService.java
index c5375f4..31b8597 100644
--- a/examples/karaf-scheduler-example/karaf-scheduler-example-runnable/src/main/java/org/apache/karaf/examples/scheduler/RunnableService.java
+++ b/examples/karaf-scheduler-example/karaf-scheduler-example-runnable/src/main/java/org/apache/karaf/examples/scheduler/RunnableService.java
@@ -22,6 +22,7 @@ import org.osgi.service.component.annotations.Component;
         property = {
                 "scheduler.name=example",
                 "scheduler.period:Long=10",
+                "scheduler.times:Integer=5",
                 "scheduler.concurrent:Boolean=false"
         }
 )
diff --git a/manual/src/main/asciidoc/user-guide/scheduler.adoc b/manual/src/main/asciidoc/user-guide/scheduler.adoc
index b8a02d3..5d21e41 100644
--- a/manual/src/main/asciidoc/user-guide/scheduler.adoc
+++ b/manual/src/main/asciidoc/user-guide/scheduler.adoc
@@ -47,6 +47,9 @@ All jobs allow configuration using service properties:
 |Scheduler.PROPERTY_SCHEDULER_PERIOD | - |
 Defines the period for a job. The period is expressed in seconds. This property needs to
be of type Long.
 
+|Scheduler.PROPERTY_SCHEDULER_TIMES | -1 |
+Defines the number of times the job is executed. -1 means infinite.
+
 |Scheduler.PROPERTY_SCHEDULER_IMMEDIATE |false |
 Define if a periodically job should be scheduled immediate.
 Default is to not startup immediate, the job is started the first time after the period has
expired.
@@ -120,6 +123,26 @@ Name                       │ Schedule
 PingJob.126                │ at(2017-11-22T15:37:17.103+01:00, -1, 10)
 ----
 
+It's also possible to pass the number of execution of the scheduler job using the `scheduler.times`
service property:
+
+```
+@Component(immediate = true, property = {
+    "scheduler.period:Long=60",
+    "scheduler.times:Integer=5",
+    "scheduler.concurrent:Boolean=false",
+    "scheduler.name=PingJob"
+  }
+)
+public class PingThread implements Runnable {
+
+  @Override
+  public void run() {
+    // ..
+  }
+
+}
+```
+
 ==== Schedule a new Job using the Gogo Shell
 
 ----
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 647b48d..8fea7c8 100644
--- a/scheduler/src/main/java/org/apache/karaf/scheduler/Scheduler.java
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/Scheduler.java
@@ -44,6 +44,13 @@ public interface Scheduler {
     String PROPERTY_SCHEDULER_PERIOD = "scheduler.period";
 
     /**
+     * Name of the configuration property to defined the number of iterations for a job.
+     * The times is expressed in iterations.
+     * This property needs to be of numeric type.
+     */
+    String PROPERTY_SCHEDULER_TIMES = "scheduler.times";
+
+    /**
      * Name of the configuration property to define if a periodically job should be scheduled
immediate.
      * Default is to not startup immediate, the job is started the first time after the period
has expired.
      * This property needs to be of type Boolean.
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/core/WhiteboardHandler.java
b/scheduler/src/main/java/org/apache/karaf/scheduler/core/WhiteboardHandler.java
index 3acfaae..f7a3822 100644
--- a/scheduler/src/main/java/org/apache/karaf/scheduler/core/WhiteboardHandler.java
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/core/WhiteboardHandler.java
@@ -115,6 +115,22 @@ public class WhiteboardHandler {
                         .name(name)
                         .canRunConcurrently(concurrent));
             } else {
+                Integer times = -1;
+                {
+                    final Object v = ref.getProperty(Scheduler.PROPERTY_SCHEDULER_TIMES);
+                    if (null != v) {
+                        if (v instanceof Integer) {
+                            times = (Integer) v;
+                        } else if (v instanceof Long) {
+                            times = ((Long) v).intValue();
+                        } else if (v instanceof Number) {
+                            times = ((Number) v).intValue();
+                        } else {
+                            times = new Integer(v.toString());
+                        }
+                    }
+                }
+
                 Long period = null;
                 if (ref.getProperty(Scheduler.PROPERTY_SCHEDULER_PERIOD) != null) {
                     if (ref.getProperty(Scheduler.PROPERTY_SCHEDULER_PERIOD) instanceof Long)
{
@@ -124,6 +140,8 @@ public class WhiteboardHandler {
                     }
                     if (period < 1) {
                         this.logger.debug("Ignoring service {} : scheduler period is less
than 1.", ref);
+                    } else if (times < -1) {
+                        this.logger.debug("Ignoring service {} : scheduler times is defined
but is less than -1.", ref);
                     } else {
                         boolean immediate = false;
                         if (ref.getProperty(Scheduler.PROPERTY_SCHEDULER_IMMEDIATE) != null)
{
@@ -137,7 +155,7 @@ public class WhiteboardHandler {
                         if (!immediate) {
                             date.setTime(System.currentTimeMillis() + period * 1000);
                         }
-                        this.scheduler.schedule(job, this.scheduler.AT(date, -1, period)
+                        this.scheduler.schedule(job, this.scheduler.AT(date, times, period)
                                 .name(name)
                                 .canRunConcurrently((concurrent != null ? concurrent : true)));
                     }


Mime
View raw message