quarz spring boot

 

package com.pkfare.task.manage.config;



import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;


@Configuration
@Component
public class JobBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {

    private ApplicationContext applicationContext;

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

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        //把Job交给Spring来管理,这样Job就能使用由Spring产生的Bean了
        applicationContext.getAutowireCapableBeanFactory().autowireBean(jobInstance);
        return jobInstance;
    }


}

 

import com.pkfare.task.manage.service.impl.SelectTaskServiceImpl;
import com.pkfare.task.manage.util.ReadConfigUtil;
import com.pkfare.task.manage.job.MainJob;
import org.quartz.JobDetail;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.text.ParseException;

import static org.quartz.JobBuilder.newJob;

/**
 * Created by llq on 2016/10/28.
 */
@Configurable
public class QuartzConfig {

    @Autowired
    private JobBeanJobFactory jobBeanJobFactory;

    @Autowired
    SelectTaskServiceImpl selectTaskService;

    @Autowired
    @Bean
    public SchedulerFactoryBean SchedulerFactoryBean(DataSource dataSource) throws ParseException {

        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setDataSource(dataSource);

        bean.setQuartzProperties(ReadConfigUtil.readConfig("quartz.properties"));
        bean.setSchedulerName("CRMscheduler");
        bean.setStartupDelay(3);
        bean.setApplicationContextSchedulerContextKey("applicationContextKey");
        bean.setOverwriteExistingJobs(true);
        bean.setAutoStartup(true);
        bean.setJobFactory(jobBeanJobFactory);

        return bean;
    }

}

 

http://www.blogjava.net/paulwong/archive/2014/11/14/420104.html

posted on 2017-05-09 11:53  dhj  阅读(295)  评论(0编辑  收藏  举报

导航