All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.quartz.impl.RemoteMBeanScheduler Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/* 
 * Copyright 2001-2009 Terracotta, Inc. 
 * 
 * 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.impl;

import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;

import org.quartz.Calendar;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.ListenerManager;
import org.quartz.Scheduler;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.quartz.SchedulerMetaData;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.UnableToInterruptJobException;
import org.quartz.Trigger.TriggerState;
import org.quartz.core.jmx.JobDetailSupport;
import org.quartz.core.jmx.TriggerSupport;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.impl.matchers.StringMatcher;
import org.quartz.spi.JobFactory;

/**
 * 

* An implementation of the Scheduler interface that remotely * proxies all method calls to the equivalent call on a given QuartzScheduler * instance, via JMX. *

* *

* A user must create a subclass to implement the actual connection to the remote * MBeanServer using their application specific connector. *

* @see org.quartz.Scheduler * @see org.quartz.core.QuartzScheduler */ public abstract class RemoteMBeanScheduler implements Scheduler { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Data members. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private ObjectName schedulerObjectName; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constructors. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ public RemoteMBeanScheduler() { } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Properties. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * Get the name under which the Scheduler MBean is registered on the * remote MBean server. */ protected ObjectName getSchedulerObjectName() { return schedulerObjectName; } /** * Set the name under which the Scheduler MBean is registered on the * remote MBean server. */ public void setSchedulerObjectName(String schedulerObjectName) throws SchedulerException { try { this.schedulerObjectName = new ObjectName(schedulerObjectName); } catch (MalformedObjectNameException e) { throw new SchedulerException("Failed to parse Scheduler MBean name: " + schedulerObjectName, e); } } /** * Set the name under which the Scheduler MBean is registered on the * remote MBean server. */ public void setSchedulerObjectName(ObjectName schedulerObjectName) throws SchedulerException { this.schedulerObjectName = schedulerObjectName; } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Abstract methods. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * Initialize this RemoteMBeanScheduler instance, connecting to the * remote MBean server. */ public abstract void initialize() throws SchedulerException; /** * Get the given attribute of the remote Scheduler MBean. */ protected abstract Object getAttribute( String attribute) throws SchedulerException; /** * Get the given attributes of the remote Scheduler MBean. */ protected abstract AttributeList getAttributes(String[] attributes) throws SchedulerException; /** * Invoke the given operation on the remote Scheduler MBean. */ protected abstract Object invoke( String operationName, Object[] params, String[] signature) throws SchedulerException; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** *

* Returns the name of the Scheduler. *

*/ public String getSchedulerName() throws SchedulerException { return (String)getAttribute("SchedulerName"); } /** *

* Returns the instance Id of the Scheduler. *

*/ public String getSchedulerInstanceId() throws SchedulerException { return (String)getAttribute("SchedulerInstanceId"); } public SchedulerMetaData getMetaData() throws SchedulerException { AttributeList attributeList = getAttributes( new String[] { "SchedulerName", "SchedulerInstanceId", "StandbyMode", "Shutdown", "JobStoreClassName", "ThreadPoolClassName", "ThreadPoolSize", "Version", "PerformanceMetrics" }); try { return new SchedulerMetaData( (String)getAttribute(attributeList, 0).getValue(), (String)getAttribute(attributeList, 1).getValue(), getClass(), true, false, (Boolean)getAttribute(attributeList, 2).getValue(), (Boolean)getAttribute(attributeList, 3).getValue(), null, Integer.parseInt(((Map)getAttribute(attributeList, 8).getValue()).get("JobsExecuted").toString()), Class.forName((String)getAttribute(attributeList, 4).getValue()), false, false, Class.forName((String)getAttribute(attributeList, 5).getValue()), (Integer)getAttribute(attributeList, 6).getValue(), (String)getAttribute(attributeList, 7).getValue()); } catch (ClassNotFoundException e) { throw new SchedulerException(e); } } private Attribute getAttribute(AttributeList attributeList, int index) { return (Attribute)attributeList.get(index); } /** *

* Returns the SchedulerContext of the Scheduler. *

*/ public SchedulerContext getContext() throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /////////////////////////////////////////////////////////////////////////// /// /// Schedululer State Management Methods /// /////////////////////////////////////////////////////////////////////////// /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public void start() throws SchedulerException { invoke("start", new Object[] {}, new String[] {}); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public void startDelayed(int seconds) throws SchedulerException { invoke("startDelayed", new Object[] {seconds}, new String[] {int.class.getName()}); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public void standby() throws SchedulerException { invoke("standby", new Object[] {}, new String[] {}); } /** * Whether the scheduler has been started. * *

* Note: This only reflects whether {@link #start()} has ever * been called on this Scheduler, so it will return true even * if the Scheduler is currently in standby mode or has been * since shutdown. *

* * @see #start() * @see #isShutdown() * @see #isInStandbyMode() */ public boolean isStarted() throws SchedulerException { return (Boolean) getAttribute("Started"); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public boolean isInStandbyMode() throws SchedulerException { return (Boolean)getAttribute("StandbyMode"); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public void shutdown() throws SchedulerException { // Have to get the scheduler name before we actually call shutdown. String schedulerName = getSchedulerName(); invoke("shutdown", new Object[] {}, new String[] {}); SchedulerRepository.getInstance().remove(schedulerName); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public void shutdown(boolean waitForJobsToComplete) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public boolean isShutdown() throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ @SuppressWarnings("unchecked") public List getCurrentlyExecutingJobs() throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /////////////////////////////////////////////////////////////////////////// /// /// Scheduling-related Methods /// /////////////////////////////////////////////////////////////////////////// /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public Date scheduleJob(JobDetail jobDetail, Trigger trigger) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public Date scheduleJob(Trigger trigger) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void addJob(JobDetail jobDetail, boolean replace) throws SchedulerException { invoke( "addJob", new Object[] { JobDetailSupport.toCompositeData(jobDetail), replace }, new String[] { CompositeData.class.getName(), boolean.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void addJob(JobDetail jobDetail, boolean replace, boolean storeNonDurableWhileAwaitingScheduling) throws SchedulerException { invoke( "addJob", new Object[] { JobDetailSupport.toCompositeData(jobDetail), replace , storeNonDurableWhileAwaitingScheduling}, new String[] { CompositeData.class.getName(), boolean.class.getName(), boolean.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public boolean deleteJob(JobKey jobKey) throws SchedulerException { return (Boolean)invoke( "deleteJob", new Object[] { jobKey.getName(), jobKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public boolean unscheduleJob(TriggerKey triggerKey) throws SchedulerException { return (Boolean)invoke( "unscheduleJob", new Object[] { triggerKey.getName(), triggerKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() }); } public boolean deleteJobs(List jobKeys) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } public void scheduleJobs(Map> triggersAndJobs, boolean replace) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } public void scheduleJob(JobDetail jobDetail, Set triggersForJob, boolean replace) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } public boolean unscheduleJobs(List triggerKeys) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public Date rescheduleJob(TriggerKey triggerKey, Trigger newTrigger) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void triggerJob(JobKey jobKey) throws SchedulerException { triggerJob(jobKey, null); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void triggerJob(JobKey jobKey, JobDataMap data) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void pauseTrigger(TriggerKey triggerKey) throws SchedulerException { invoke( "pauseTrigger", new Object[] { triggerKey.getName(), triggerKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void pauseTriggers(GroupMatcher matcher) throws SchedulerException { String operation = null; switch (matcher.getCompareWithOperator()) { case EQUALS: operation = "pauseTriggerGroup"; break; case CONTAINS: operation = "pauseTriggersContaining"; break; case STARTS_WITH: operation = "pauseTriggersStartingWith"; break; case ENDS_WITH: operation = "pauseTriggersEndingWith"; } if (operation != null) { invoke( operation, new Object[] { matcher.getCompareToValue() }, new String[] { String.class.getName() }); } else { throw new SchedulerException("Unsupported GroupMatcher kind for pausing triggers: " + matcher.getCompareWithOperator()); } } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void pauseJob(JobKey jobKey) throws SchedulerException { invoke( "pauseJob", new Object[] { jobKey.getName(), jobKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void pauseJobs(GroupMatcher matcher) throws SchedulerException { String operation = null; switch (matcher.getCompareWithOperator()) { case EQUALS: operation = "pauseJobGroup"; break; case STARTS_WITH: operation = "pauseJobsStartingWith"; break; case ENDS_WITH: operation = "pauseJobsEndingWith"; break; case CONTAINS: operation = "pauseJobsContaining"; } invoke( operation, new Object[] { matcher.getCompareToValue() }, new String[] { String.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void resumeTrigger(TriggerKey triggerKey) throws SchedulerException { invoke( "resumeTrigger", new Object[] { triggerKey.getName(), triggerKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void resumeTriggers(GroupMatcher matcher) throws SchedulerException { String operation = null; switch (matcher.getCompareWithOperator()) { case EQUALS: operation = "resumeTriggerGroup"; break; case CONTAINS: operation = "resumeTriggersContaining"; break; case STARTS_WITH: operation = "resumeTriggersStartingWith"; break; case ENDS_WITH: operation = "resumeTriggersEndingWith"; } if (operation != null) { invoke( operation, new Object[] { matcher.getCompareToValue() }, new String[] { String.class.getName() }); } else { throw new SchedulerException("Unsupported GroupMatcher kind for resuming triggers: " + matcher.getCompareWithOperator()); } } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void resumeJob(JobKey jobKey) throws SchedulerException { invoke( "resumeJob", new Object[] { jobKey.getName(), jobKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void resumeJobs(GroupMatcher matcher) throws SchedulerException { String operation = null; switch (matcher.getCompareWithOperator()) { case EQUALS: operation = "resumeJobGroup"; break; case STARTS_WITH: operation = "resumeJobsStartingWith"; break; case ENDS_WITH: operation = "resumeJobsEndingWith"; break; case CONTAINS: operation = "resumeJobsContaining"; } invoke( operation, new Object[] { matcher.getCompareToValue() }, new String[] { String.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void pauseAll() throws SchedulerException { invoke( "pauseAllTriggers", new Object[] { }, new String[] { }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void resumeAll() throws SchedulerException { invoke( "resumeAllTriggers", new Object[] { }, new String[] { }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ @SuppressWarnings("unchecked") public List getJobGroupNames() throws SchedulerException { return (List)getAttribute("JobGroupNames"); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ @SuppressWarnings("unchecked") public Set getJobKeys(GroupMatcher matcher) throws SchedulerException { if (matcher.getCompareWithOperator().equals(StringMatcher.StringOperatorName.EQUALS)) { List keys = (List)invoke( "getJobNames", new Object[] { matcher.getCompareToValue() }, new String[] { String.class.getName() }); return new HashSet(keys); } else { throw new SchedulerException("Only equals matcher are supported for looking up JobKeys"); } } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ @SuppressWarnings("unchecked") public List getTriggersOfJob(JobKey jobKey) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ @SuppressWarnings("unchecked") public List getTriggerGroupNames() throws SchedulerException { return (List)getAttribute("TriggerGroupNames"); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ @SuppressWarnings("unchecked") public Set getTriggerKeys(GroupMatcher matcher) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public JobDetail getJobDetail(JobKey jobKey) throws SchedulerException { try { return JobDetailSupport.newJobDetail((CompositeData)invoke( "getJobDetail", new Object[] { jobKey.getName(), jobKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() })); } catch (ClassNotFoundException e) { throw new SchedulerException("Unable to resolve job class", e); } } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public Trigger getTrigger(TriggerKey triggerKey) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public boolean checkExists(JobKey jobKey) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public boolean checkExists(TriggerKey triggerKey) throws SchedulerException { return (Boolean)invoke( "checkExists", new Object[] { triggerKey }, new String[] { TriggerKey.class.getName() }); } public void clear() throws SchedulerException { invoke( "clear", new Object[] { }, new String[] { }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public TriggerState getTriggerState(TriggerKey triggerKey) throws SchedulerException { return TriggerState.valueOf((String)invoke( "getTriggerState", new Object[] { triggerKey.getName(), triggerKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() })); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public void addCalendar(String calName, Calendar calendar, boolean replace, boolean updateTriggers) throws SchedulerException { invoke( "addCalendar", new Object[] { calName, calendar, replace, updateTriggers }, new String[] { String.class.getName(), Calendar.class.getName(), boolean.class.getName(), boolean.class.getName() }); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public boolean deleteCalendar(String calName) throws SchedulerException { invoke("deleteCalendar", new Object[] { calName }, new String[] { String.class.getName() }); return true; } /** *

* Calls th0e equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ public Calendar getCalendar(String calName) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler, * passing the SchedulingContext associated with this * instance. *

*/ @SuppressWarnings("unchecked") public List getCalendarNames() throws SchedulerException { return (List)getAttribute("CalendarNames"); } /** * @see org.quartz.Scheduler#getPausedTriggerGroups() */ @SuppressWarnings("unchecked") public Set getPausedTriggerGroups() throws SchedulerException { return (Set)getAttribute("PausedTriggerGroups"); } /////////////////////////////////////////////////////////////////////////// /// /// Other Methods /// /////////////////////////////////////////////////////////////////////////// /** *

* Calls the equivalent method on the 'proxied' QuartzScheduler. *

*/ public ListenerManager getListenerManager() throws SchedulerException { throw new SchedulerException( "Operation not supported for remote schedulers."); } /** * @see org.quartz.Scheduler#interrupt(JobKey) */ public boolean interrupt(JobKey jobKey) throws UnableToInterruptJobException { try { return (Boolean)invoke( "interruptJob", new Object[] { jobKey.getName(), jobKey.getGroup() }, new String[] { String.class.getName(), String.class.getName() }); } catch (SchedulerException se) { throw new UnableToInterruptJobException(se); } } public boolean interrupt(String fireInstanceId) throws UnableToInterruptJobException { try { return (Boolean)invoke( "interruptJob", new Object[] { fireInstanceId }, new String[] { String.class.getName() }); } catch (SchedulerException se) { throw new UnableToInterruptJobException(se); } } /** * @see org.quartz.Scheduler#setJobFactory(org.quartz.spi.JobFactory) */ public void setJobFactory(JobFactory factory) throws SchedulerException { throw new SchedulerException("Operation not supported for remote schedulers."); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy