new_seas
HDNew
2017-05-03 08:06
浏览 1.6k

如何关闭SchedulerFactoryBean中的一个任务triggers

配置文件中有如下配置:

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
    <property name="triggers"> 
        <list>
            <ref local="customCronTrigger" />
        </list>
    </property>
</bean>
<!-- 自定义触发器 -->
<bean id="customCronTrigger" class="com.gilight.dep.metadata.web.util.CustomCronTrigger">
    <property name="jobDetail" ref="myJobDetail" /> 
    <property name="initBackupTimeService" ref="initBackupTimeService"></property>
</bean>
<!-- 动态jobDetail备份系统 -->
<bean id="myJobDetail"  
      class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
        <ref bean="systemBackupService" />
    </property>
    <property name="targetMethod" value="backupSystem" />
    <property name="concurrent" value="false" />
</bean> 

现在要实现一个改变定时任务的功能,需要在控制类中关闭原来的triggers,查源码发现,关闭triggers的方法在SchedulerFactoryBean中,是stop();但是在控制类中如何注入这个原来 的SchedulerFactoryBean使其关闭呢?
我写的是这样;但是不可以,会报一个类型转换异常的错StdScheduler cannot be cast to SchedulerFactoryBean

public class SystemBackupController extends MetadataBaseController{

private SchedulerFactoryBean schedulerFactoryBean;//实现类无法bean注入?????
private CustomCronTrigger customCronTrigger;
private ISystemBackupService systemBackupService;
private IInitBackupTimeService initBackupTimeService;

private String backupTime;//前台时间
private Trigger[] triggers;

/*get,set方法省略*/


@RequestMapping(value = "/start", method = RequestMethod.POST)
public void start(@RequestBody String json,HttpServletRequest request) throws ParseException {

    System.out.println(json);
    Map<String, Object> map = (Map<String, Object>) JSON.parse(json);
    backupTime = (String) map.get("backupTime");
    System.out.println(backupTime);

    ApplicationContext sac = RequestContextUtils.getWebApplicationContext(request);
    ConfigurableApplicationContext context = (ConfigurableApplicationContext) sac;

    //Bean的实例工厂  
    DefaultListableBeanFactory dbf = (DefaultListableBeanFactory) context.getBeanFactory(); 
    schedulerFactoryBean =(SchedulerFactoryBean) dbf.getBean("schedulerFactoryBean");

    String cronExpression = customCronTrigger.getCronExpression();// 原触发器的时间
    saveBackupTime(backupTime);
    if (cronExpression != backupTime) {
        schedulerFactoryBean.stop();
        customCronTrigger.setCronExpression(backupTime);
        triggers[0] = customCronTrigger;
        schedulerFactoryBean.setTriggers(triggers);
        schedulerFactoryBean.start();
    }

}
public void saveBackupTime(String backupTime) {
    systemBackupService.saveBackupTime(backupTime);
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐