📄 remotescheduler.java
字号:
/* * Copyright James House (c) 2001-2004 * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.quartz.impl;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import java.util.Date;import java.util.List;import java.util.Set;import org.quartz.Calendar;import org.quartz.JobDetail;import org.quartz.JobListener;import org.quartz.Scheduler;import org.quartz.SchedulerContext;import org.quartz.SchedulerException;import org.quartz.SchedulerListener;import org.quartz.SchedulerMetaData;import org.quartz.Trigger;import org.quartz.TriggerListener;import org.quartz.UnableToInterruptJobException;import org.quartz.core.RemotableQuartzScheduler;import org.quartz.core.SchedulingContext;/** * <p> * An implementation of the <code>Scheduler</code> interface that remotely * proxies all method calls to the equivalent call on a given <code>QuartzScheduler</code> * instance, via RMI. * </p> * * @see org.quartz.Scheduler * @see org.quartz.core.QuartzScheduler * @see org.quartz.core.SchedulingContext * * @author James House */public class RemoteScheduler implements Scheduler { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Data members. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private RemotableQuartzScheduler rsched; private SchedulingContext schedCtxt; private String schedId; private String rmiHost; private int rmiPort; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constructors. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * <p> * Construct a <code>RemoteScheduler</code> instance to proxy the given * <code>RemoteableQuartzScheduler</code> instance, and with the given * <code>SchedulingContext</code>. * </p> */ public RemoteScheduler(SchedulingContext schedCtxt, String schedId, String host, int port) { this.schedCtxt = schedCtxt; this.schedId = schedId; this.rmiHost = host; this.rmiPort = port; } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ protected RemotableQuartzScheduler getRemoteScheduler() throws SchedulerException { if (rsched != null) return rsched; try { Registry registry = LocateRegistry.getRegistry(rmiHost, rmiPort); rsched = (RemotableQuartzScheduler) registry.lookup(schedId); } catch (Exception e) { SchedulerException initException = new SchedulerException( "Could not get handle to remote scheduler: " + e.getMessage(), e); initException .setErrorCode(SchedulerException.ERR_COMMUNICATION_FAILURE); throw initException; } return rsched; } protected SchedulerException invalidateHandleCreateException(String msg, Exception cause) { rsched = null; SchedulerException ex = new SchedulerException(msg, cause); ex.setErrorCode(SchedulerException.ERR_COMMUNICATION_FAILURE); return ex; } /** * <p> * Returns the name of the <code>Scheduler</code>. * </p> */ public String getSchedulerName() throws SchedulerException { try { return getRemoteScheduler().getSchedulerName(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Returns the instance Id of the <code>Scheduler</code>. * </p> */ public String getSchedulerInstanceId() throws SchedulerException { try { return getRemoteScheduler().getSchedulerInstanceId(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } public SchedulerMetaData getMetaData() throws SchedulerException { try { RemotableQuartzScheduler sched = getRemoteScheduler(); return new SchedulerMetaData(getSchedulerName(), getSchedulerInstanceId(), getClass(), true, sched .runningSince() != null, isPaused(), isShutdown(), sched.runningSince(), sched.numJobsExecuted(), sched .getJobStoreClass(), sched.supportsPersistence(), sched.getThreadPoolClass(), sched.getThreadPoolSize(), sched.getVersion()); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Returns the <code>SchedulerContext</code> of the <code>Scheduler</code>. * </p> */ public SchedulerContext getContext() throws SchedulerException { try { return getRemoteScheduler().getSchedulerContext(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /////////////////////////////////////////////////////////////////////////// /// /// Schedululer State Management Methods /// /////////////////////////////////////////////////////////////////////////// /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>. * </p> */ public void start() throws SchedulerException { try { getRemoteScheduler().start(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>. * </p> */ public void pause() throws SchedulerException { try { getRemoteScheduler().pause(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>. * </p> */ public boolean isPaused() throws SchedulerException { try { return getRemoteScheduler().isPaused(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>. * </p> */ public void shutdown() throws SchedulerException { try { getRemoteScheduler().shutdown(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>. * </p> */ public void shutdown(boolean waitForJobsToComplete) throws SchedulerException { try { getRemoteScheduler().shutdown(waitForJobsToComplete); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>. * </p> */ public boolean isShutdown() throws SchedulerException { try { return getRemoteScheduler().isShutdown(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>. * </p> */ public List getCurrentlyExecutingJobs() throws SchedulerException { try { return getRemoteScheduler().getCurrentlyExecutingJobs(); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /////////////////////////////////////////////////////////////////////////// /// /// Scheduling-related Methods /// /////////////////////////////////////////////////////////////////////////// /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>, * passing the <code>SchedulingContext</code> associated with this * instance. * </p> */ public Date scheduleJob(JobDetail jobDetail, Trigger trigger) throws SchedulerException { try { return getRemoteScheduler().scheduleJob(schedCtxt, jobDetail, trigger); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>, * passing the <code>SchedulingContext</code> associated with this * instance. * </p> */ public Date scheduleJob(Trigger trigger) throws SchedulerException { try { return getRemoteScheduler().scheduleJob(schedCtxt, trigger); } catch (RemoteException re) { throw invalidateHandleCreateException( "Error communicating with remote scheduler.", re); } } /** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -