You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2016/01/01 21:27:21 UTC

deltaspike git commit: DELTASPIKE-1056 documentation for using java.lang.Runnable with @Scheduled

Repository: deltaspike
Updated Branches:
  refs/heads/master 6561ee1ee -> e81a1a45a


DELTASPIKE-1056 documentation for using java.lang.Runnable with @Scheduled


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

Branch: refs/heads/master
Commit: e81a1a45aba66279e4207c8404b33716c7820fac
Parents: 6561ee1
Author: gpetracek <gp...@apache.org>
Authored: Fri Jan 1 20:57:43 2016 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Fri Jan 1 21:26:08 2016 +0100

----------------------------------------------------------------------
 documentation/src/main/asciidoc/scheduler.adoc | 58 +++++++++++++++++++++
 1 file changed, 58 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/e81a1a45/documentation/src/main/asciidoc/scheduler.adoc
----------------------------------------------------------------------
diff --git a/documentation/src/main/asciidoc/scheduler.adoc b/documentation/src/main/asciidoc/scheduler.adoc
index d8ae424..354725c 100644
--- a/documentation/src/main/asciidoc/scheduler.adoc
+++ b/documentation/src/main/asciidoc/scheduler.adoc
@@ -174,6 +174,64 @@ or
 </alternatives>
 -----------------------------------------------------------------------------
 
+== @Scheduled with java.lang.Runnable
+
+In several applications there is no need to use `JobExecutionContext` and therefore there shouldn't be the need to implement `org.quartz.Job`.
+For implementing custom schedulers which use custom job-types it was always possible to configure a custom class or interface
+by adding something like `deltaspike.scheduler.job-class=myPackage.MyJobType` to `META-INF/apache-deltaspike.properties`.
+At runtime the corresponding scheduler-implementation gets active.
+Since DeltaSpike v1.5.3, the scheduler-module supports `java.lang.Runnable` out-of-the-box by adding `deltaspike.scheduler.job-class=java.lang.Runnable`
+to `META-INF/apache-deltaspike.properties`.
+
+With that it's possible to use something like:
+
+[source,java]
+---------------------------------------------------------------------------------
+@Scheduled(cronExpression = "0 0/10 * * * ?")
+public class CdiAwareQuartzJob implements java.lang.Runnable
+{
+    @Inject
+    private MyService service;
+
+    @Override
+    public void run()
+    {
+        //...
+    }
+}
+---------------------------------------------------------------------------------
+
+Behind the scenes DeltaSpike registers an adapter for Quartz which just delegates to the `run`-method once the adapter gets called by Quartz.
+Technically you end up with almost the same, just with a reduced API for implementing (all) your scheduled jobs.
+Per default the adapter just calls the `run`-method directly, because Quartz executes the adapter in a new thread anyway.
+
+
+== Execute java.lang.Runnable with ManagedExecutorService
+
+If you would like to use e.g. the `ManagedExecutorService` (with EE7+) to run the jobs,
+you can provide a custom adapter by adding e.g.
+`deltaspike.scheduler.runnable-adapter-class=mypackage.DelegatingJobRunnableAdapter` to `META-INF/apache-deltaspike.properties`.
+Such an adapter just needs to implement `org.quartz.Job` and in case of EE7+ inject e.g. `ManagedExecutorService` as shown in the following example:
+
+[source,java]
+---------------------------------------------------------------------------------
+public class DelegatingJobRunnableAdapter implements Job
+{
+    @Resource
+    private ManagedExecutorService managedExecutorService;
+
+    @Override
+    public void execute(JobExecutionContext context) throws JobExecutionException
+    {
+        Class<? extends Runnable> jobClass =
+            ClassUtils.tryToLoadClassForName(context.getJobDetail().getKey().getName(), Runnable.class);
+
+        Runnable runnableBean = BeanProvider.getContextualReference(jobClass);
+        managedExecutorService.execute(runnableBean);
+    }
+}
+---------------------------------------------------------------------------------
+
 == Custom Scheduler
 
 It is possible to replace the default integration with Quartz. Any scheduler that supports cron-expressions for job-classes can be used.