You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2018/01/17 10:14:30 UTC

[karaf] branch karaf-4.1.x updated: [KARAF-5558] Expose Quartz Scheduler configuration

This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/karaf-4.1.x by this push:
     new 3a2178b  [KARAF-5558] Expose Quartz Scheduler configuration
3a2178b is described below

commit 3a2178b1681e22215450015635cbefe18ed1bb31
Author: Jean-Baptiste Onofré <jb...@apache.org>
AuthorDate: Wed Jan 10 21:00:25 2018 +0100

    [KARAF-5558] Expose Quartz Scheduler configuration
---
 .../features/standard/src/main/feature/feature.xml | 38 +++++++++++++++++++
 scheduler/pom.xml                                  |  8 ++++
 .../org/apache/karaf/scheduler/core/Activator.java | 34 ++++++++++++++---
 .../karaf/scheduler/core/QuartzScheduler.java      | 44 ++++++++--------------
 4 files changed, 90 insertions(+), 34 deletions(-)

diff --git a/assemblies/features/standard/src/main/feature/feature.xml b/assemblies/features/standard/src/main/feature/feature.xml
index a811bd4..08faed7 100644
--- a/assemblies/features/standard/src/main/feature/feature.xml
+++ b/assemblies/features/standard/src/main/feature/feature.xml
@@ -1284,6 +1284,44 @@
     </feature>
 
     <feature name="scheduler" description="Provide a scheduler service in Karaf to fire events" version="${project.version}">
+        <config name="org.apache.karaf.scheduler.quartz">
+            ################################################################################
+            #
+            #    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.
+            #
+            ################################################################################
+
+            #============================================================================
+            # Configure Karaf Scheduler Properties
+            #============================================================================
+            org.quartz.scheduler.instanceName=Karaf
+            org.quartz.scheduler.instanceId=AUTO
+
+            #============================================================================
+            # Configure ThreadPool
+            #============================================================================
+            org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
+            org.quartz.threadPool.threadCount=3
+            org.quartz.threadPool.threadPriority=5
+
+            #============================================================================
+            # Configure JobStore
+            #============================================================================
+            org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
+        </config>
         <bundle start-level="30">mvn:org.apache.karaf.scheduler/org.apache.karaf.scheduler.core/${project.version}</bundle>
     </feature>
 
diff --git a/scheduler/pom.xml b/scheduler/pom.xml
index beb7b15..fb145cb 100644
--- a/scheduler/pom.xml
+++ b/scheduler/pom.xml
@@ -69,6 +69,8 @@
                             !org.quartz.*,
                             !weblogic.*,
                             !javax.transaction,
+                            javax.servlet*;resolution:=optional,
+                            org.jboss.*;resolution:=optional,
                             *
                         </Import-Package>
                         <Private-Package>
@@ -81,6 +83,7 @@
                             org.quartz.spi.*,
                             org.quartz.simpl.*,
                             org.quartz.utils.*,
+                            org.quartz.ee.*
                         </Private-Package>
                     </instructions>
                 </configuration>
@@ -95,6 +98,11 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <scope>provided</scope>
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java b/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java
index 8271609..1e66c19 100644
--- a/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java
@@ -18,24 +18,46 @@ package org.apache.karaf.scheduler.core;
 
 import org.apache.karaf.scheduler.Scheduler;
 import org.apache.karaf.util.tracker.BaseActivator;
+import org.apache.karaf.util.tracker.annotation.Managed;
 import org.apache.karaf.util.tracker.annotation.ProvideService;
 import org.apache.karaf.util.tracker.annotation.Services;
+import org.osgi.service.cm.ManagedService;
+import org.quartz.impl.jdbcjobstore.JobStoreSupport;
+import org.quartz.impl.jdbcjobstore.JobStoreTX;
+import org.quartz.impl.jdbcjobstore.StdJDBCDelegate;
+import org.quartz.simpl.RAMJobStore;
 import org.quartz.simpl.SimpleThreadPool;
+import org.quartz.spi.JobStore;
 import org.quartz.spi.ThreadPool;
+import org.quartz.utils.ConnectionProvider;
+
+import java.util.Enumeration;
+import java.util.Properties;
 
 @Services(provides = @ProvideService(Scheduler.class))
-public class Activator extends BaseActivator {
+@Managed("org.apache.karaf.scheduler.quartz")
+public class Activator extends BaseActivator implements ManagedService {
 
-    private ThreadPool threadPool;
     private QuartzScheduler scheduler;
     private WhiteboardHandler whiteboardHandler;
 
     @Override
     protected void doStart() throws Exception {
-        threadPool = new SimpleThreadPool(4, Thread.NORM_PRIORITY);
-        scheduler = new QuartzScheduler(threadPool);
-        whiteboardHandler = new WhiteboardHandler(bundleContext, scheduler);
+        Properties properties = new Properties();
+        if (getConfiguration() == null) {
+            return;
+        }
+        Enumeration<String> keys = getConfiguration().keys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            if (key.startsWith("org.quartz")) {
+                Object value = getConfiguration().get(key);
+                properties.put(key, value);
+            }
+        }
+        scheduler = new QuartzScheduler(properties);
         register(Scheduler.class, scheduler);
+        whiteboardHandler = new WhiteboardHandler(bundleContext, scheduler);
 
         SchedulerMBeanImpl mBean = new SchedulerMBeanImpl();
         mBean.setScheduler(scheduler);
@@ -44,6 +66,8 @@ public class Activator extends BaseActivator {
 
     @Override
     protected void doStop() {
+        super.doStop();
+
         if (whiteboardHandler != null) {
             whiteboardHandler.deactivate();
             whiteboardHandler = null;
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 75c3950..3a08c30 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
@@ -16,11 +16,7 @@
  */
 package org.apache.karaf.scheduler.core;
 
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.UUID;
+import java.util.*;
 
 import org.apache.karaf.scheduler.Job;
 import org.apache.karaf.scheduler.ScheduleOptions;
@@ -28,8 +24,11 @@ import org.apache.karaf.scheduler.Scheduler;
 import org.apache.karaf.scheduler.SchedulerError;
 import org.quartz.*;
 import org.quartz.impl.DirectSchedulerFactory;
+import org.quartz.impl.StdSchedulerFactory;
 import org.quartz.impl.matchers.GroupMatcher;
+import org.quartz.simpl.CascadingClassLoadHelper;
 import org.quartz.simpl.RAMJobStore;
+import org.quartz.spi.JobStore;
 import org.quartz.spi.ThreadPool;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -45,8 +44,6 @@ public class QuartzScheduler implements Scheduler {
 
     private static final String PREFIX = "Apache Karaf Quartz Scheduler ";
 
-    private static final String QUARTZ_SCHEDULER_NAME = "ApacheKaraf";
-
     /** Map key for the job object */
     static final String DATA_MAP_OBJECT = "QuartzJobScheduler.Object";
 
@@ -62,30 +59,19 @@ public class QuartzScheduler implements Scheduler {
     /** The quartz scheduler. */
     private volatile org.quartz.Scheduler scheduler;
 
-    public QuartzScheduler(ThreadPool threadPool) throws SchedulerException {
+    public QuartzScheduler(Properties configuration) {
         // SLING-2261 Prevent Quartz from checking for updates
         System.setProperty("org.terracotta.quartz.skipUpdateCheck", Boolean.TRUE.toString());
-
-        final DirectSchedulerFactory factory = DirectSchedulerFactory.getInstance();
-        // unique run id
-        final String runID = new Date().toString().replace(' ', '_');
-        factory.createScheduler(QUARTZ_SCHEDULER_NAME, runID, threadPool, new RAMJobStore());
-        // quartz does not provide a way to get the scheduler by name AND runID, so we have to iterate!
-        final Iterator<org.quartz.Scheduler> allSchedulersIter = factory.getAllSchedulers().iterator();
-        while ( scheduler == null && allSchedulersIter.hasNext() ) {
-            final org.quartz.Scheduler current = allSchedulersIter.next();
-            if ( QUARTZ_SCHEDULER_NAME.equals(current.getSchedulerName())
-                    && runID.equals(current.getSchedulerInstanceId()) ) {
-                scheduler = current;
-            }
-        }
-        if ( scheduler == null ) {
-            throw new SchedulerException("Unable to find new scheduler with name " + QUARTZ_SCHEDULER_NAME + " and run ID " + runID);
-        }
-
-        scheduler.start();
-        if ( this.logger.isDebugEnabled() ) {
-            this.logger.debug(PREFIX + "started.");
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        try {
+            Thread.currentThread().setContextClassLoader(QuartzScheduler.class.getClassLoader());
+            StdSchedulerFactory factory = new StdSchedulerFactory(configuration);
+            scheduler = factory.getScheduler();
+            scheduler.start();
+        } catch (Throwable t) {
+            throw new RuntimeException("Unable to create quartz scheduler", t);
+        } finally {
+            Thread.currentThread().setContextClassLoader(cl);
         }
     }
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@karaf.apache.org" <co...@karaf.apache.org>'].