Commits

Anonymous committed d6296c3

QUARTZ-686: New config property: org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer

git-svn-id: http://svn.opensymphony.com/svn/quartz/trunk@82169f7d36a-ea1c-0410-88ea-9fd03e4c9665

  • Participants
  • Parent commits a4ec704

Comments (0)

Files changed (4)

File src/java/org/quartz/core/QuartzSchedulerResources.java

     
     private boolean makeSchedulerThreadDaemon = false;
 
+    private boolean threadsInheritInitializersClassLoadContext = false;
+
     private String rmiBindName;
     
     private boolean jmxExport;
     }
 
     /**
+     * Get whether to set the class load context of spawned threads to that
+     * of the initializing thread.
+     */
+    public boolean isThreadsInheritInitializersClassLoadContext() {
+		return threadsInheritInitializersClassLoadContext;
+	}
+
+    /**
+     * Set whether to set the class load context of spawned threads to that
+     * of the initializing thread.
+     */
+	public void setThreadsInheritInitializersClassLoadContext(
+			boolean threadsInheritInitializersClassLoadContext) {
+		this.threadsInheritInitializersClassLoadContext = threadsInheritInitializersClassLoadContext;
+	}
+
+	/**
      * Get the name under which to bind the QuartzScheduler in RMI.  Will 
      * return the value of the uniqueIdentifier property if explict RMI bind 
      * name was never set.

File src/java/org/quartz/core/QuartzSchedulerThread.java

         this.qsRsrcs = qsRsrcs;
         this.ctxt = ctxt;
         this.setDaemon(setDaemon);
+        if(qsRsrcs.isThreadsInheritInitializersClassLoadContext()) {
+        	log.info("QuartzSchedulerThread Inheriting ContextClassLoader of thread: " + Thread.currentThread().getName());
+        	this.setContextClassLoader(Thread.currentThread().getContextClassLoader());
+        }
+        
         this.setPriority(threadPrio);
 
         // start the underlying thread, but put this object into the 'paused'

File src/java/org/quartz/impl/StdSchedulerFactory.java

 
     public static final String PROP_SCHED_MAKE_SCHEDULER_THREAD_DAEMON = "org.quartz.scheduler.makeSchedulerThreadDaemon";
 
+    public static final String PROP_SCHED_SCHEDULER_THREADS_INHERIT_CONTEXT_CLASS_LOADER_OF_INITIALIZING_THREAD = "org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer";
+
     public static final String PROP_SCHED_CLASS_LOAD_HELPER_CLASS = "org.quartz.scheduler.classLoadHelper.class";
 
     public static final String PROP_SCHED_JOB_FACTORY_CLASS = "org.quartz.scheduler.jobFactory.class";
         boolean makeSchedulerThreadDaemon = 
             cfg.getBooleanProperty(PROP_SCHED_MAKE_SCHEDULER_THREAD_DAEMON);
         
+        boolean threadsInheritInitalizersClassLoader =
+        	cfg.getBooleanProperty(PROP_SCHED_SCHEDULER_THREADS_INHERIT_CONTEXT_CLASS_LOADER_OF_INITIALIZING_THREAD);
+        
         boolean jmxExport = cfg.getBooleanProperty(PROP_SCHED_JMX_EXPORT);
         boolean jmxProxy = cfg.getBooleanProperty(PROP_SCHED_JMX_PROXY);
         String jmxProxyClass = cfg.getStringProperty(PROP_SCHED_JMX_PROXY_CLASS);
             JobStoreSupport jjs = (JobStoreSupport)js;
             jjs.setInstanceId(schedInstId);
             jjs.setDbRetryInterval(dbFailureRetry);
+            if(threadsInheritInitalizersClassLoader)
+            	jjs.setThreadsInheritInitializersClassLoadContext(threadsInheritInitalizersClassLoader);
         }
         
         QuartzSchedulerResources rsrcs = new QuartzSchedulerResources();
         rsrcs.setInstanceId(schedInstId);
         rsrcs.setJobRunShellFactory(jrsf);
         rsrcs.setMakeSchedulerThreadDaemon(makeSchedulerThreadDaemon);
+        rsrcs.setThreadsInheritInitializersClassLoadContext(threadsInheritInitalizersClassLoader);
         rsrcs.setJMXExport(jmxExport);
         rsrcs.setJMXObjectName(jmxObjectName);
         	
         rsrcs.setThreadPool(tp);
         if(tp instanceof SimpleThreadPool) {
             ((SimpleThreadPool)tp).setThreadNamePrefix(schedName + "_Worker");
+            if(threadsInheritInitalizersClassLoader)
+            	((SimpleThreadPool)tp).setThreadsInheritContextClassLoaderOfInitializingThread(threadsInheritInitalizersClassLoader);
         }
         tp.initialize();
         

File src/java/org/quartz/impl/jdbcjobstore/JobStoreSupport.java

     private long dbRetryInterval = 10000;
     
     private boolean makeThreadsDaemons = false;
+
+    private boolean threadsInheritInitializersClassLoadContext = false;
+    private ClassLoader initializersLoader = null;
     
     private boolean doubleCheckLockMisfireHandler = true;
     
     }
     
     /**
+     * Get whether to set the class load context of spawned threads to that
+     * of the initializing thread.
+     */
+    public boolean isThreadsInheritInitializersClassLoadContext() {
+		return threadsInheritInitializersClassLoadContext;
+	}
+
+    /**
+     * Set whether to set the class load context of spawned threads to that
+     * of the initializing thread.
+     */
+	public void setThreadsInheritInitializersClassLoadContext(
+			boolean threadsInheritInitializersClassLoadContext) {
+		this.threadsInheritInitializersClassLoadContext = threadsInheritInitializersClassLoadContext;
+	}
+
+	/**
      * Get whether to check to see if there are Triggers that have misfired
      * before actually acquiring the lock to recover them.  This should be 
      * set to false if the majority of the time, there are are misfired
         }
 
         classLoadHelper = loadHelper;
+        if(isThreadsInheritInitializersClassLoadContext()) {
+        	log.info("JDBCJobStore threads will inherit ContextClassLoader of thread: " + Thread.currentThread().getName());
+        	initializersLoader = Thread.currentThread().getContextClassLoader();
+        }
+        
         this.signaler = signaler;
 
         // If the user hasn't specified an explicit lock handler, then 
 
         if (isClustered()) {
             clusterManagementThread = new ClusterManager();
+            if(initializersLoader != null)
+            	clusterManagementThread.setContextClassLoader(initializersLoader);
             clusterManagementThread.initialize();
         } else {
             try {
         }
 
         misfireHandler = new MisfireHandler();
+        if(initializersLoader != null)
+        	misfireHandler.setContextClassLoader(initializersLoader);
         misfireHandler.initialize();
     }