下面列出了怎么用org.quartz.JobListener的API类实例代码及写法,或者点击链接到github查看源代码。
public void addJobListener(JobListener jobListener, List<Matcher<JobKey>> matchers) {
if (jobListener.getName() == null || jobListener.getName().length() == 0) {
throw new IllegalArgumentException(
"JobListener name cannot be empty.");
}
synchronized (globalJobListeners) {
globalJobListeners.put(jobListener.getName(), jobListener);
LinkedList<Matcher<JobKey>> matchersL = new LinkedList<Matcher<JobKey>>();
if(matchers != null && matchers.size() > 0)
matchersL.addAll(matchers);
else
matchersL.add(EverythingMatcher.allJobs());
globalJobListenersMatchers.put(jobListener.getName(), matchersL);
}
}
public void addJobListener(JobListener jobListener, Matcher<JobKey> matcher) {
if (jobListener.getName() == null || jobListener.getName().length() == 0) {
throw new IllegalArgumentException(
"JobListener name cannot be empty.");
}
synchronized (globalJobListeners) {
globalJobListeners.put(jobListener.getName(), jobListener);
LinkedList<Matcher<JobKey>> matchersL = new LinkedList<Matcher<JobKey>>();
if(matcher != null)
matchersL.add(matcher);
else
matchersL.add(EverythingMatcher.allJobs());
globalJobListenersMatchers.put(jobListener.getName(), matchersL);
}
}
public void notifyJobListenersToBeExecuted(JobExecutionContext jec)
throws SchedulerException {
// build a list of all job listeners that are to be notified...
List<JobListener> jobListeners = buildJobListenerList();
// notify all job listeners
for(JobListener jl: jobListeners) {
try {
if(!matchJobListener(jl, jec.getJobDetail().getKey()))
continue;
jl.jobToBeExecuted(jec);
} catch (Exception e) {
SchedulerException se = new SchedulerException(
"JobListener '" + jl.getName() + "' threw exception: "
+ e.getMessage(), e);
throw se;
}
}
}
public void notifyJobListenersWasVetoed(JobExecutionContext jec)
throws SchedulerException {
// build a list of all job listeners that are to be notified...
List<JobListener> jobListeners = buildJobListenerList();
// notify all job listeners
for(JobListener jl: jobListeners) {
try {
if(!matchJobListener(jl, jec.getJobDetail().getKey()))
continue;
jl.jobExecutionVetoed(jec);
} catch (Exception e) {
SchedulerException se = new SchedulerException(
"JobListener '" + jl.getName() + "' threw exception: "
+ e.getMessage(), e);
throw se;
}
}
}
public void notifyJobListenersWasExecuted(JobExecutionContext jec,
JobExecutionException je) throws SchedulerException {
// build a list of all job listeners that are to be notified...
List<JobListener> jobListeners = buildJobListenerList();
// notify all job listeners
for(JobListener jl: jobListeners) {
try {
if(!matchJobListener(jl, jec.getJobDetail().getKey()))
continue;
jl.jobWasExecuted(jec, je);
} catch (Exception e) {
SchedulerException se = new SchedulerException(
"JobListener '" + jl.getName() + "' threw exception: "
+ e.getMessage(), e);
throw se;
}
}
}
private List buildJobListenerList(String[] additionalLstnrs)
throws SchedulerException {
List jobListeners = getGlobalJobListeners();
for (int i = 0; i < additionalLstnrs.length; i++) {
JobListener jl = getJobListener(additionalLstnrs[i]);
if (jl != null) {
jobListeners.add(jl);
} else {
throw new SchedulerException("JobListener '"
+ additionalLstnrs[i] + "' not found.",
SchedulerException.ERR_JOB_LISTENER_NOT_FOUND);
}
}
return jobListeners;
}
public void notifyJobListenersToBeExecuted(JobExecutionContext jec)
throws SchedulerException {
// build a list of all job listeners that are to be notified...
List jobListeners = buildJobListenerList(jec.getJobDetail()
.getJobListenerNames());
// notify all job listeners
java.util.Iterator itr = jobListeners.iterator();
while (itr.hasNext()) {
JobListener jl = (JobListener) itr.next();
try {
jl.jobToBeExecuted(jec);
} catch (Exception e) {
SchedulerException se = new SchedulerException(
"JobListener '" + jl.getName() + "' threw exception: "
+ e.getMessage(), e);
se.setErrorCode(SchedulerException.ERR_JOB_LISTENER);
throw se;
}
}
}
public void notifyJobListenersWasVetoed(JobExecutionContext jec)
throws SchedulerException {
// build a list of all job listeners that are to be notified...
List jobListeners = buildJobListenerList(jec.getJobDetail()
.getJobListenerNames());
// notify all job listeners
java.util.Iterator itr = jobListeners.iterator();
while (itr.hasNext()) {
JobListener jl = (JobListener) itr.next();
try {
jl.jobExecutionVetoed(jec);
} catch (Exception e) {
SchedulerException se = new SchedulerException(
"JobListener '" + jl.getName() + "' threw exception: "
+ e.getMessage(), e);
se.setErrorCode(SchedulerException.ERR_JOB_LISTENER);
throw se;
}
}
}
public void notifyJobListenersWasExecuted(JobExecutionContext jec,
JobExecutionException je) throws SchedulerException {
// build a list of all job listeners that are to be notified...
List jobListeners = buildJobListenerList(jec.getJobDetail()
.getJobListenerNames());
// notify all job listeners
java.util.Iterator itr = jobListeners.iterator();
while (itr.hasNext()) {
JobListener jl = (JobListener) itr.next();
try {
jl.jobWasExecuted(jec, je);
} catch (Exception e) {
SchedulerException se = new SchedulerException(
"JobListener '" + jl.getName() + "' threw exception: "
+ e.getMessage(), e);
se.setErrorCode(SchedulerException.ERR_JOB_LISTENER);
throw se;
}
}
}
@SuppressWarnings("unchecked")
public void removeGlobalJobListener(Class<? extends JobListener> jobListenerClass)
throws SchedulerException {
for (final Scheduler scheduler : JobInformations.getAllSchedulers()) {
final List<JobListener> globalJobListeners = scheduler.getGlobalJobListeners();
for (final JobListener jobListener : new ArrayList<JobListener>(globalJobListeners)) {
if (jobListenerClass.isInstance(jobListener)) {
try {
scheduler.removeGlobalJobListener(jobListener);
} catch (final NoSuchMethodError e1) {
// pour Quartz 1.7, 1.8 et +,
// cette méthode n'existe pas avant Quartz 1.6
try {
final Class<? extends Scheduler> schedulerClass = scheduler.getClass();
schedulerClass.getMethod("removeGlobalJobListener", String.class)
.invoke(scheduler, jobListener.getName());
} catch (final Exception e2) {
throw new IllegalArgumentException(e2);
}
}
}
}
}
}
@Override
public void addGlobalJobListener(JobListener jobGlobalListener) throws SchedulerException {
final Scheduler defaultScheduler;
final List<Matcher<JobKey>> allJobs = new ArrayList<Matcher<JobKey>>();
allJobs.add(EverythingMatcher.allJobs());
if (Parameter.QUARTZ_DEFAULT_LISTENER_DISABLED.getValueAsBoolean()) {
defaultScheduler = null;
LOG.debug("Initialization of Quartz default listener has been disabled");
} else {
defaultScheduler = StdSchedulerFactory.getDefaultScheduler();
defaultScheduler.getListenerManager().addJobListener(jobGlobalListener, allJobs);
}
for (final Scheduler scheduler : JobInformations.getAllSchedulers()) {
if (scheduler != defaultScheduler) {
scheduler.getListenerManager().addJobListener(jobGlobalListener, allJobs);
}
}
}
/**
* Construct an instance with the given name.
*
* (Remember to add some delegate listeners!)
*
* @param name the name of this instance
*/
public BroadcastJobListener(String name) {
if(name == null) {
throw new IllegalArgumentException("Listener name cannot be null!");
}
this.name = name;
listeners = new LinkedList<JobListener>();
}
public boolean removeListener(String listenerName) {
Iterator<JobListener> itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = itr.next();
if(jl.getName().equals(listenerName)) {
itr.remove();
return true;
}
}
return false;
}
public void jobToBeExecuted(JobExecutionContext context) {
Iterator<JobListener> itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = itr.next();
jl.jobToBeExecuted(context);
}
}
public void jobExecutionVetoed(JobExecutionContext context) {
Iterator<JobListener> itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = itr.next();
jl.jobExecutionVetoed(context);
}
}
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
Iterator<JobListener> itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = itr.next();
jl.jobWasExecuted(context, jobException);
}
}
/**
* <p>
* Create a <code>QuartzScheduler</code> with the given configuration
* properties.
* </p>
*
* @see QuartzSchedulerResources
*/
public QuartzScheduler(QuartzSchedulerResources resources, long idleWaitTime, @Deprecated long dbRetryInterval)
throws SchedulerException {
this.resources = resources;
if (resources.getJobStore() instanceof JobListener) {
addInternalJobListener((JobListener)resources.getJobStore());
}
this.schedThread = new QuartzSchedulerThread(this, resources);
ThreadExecutor schedThreadExecutor = resources.getThreadExecutor();
schedThreadExecutor.execute(this.schedThread);
if (idleWaitTime > 0) {
this.schedThread.setIdleWaitTime(idleWaitTime);
}
jobMgr = new ExecutingJobsManager();
addInternalJobListener(jobMgr);
errLogger = new ErrorLogger();
addInternalSchedulerListener(errLogger);
signaler = new SchedulerSignalerImpl(this, this.schedThread);
if(shouldRunUpdateCheck())
updateTimer = scheduleUpdateCheck();
else
updateTimer = null;
getLog().info("Quartz Scheduler v." + getVersion() + " created.");
}
/**
* <p>
* Add the given <code>{@link org.quartz.JobListener}</code> to the
* <code>Scheduler</code>'s <i>internal</i> list.
* </p>
*/
public void addInternalJobListener(JobListener jobListener) {
if (jobListener.getName() == null
|| jobListener.getName().length() == 0) {
throw new IllegalArgumentException(
"JobListener name cannot be empty.");
}
synchronized (internalJobListeners) {
internalJobListeners.put(jobListener.getName(), jobListener);
}
}
private List<JobListener> buildJobListenerList()
throws SchedulerException {
List<JobListener> allListeners = new LinkedList<JobListener>();
allListeners.addAll(getListenerManager().getJobListeners());
allListeners.addAll(getInternalJobListeners());
return allListeners;
}
private boolean matchJobListener(JobListener listener, JobKey key) {
List<Matcher<JobKey>> matchers = getListenerManager().getJobListenerMatchers(listener.getName());
if(matchers == null)
return true;
for(Matcher<JobKey> matcher: matchers) {
if(matcher.isMatch(key))
return true;
}
return false;
}
/**
* <p>
* Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
* </p>
*/
public void addGlobalJobListener(JobListener jobListener)
throws SchedulerException {
throw new SchedulerException(
"Operation not supported for remote schedulers.",
SchedulerException.ERR_UNSUPPORTED_FUNCTION_IN_THIS_CONFIGURATION);
}
/**
* <p>
* Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
* </p>
*/
public void addJobListener(JobListener jobListener)
throws SchedulerException {
throw new SchedulerException(
"Operation not supported for remote schedulers.",
SchedulerException.ERR_UNSUPPORTED_FUNCTION_IN_THIS_CONFIGURATION);
}
/**
* <p>
* Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
* </p>
*/
public void addGlobalJobListener(JobListener jobListener)
throws SchedulerException {
throw new SchedulerException(
"Operation not supported for remote schedulers.",
SchedulerException.ERR_UNSUPPORTED_FUNCTION_IN_THIS_CONFIGURATION);
}
/**
* <p>
* Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
* </p>
*/
public void addJobListener(JobListener jobListener)
throws SchedulerException {
throw new SchedulerException(
"Operation not supported for remote schedulers.",
SchedulerException.ERR_UNSUPPORTED_FUNCTION_IN_THIS_CONFIGURATION);
}
public boolean removeListener(String listenerName) {
Iterator itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = (JobListener) itr.next();
if(jl.getName().equals(listenerName)) {
itr.remove();
return true;
}
}
return false;
}
public void jobToBeExecuted(JobExecutionContext context) {
if(!shouldDispatch(context)) {
return;
}
Iterator itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = (JobListener) itr.next();
jl.jobToBeExecuted(context);
}
}
public void jobExecutionVetoed(JobExecutionContext context) {
if(!shouldDispatch(context)) {
return;
}
Iterator itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = (JobListener) itr.next();
jl.jobExecutionVetoed(context);
}
}
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
if(!shouldDispatch(context)) {
return;
}
Iterator itr = listeners.iterator();
while(itr.hasNext()) {
JobListener jl = (JobListener) itr.next();
jl.jobWasExecuted(context, jobException);
}
}
/**
* <p>
* Add the given <code>{@link org.quartz.JobListener}</code> to the
* <code>Scheduler</code>'s<i>global</i> list.
* </p>
*
* <p>
* Listeners in the 'global' list receive notification of execution events
* for ALL <code>{@link org.quartz.Job}</code>s.
* </p>
*/
public void addGlobalJobListener(JobListener jobListener) {
if (jobListener.getName() == null
|| jobListener.getName().length() == 0) {
throw new IllegalArgumentException(
"JobListener name cannot be empty.");
}
synchronized (globalJobListeners) {
globalJobListeners.put(jobListener.getName(), jobListener);
}
}
/**
* <p>
* Add the given <code>{@link org.quartz.JobListener}</code> to the
* <code>Scheduler</code>'s list, of registered <code>JobListener</code>s.
*/
public void addJobListener(JobListener jobListener) {
if (jobListener.getName() == null
|| jobListener.getName().length() == 0) {
throw new IllegalArgumentException(
"JobListener name cannot be empty.");
}
synchronized (jobListeners) {
jobListeners.put(jobListener.getName(), jobListener);
}
}