spring quartz点滴的理解

1.spring容器启动,很多插件就可以充当一个bean在spring容器启动的时候跟着容器一起初始化,举个例子:quartz,其就配置成一个bean放在spring的配置文件中。因而我们知道了,quartz的初始化时机。


首先我们来看配置文件

<!-- Quartz -->
<bean lazy-init="false" autowire="no" name="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:quartz.properties" />
</bean>

<!-- applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文(applicationcontext)以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文 -->

 private static final String APPLICATION_CONTEXT_KEY = "applicationContextKey";
  private ApplicationContext getApplicationContext(JobExecutionContext context) throws Exception {
      ApplicationContext appCtx = null;
      appCtx = (ApplicationContext) context.getScheduler().getContext().get(APPLICATION_CONTEXT_KEY);//这里就刚好满足上面那句话的意思,获取到spring上下 文(applicationcontext)
2.每一个 Quartz Job 必须有一个实现了 org.quartz.Job 接口的具体类。这个接口仅有一个要你在 Job 中实现的方法,execute(),方法 execute() 的原型如下:

public void execute(JobExecutionContext context) throws JobExecutionException;

 Quartz 调度器确定到时间激发一个 Job 的时候,它就会生成一个 Job 实例,并调用这个实例的 execute() 方法。调度器只管调用 execute() 方法,而不关心执行的结果,除了在作业执行中出问题抛出的 org.quartz.JobExecutionException 异常。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值