?? schedulerfactorybean.java
字號:
}
((StdSchedulerFactory) schedulerFactory).initialize(mergedProps);
}
}
/**
* Create the Scheduler instance for the given factory and scheduler name.
* Called by afterPropertiesSet.
* <p>Default implementation invokes SchedulerFactory's <code>getScheduler</code>
* method. Can be overridden for custom Scheduler creation.
* @param schedulerFactory the factory to create the Scheduler with
* @param schedulerName the name of the scheduler to create
* @return the Scheduler instance
* @throws SchedulerException if thrown by Quartz methods
* @see #afterPropertiesSet
* @see org.quartz.SchedulerFactory#getScheduler
*/
protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName)
throws SchedulerException {
// StdSchedulerFactory's default "getScheduler" implementation
// uses the scheduler name specified in the Quartz properties,
// which we have set before (in "initSchedulerFactory").
return schedulerFactory.getScheduler();
}
/**
* Expose the specified context attributes and/or the current
* ApplicationContext in the Quartz SchedulerContext.
*/
private void populateSchedulerContext() throws SchedulerException {
// Put specified objects into Scheduler context.
if (this.schedulerContextMap != null) {
this.scheduler.getContext().putAll(this.schedulerContextMap);
}
// Register ApplicationContext in Scheduler context.
if (this.applicationContextSchedulerContextKey != null) {
if (this.applicationContext == null) {
throw new IllegalStateException(
"SchedulerFactoryBean needs to be set up in an ApplicationContext " +
"to be able to handle an 'applicationContextSchedulerContextKey'");
}
this.scheduler.getContext().put(this.applicationContextSchedulerContextKey, this.applicationContext);
}
}
/**
* Register all specified listeners with the Scheduler.
*/
private void registerListeners() throws SchedulerException {
if (this.schedulerListeners != null) {
for (int i = 0; i < this.schedulerListeners.length; i++) {
this.scheduler.addSchedulerListener(this.schedulerListeners[i]);
}
}
if (this.globalJobListeners != null) {
for (int i = 0; i < this.globalJobListeners.length; i++) {
this.scheduler.addGlobalJobListener(this.globalJobListeners[i]);
}
}
if (this.jobListeners != null) {
for (int i = 0; i < this.jobListeners.length; i++) {
this.scheduler.addJobListener(this.jobListeners[i]);
}
}
if (this.globalTriggerListeners != null) {
for (int i = 0; i < this.globalTriggerListeners.length; i++) {
this.scheduler.addGlobalTriggerListener(this.globalTriggerListeners[i]);
}
}
if (this.triggerListeners != null) {
for (int i = 0; i < this.triggerListeners.length; i++) {
this.scheduler.addTriggerListener(this.triggerListeners[i]);
}
}
}
/**
* Register jobs and triggers (within a transaction, if possible).
*/
private void registerJobsAndTriggers() throws SchedulerException {
TransactionStatus transactionStatus = null;
if (this.transactionManager != null) {
transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
}
try {
if (this.jobSchedulingDataLocations != null) {
ResourceJobSchedulingDataProcessor dataProcessor = new ResourceJobSchedulingDataProcessor();
if (this.applicationContext != null) {
dataProcessor.setResourceLoader(this.applicationContext);
}
for (int i = 0; i < this.jobSchedulingDataLocations.length; i++) {
dataProcessor.processFileAndScheduleJobs(
this.jobSchedulingDataLocations[i], this.scheduler, this.overwriteExistingJobs);
}
}
// Register JobDetails.
if (this.jobDetails != null) {
for (Iterator it = this.jobDetails.iterator(); it.hasNext();) {
JobDetail jobDetail = (JobDetail) it.next();
addJobToScheduler(jobDetail);
}
}
else {
// Create empty list for easier checks when registering triggers.
this.jobDetails = new LinkedList();
}
// Register Calendars.
if (this.calendars != null) {
for (Iterator it = this.calendars.keySet().iterator(); it.hasNext();) {
String calendarName = (String) it.next();
Calendar calendar = (Calendar) this.calendars.get(calendarName);
this.scheduler.addCalendar(calendarName, calendar, true, true);
}
}
// Register Triggers.
if (this.triggers != null) {
for (Iterator it = this.triggers.iterator(); it.hasNext();) {
Trigger trigger = (Trigger) it.next();
addTriggerToScheduler(trigger);
}
}
}
catch (Throwable ex) {
if (transactionStatus != null) {
try {
this.transactionManager.rollback(transactionStatus);
}
catch (TransactionException tex) {
logger.error("Job registration exception overridden by rollback exception", ex);
throw tex;
}
}
if (ex instanceof SchedulerException) {
throw (SchedulerException) ex;
}
if (ex instanceof Exception) {
throw new SchedulerException(
"Registration of jobs and triggers failed: " + ex.getMessage(), (Exception) ex);
}
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
}
if (transactionStatus != null) {
this.transactionManager.commit(transactionStatus);
}
}
/**
* Add the given job to the Scheduler, if it doesn't already exist.
* Overwrites the job in any case if "overwriteExistingJobs" is set.
* @param jobDetail the job to add
* @return <code>true</code> if the job was actually added,
* <code>false</code> if it already existed before
* @see #setOverwriteExistingJobs
*/
private boolean addJobToScheduler(JobDetail jobDetail) throws SchedulerException {
if (this.overwriteExistingJobs ||
this.scheduler.getJobDetail(jobDetail.getName(), jobDetail.getGroup()) == null) {
this.scheduler.addJob(jobDetail, true);
return true;
}
else {
return false;
}
}
/**
* Add the given trigger to the Scheduler, if it doesn't already exist.
* Overwrites the trigger in any case if "overwriteExistingJobs" is set.
* @param trigger the trigger to add
* @return <code>true</code> if the trigger was actually added,
* <code>false</code> if it already existed before
* @see #setOverwriteExistingJobs
*/
private boolean addTriggerToScheduler(Trigger trigger) throws SchedulerException {
boolean triggerExists = (this.scheduler.getTrigger(trigger.getName(), trigger.getGroup()) != null);
if (!triggerExists || this.overwriteExistingJobs) {
// Check if the Trigger is aware of an associated JobDetail.
if (trigger instanceof JobDetailAwareTrigger) {
JobDetail jobDetail = ((JobDetailAwareTrigger) trigger).getJobDetail();
// Automatically register the JobDetail too.
if (!this.jobDetails.contains(jobDetail) && addJobToScheduler(jobDetail)) {
this.jobDetails.add(jobDetail);
}
}
if (!triggerExists) {
try {
this.scheduler.scheduleJob(trigger);
}
catch (ObjectAlreadyExistsException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Unexpectedly found existing trigger, assumably due to cluster race condition: " +
ex.getMessage() + " - can safely be ignored");
}
if (this.overwriteExistingJobs) {
this.scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
}
}
}
else {
this.scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
}
return true;
}
else {
return false;
}
}
/**
* Start the Quartz Scheduler, respecting the "startupDelay" setting.
* @param scheduler the Scheduler to start
* @param startupDelay the number of seconds to wait before starting
* the Scheduler asynchronously
*/
protected void startScheduler(final Scheduler scheduler, final int startupDelay) throws SchedulerException {
if (startupDelay <= 0) {
logger.info("Starting Quartz Scheduler now");
scheduler.start();
}
else {
if (logger.isInfoEnabled()) {
logger.info("Will start Quartz Scheduler [" + scheduler.getSchedulerName() +
"] in " + startupDelay + " seconds");
}
Thread schedulerThread = new Thread() {
public void run() {
try {
Thread.sleep(startupDelay * 1000);
}
catch (InterruptedException ex) {
// simply proceed
}
if (logger.isInfoEnabled()) {
logger.info("Starting Quartz Scheduler now, after delay of " + startupDelay + " seconds");
}
try {
scheduler.start();
}
catch (SchedulerException ex) {
throw new SchedulingException("Could not start Quartz Scheduler after delay", ex);
}
}
};
schedulerThread.setName("Quartz Scheduler [" + scheduler.getSchedulerName() + "]");
schedulerThread.start();
}
}
//---------------------------------------------------------------------
// Implementation of FactoryBean interface
//---------------------------------------------------------------------
public Object getObject() {
return this.scheduler;
}
public Class getObjectType() {
return (this.scheduler != null) ? this.scheduler.getClass() : Scheduler.class;
}
public boolean isSingleton() {
return true;
}
//---------------------------------------------------------------------
// Implementation of Lifecycle interface
//---------------------------------------------------------------------
public void start() throws SchedulingException {
if (this.scheduler != null) {
try {
this.scheduler.start();
}
catch (SchedulerException ex) {
throw new SchedulingException("Could not start Quartz Scheduler", ex);
}
}
}
public void stop() throws SchedulingException {
if (this.scheduler != null) {
try {
this.scheduler.standby();
}
catch (SchedulerException ex) {
throw new SchedulingException("Could not stop Quartz Scheduler", ex);
}
}
}
public boolean isRunning() throws SchedulingException {
if (this.scheduler != null) {
try {
return !this.scheduler.isInStandbyMode();
}
catch (SchedulerException ex) {
return false;
}
}
return false;
}
//---------------------------------------------------------------------
// Implementation of DisposableBean interface
//---------------------------------------------------------------------
/**
* Shut down the Quartz scheduler on bean factory shutdown,
* stopping all scheduled jobs.
*/
public void destroy() throws SchedulerException {
logger.info("Shutting down Quartz Scheduler");
this.scheduler.shutdown(this.waitForJobsToCompleteOnShutdown);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -