ITeye博客

quartz ha 一点疑惑

taoshi · 2016-12-01 19:50 · Spring quartz · 企业架构 · Markdown


public class MyClusterDetailQuartzJobBean extends QuartzJobBean implements StatefulJob{

	protected final Logger logger = LoggerFactory
			.getLogger(ClusterDetailQuartzJobBean.class);

	// 上下文
	private ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	// 对象
	private String targetObject;

	public void setTargetObject(String targetObject) {
		this.targetObject = targetObject;
	}

	// 方法
	private String targetMethod;

	public void setTargetMethod(String targetMethod) {
		this.targetMethod = targetMethod;
	}

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		logger.debug("execute [" + targetobject + "] at once >>>>>>");
		try {
			// 从Spring获取对象
			Object otargetObject = applicationContext.getBean(targetObject);
			// 获取方法
			Method m = otargetObject.getClass().getMethod(targetMethod,
					new Class[] {JobExecutionContext.class});
			// 执行
			m.invoke(otargetObject, new Object[] { context });
		} catch (Exception e) {
			logger.error("", e);
			throw new JobExecutionException(e);
		}
	}

这里的applicationContext 如何注入进来的?

org.springframework.scheduling.quartz.JobDetailBean 里面一段注释

/** * Set the key of an ApplicationContext reference to expose in the JobDataMap, * for example "applicationContext". Default is none. * Only applicable when running in a Spring ApplicationContext. * In case of a QuartzJobBean, the reference will be applied to the Job * instance as bean property. An "applicationContext" attribute will correspond * to a "setApplicationContext" method in that scenario. * Note that BeanFactory callback interfaces like ApplicationContextAware * are not automatically applied to Quartz Job instances, because Quartz * itself is responsible for the lifecycle of its Jobs. * Note: When using persistent job stores where JobDetail contents will * be kept in the database, do not put an ApplicationContext reference into * the JobDataMap but rather into the SchedulerContext. * @see SchedulerFactoryBean#setApplicationContextSchedulerContextKey * @see org.springframework.context.ApplicationContext */

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。