quartz ha 一点疑惑


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.
[color=red] * <p>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.[/color]
* <p>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.
* <p><b>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.</b>
* @see SchedulerFactoryBean#setApplicationContextSchedulerContextKey
* @see org.springframework.context.ApplicationContext
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值