定时器注入spring
生活随笔
收集整理的這篇文章主要介紹了
定时器注入spring
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
擼了今年阿里、頭條和美團(tuán)的面試,我有一個重要發(fā)現(xiàn).......>>>
ApplicationContextUtil.java?
package org.job;import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;/*** Created with IntelliJ IDEA.定時器的執(zhí)行優(yōu)先于注入 所以需要創(chuàng)建一個類,用來獲取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() {//觸發(fā)器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 {//把作業(yè)和觸發(fā)器注冊到任務(wù)調(diào)度中scheduler.scheduleJob(job, trigger);//啟動調(diào)度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";}?
總結(jié)
以上是生活随笔為你收集整理的定时器注入spring的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git 删除远程仓库文件
- 下一篇: mybatis 处理参数ListStri