定时器注入spring

ApplicationContextUtil.java 

package org.job;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Created with IntelliJ IDEA.定时器的执行优先于注入 所以需要创建一个类,用来获取service
 * User: vincent
 * Date: 2017/11/8
 * Time: 15:39
 */
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.applicationContext = applicationContext;
    }

    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

}

JobFactory.java

package org.job;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * User: vincent
 * Date: 2017/10/30
 * Time: 17:00
 */
@Configuration
public class JobFactory  {

    @Bean(name = "jobScheduler")
    public Scheduler getScheduler() {

        SchedulerFactory schedulerfactory = new StdSchedulerFactory();
        Scheduler scheduler = null;
        try {
            scheduler = schedulerfactory.getScheduler();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return scheduler;
    }

}

PersonService.java

@Service("pointService")
public class PointServiceImpl implements PointService {

	@Autowired
	PersonDao personDao;
    public getPersonList(){
     doSometing();
    }
	
}

MyJob.java

@DisallowConcurrentExecution
public class MyJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //System.out.println(DateHelper2.getCurrentDateTimeFormat());
        try {
            Thread.sleep(2000);
            PointService pointService = (PersonService)ApplicationContextUtil.getBean("personService");
            System.out.println(personService);
            personService.updateGlobalPointValue1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public MyJob() {
    }
}

JobServiceImpl.java

package org.taian.service.impl;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.taian.service.JobService;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vincent
 * Date: 2017/11/9
 * Time: 14:15
 */
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    @Qualifier("jobScheduler")
    private Scheduler scheduler;

    @Override
    public void doJob() {
        //触发器
        JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job1", "jgroup1").build();
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simpleTrigger", "triggerGroup")
                .withSchedule(CronScheduleBuilder.cronSchedule("1-30 * * * * ? *"))
                .startNow().build();
        try {
            //把作业和触发器注册到任务调度中
            scheduler.scheduleJob(job, trigger);
            //启动调度
            scheduler.start();
            Thread.sleep(5000);
            List<JobExecutionContext> currentlyExecutingJobs = scheduler.getCurrentlyExecutingJobs();
            System.out.println(currentlyExecutingJobs.size());
            for (JobExecutionContext jobExecutionContext:currentlyExecutingJobs){
                System.out.println(jobExecutionContext.getJobDetail().getKey());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

controller.java

@ResponseBody
    @RequestMapping(value = "doQuartz", method = RequestMethod.GET)
    public String doQuartz(){
        jobService.doJob();
        return "a";
    }

 

转载于:https://my.oschina.net/duanvincent/blog/1570561

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值