Commits

Anonymous committed 6468adb

Issue number: QUARTZ-256
Obtained from:
Submitted by:
Reviewed by:
CVS: ----------------------------------------------------------------------
CVS: Issue number:
CVS: If this change addresses one or more issues,
CVS: then enter the issue number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to the project by someone else; i.e.,
CVS: they sent us a patch or a set of diffs, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.

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

Comments (0)

Files changed (4)

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

 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Method;
-import java.net.InetAddress;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Iterator;
 import org.quartz.simpl.RAMJobStore;
 import org.quartz.simpl.SimpleThreadPool;
 import org.quartz.spi.ClassLoadHelper;
+import org.quartz.spi.InstanceIdGenerator;
 import org.quartz.spi.JobFactory;
 import org.quartz.spi.JobStore;
 import org.quartz.spi.SchedulerPlugin;
 
     public static final String PROP_SCHED_INSTANCE_ID = "org.quartz.scheduler.instanceId";
 
+    public static final String PROP_SCHED_INSTANCE_ID_GENERATOR_CLASS = "org.quartz.scheduler.instanceIdGenerator.class";
+    
     public static final String PROP_SCHED_THREAD_NAME = "org.quartz.scheduler.threadName";
     
     public static final String PROP_SCHED_RMI_EXPORT = "org.quartz.scheduler.rmi.export";
     /**
      *  
      */
-private Scheduler instantiate() throws SchedulerException {
+    private Scheduler instantiate() throws SchedulerException {
         if (cfg == null) initialize();
 
         if (initException != null) throw initException;
         QuartzScheduler qs = null;
         SchedulingContext schedCtxt = null;
         DBConnectionManager dbMgr = null;
+        String instanceIdGeneratorClass = null;
         Properties tProps = null;
         String userTXLocation = null;
         boolean wrapJobInTx = false;
         String schedInstId = cfg.getStringProperty(PROP_SCHED_INSTANCE_ID,
                 DEFAULT_INSTANCE_ID);
 
-        if (schedInstId.equals(AUTO_GENERATE_INSTANCE_ID)) autoId = true;
-
+        if (schedInstId.equals(AUTO_GENERATE_INSTANCE_ID)) {
+            autoId = true;
+            instanceIdGeneratorClass = cfg.getStringProperty(
+                    PROP_SCHED_INSTANCE_ID_GENERATOR_CLASS,
+                    "org.quartz.simpl.SimpleInstanceIdGenerator");
+        }
+        
         userTXLocation = cfg.getStringProperty(PROP_SCHED_USER_TX_URL,
                 userTXLocation);
         if (userTXLocation != null && userTXLocation.trim().length() == 0)
             }
         }        
         
+        InstanceIdGenerator instanceIdGenerator = null;
+        if(instanceIdGeneratorClass != null) {
+            try {
+                instanceIdGenerator = (InstanceIdGenerator) loadHelper.loadClass(instanceIdGeneratorClass)
+                    .newInstance();
+            } catch (Exception e) {
+                throw new SchedulerConfigException(
+                        "Unable to instantiate InstanceIdGenerator class: "
+                        + e.getMessage(), e);
+            }
+        }               
+        
         // Get ThreadPool Properties
         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
                 if (js instanceof org.quartz.impl.jdbcjobstore.JobStoreSupport) {
                     if(((org.quartz.impl.jdbcjobstore.JobStoreSupport) js) 
                         .isClustered()) {
-                        schedInstId = InetAddress.getLocalHost().getHostName()
-                            + System.currentTimeMillis();
+                        schedInstId = instanceIdGenerator.generateInstanceId();                    
                     }
                 }
             } catch (Exception e) {
-                getLog().error("Couldn't get host name!", e);
+                getLog().error("Couldn't generate instance Id!", e);
                 throw new IllegalStateException(
                         "Cannot run without an instance id.");
             }

src/java/org/quartz/simpl/HostnameInstanceIdGenerator.java

+/* 
+ * Copyright 2004-2005 OpenSymphony 
+ * 
+ * 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.quartz.simpl;
+
+import java.net.InetAddress;
+
+import org.quartz.SchedulerException;
+import org.quartz.spi.InstanceIdGenerator;
+
+/**
+ * <p>
+ * <code>InstanceIdGenerator</code> that names the scheduler instance using 
+ * just the machine hostname.
+ * </p>
+ * 
+ * <p>
+ * This class is useful when you know that your scheduler instance will be the 
+ * only one running on a particular machine.  Each time the scheduler is 
+ * restarted, it will get the same instance id as long as the machine is not 
+ * renamed.
+ * </p>
+ * 
+ * @see InstanceIdGenerator
+ * @see SimpleInstanceIdGenerator
+ */
+public class HostnameInstanceIdGenerator implements InstanceIdGenerator
+{
+    public String generateInstanceId() throws SchedulerException {
+        try {
+            return InetAddress.getLocalHost().getHostName();
+        }
+        catch (Exception e) {
+            throw new SchedulerException("Couldn't get host name!", e);
+        }
+    }
+}

src/java/org/quartz/simpl/SimpleInstanceIdGenerator.java

+/* 
+ * Copyright 2004-2005 OpenSymphony 
+ * 
+ * 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.quartz.simpl;
+
+import java.net.InetAddress;
+
+import org.quartz.SchedulerException;
+import org.quartz.spi.InstanceIdGenerator;
+
+/**
+ * The default InstanceIdGenerator used by Quartz when instance id is to be
+ * automatically generated.  Instance id is of the form HOSTNAME + CURRENT_TIME.
+ * 
+ * @see InstanceIdGenerator
+ * @see HostnameInstanceIdGenerator
+ */
+public class SimpleInstanceIdGenerator implements InstanceIdGenerator
+{
+    public String generateInstanceId() throws SchedulerException {
+        try {
+            return InetAddress.getLocalHost().getHostName() + System.currentTimeMillis();
+        }
+        catch (Exception e) {
+            throw new SchedulerException("Couldn't get host name!", e);
+        }
+    }
+}

src/java/org/quartz/spi/InstanceIdGenerator.java

+/* 
+ * Copyright 2004-2005 OpenSymphony 
+ * 
+ * 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.quartz.spi;
+
+import org.quartz.SchedulerException;
+
+/**
+ * <p>
+ * An InstanceIdGenerator is responsible for generating the clusterwide unique 
+ * instance id for a <code>Scheduler</code> nodde.
+ * </p>
+ * 
+ * <p>
+ * This interface may be of use to those wishing to have specific control over 
+ * the mechanism by which the <code>Scheduler</code> instances in their 
+ * application are named.
+ * </p>
+ * 
+ * @see org.quartz.simpl.SimpleInstanceIdGenerator
+ */
+public interface InstanceIdGenerator
+{
+    /**
+     * Generate the instance id for a <code>Scheduler</code>
+     * 
+     * @return The clusterwide unique instance id.
+     */
+    String generateInstanceId() throws SchedulerException;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.