2019独角兽企业重金招聘Python工程师标准>>>
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;}@Overridepublic 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 {@AutowiredPersonDao personDao;public getPersonList(){doSometing();}}
MyJob.java
@DisallowConcurrentExecution
public class MyJob implements Job {@Overridepublic 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;@Overridepublic 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
}
controller.java
@ResponseBody@RequestMapping(value = "doQuartz", method = RequestMethod.GET)public String doQuartz(){jobService.doJob();return "a";}