⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schedulerfactorybean.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		}
	}

	/**
	 * 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);
					addCalendarToScheduler(calendarName, calendar);
				}
			}

			// Register Triggers.
			if (this.triggers != null) {
				for (Iterator it = this.triggers.iterator(); it.hasNext();) {
					Trigger trigger = (Trigger) it.next();
					if (this.scheduler.getTrigger(trigger.getName(), trigger.getGroup()) == null) {
						// 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);
							}
						}
						this.scheduler.scheduleJob(trigger);
					}
					else if (this.overwriteExistingJobs) {
						this.scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(), 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 true if the job was actually added,
	 * false 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 calendar to the Scheduler, checking for the
	 * corresponding Quartz 1.4 or Quartz 1.3 method
	 * (which differ in 1.4's additional "updateTriggers" flag).
	 * @param calendarName the name of the calendar
	 * @param calendar the Calendar object
	 * @see org.quartz.Scheduler#addCalendar
	 */
	private void addCalendarToScheduler(String calendarName, Calendar calendar) throws SchedulerException {
		try {
			try {
				// Try Quartz 1.4 (with "updateTriggers" flag).
				Method addCalendarMethod = this.scheduler.getClass().getMethod(
						"addCalendar", new Class[] {String.class, Calendar.class, boolean.class, boolean.class});
				addCalendarMethod.invoke(
						this.scheduler, new Object[] {calendarName, calendar, Boolean.TRUE, Boolean.TRUE});
			}
			catch (NoSuchMethodException ex) {
				// Try Quartz 1.3 (without "updateTriggers" flag).
				Method addCalendarMethod = this.scheduler.getClass().getMethod(
						"addCalendar", new Class[] {String.class, Calendar.class, boolean.class});
				addCalendarMethod.invoke(
						this.scheduler, new Object[] {calendarName, calendar, Boolean.TRUE});
			}
		}
		catch (InvocationTargetException ex) {
			if (ex.getTargetException() instanceof SchedulerException) {
				throw (SchedulerException) ex.getTargetException();
			}
			ReflectionUtils.handleInvocationTargetException(ex);
		}
		catch (Exception ex) {
			ReflectionUtils.handleReflectionException(ex);
		}
	}

	/**
	 * 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 {
			logger.info("Will start Quartz scheduler in " + startupDelay + " seconds");
			new Thread() {
				public void run() {
					try {
						Thread.sleep(startupDelay * 1000);
					}
					catch (InterruptedException ex) {
						// simply proceed
					}
					logger.info("Starting Quartz scheduler now, after delay of " + startupDelay + " seconds");
					try {
						scheduler.start();
					}
					catch (SchedulerException ex) {
						throw new DelayedSchedulerStartException(ex);
					}
				}
			}.start();
		}
	}


	public Object getObject() {
		return this.scheduler;
	}

	public Class getObjectType() {
		return (this.scheduler != null) ? this.scheduler.getClass() : Scheduler.class;
	}

	public boolean isSingleton() {
		return true;
	}


	/**
	 * 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);
	}


	/**
	 * Exception to be thrown if the Quartz scheduler cannot be started
	 * after the specified delay has passed.
	 */
	public static class DelayedSchedulerStartException extends NestedRuntimeException {

		private DelayedSchedulerStartException(SchedulerException ex) {
			super("Could not start Quartz scheduler after delay", ex);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -