Quartz framework to achieve timer

Recent projects to achieve key areas because people in real-time traffic statistics, which involve the AI ​​field, so the system has first timer mode, timed push data.

1. Create CronTriggerExample. Here achieve two timed task.

package com.troy.emergency.monitor.web.rest;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @Author: CaoTing
 * @Description:  启动定时任务
 * @Date: 2019/8/22
 */
@Component
public class CronTriggerExample implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        the try { 
            servletContextEvent.getServletContext () log (. "Timer Started" );
             // flow 
            JobKey jobKeyA = new new JobKey ( "JobFlowPerson", "named group1" ); 
            the JobDetail Joba = JobBuilder.newJob (. QuartzJobFlowPerson class ) .withIdentity ( jobKeyA) .build ();
             // Real-time staff track 
            JobKey jobKeyB = new new JobKey ( "JobPatrolPlanChildLocus", "group2" ); 
            JobDetail Jobb . = JobBuilder.newJob (QuartzJobPlanChildLocus class ) .withIdentity (jobKeyB) .build ();
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(jobA, QuartzJobFlowPerson.getJobFlowPersonTrigger());
            scheduler.scheduleJob(jobB, QuartzJobPlanChildLocus.getJobPatrolPlanChildLocusTrigger());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

}

2. Create Job.

(1)QuartzJobFlowPerson

package com.troy.emergency.monitor.web.rest;

import com.troy.emergency.monitor.service.FlowPersonService;
import com.troy.emergency.monitor.utils.ApplicationContextProvider;
import com.troy.emergency.monitor.utils.DateFormatUtils;
import lombok.extern.log4j.Log4j;
import org.quartz.*;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;


/**
 * @Author: CaoTing
 * @Description:  人流任务
 * @Date: 2019/8/22
 */
@Log4j
@RestController
public class QuartzJobFlowPerson implements Job {

    @Resource
    private FlowPersonService flowPersonService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            //人流量
            this.flowPersonService = ApplicationContextProvider.getBean(FlowPersonService.class);
            flowPersonService.addFlowPerson();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Trigger getJobFlowPersonTrigger(){
        try {
            //获取当前时间的整点
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.MINUTE, 0);
            ca.set(Calendar.SECOND, 0);
            Date sTime = ca.getTime();
            Date eTime= DateFormatUtils.StringToDate("2020-12-31 23:59:59","yyyy-MM-dd HH:mm:ss");
            SimpleScheduleBuilder scheduleBuilder=SimpleScheduleBuilder.simpleSchedule().withIntervalInHours(2).repeatForever();
            return TriggerBuilder.newTrigger().withIdentity("flowPersonJob","group1").startAt(sTime).endAt(eTime).withSchedule(scheduleBuilder).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

(2) Create QuartzJobPlanChildLocus.

package com.troy.emergency.monitor.web.rest;

import com.troy.emergency.monitor.service.PatrolPlanChildLocusService;
import com.troy.emergency.monitor.utils.ApplicationContextProvider;
import com.troy.emergency.monitor.utils.DateFormatUtils;
import com.troy.emergency.monitor.utils.GetDateUtil;
import lombok.extern.log4j.Log4j;
import org.quartz.*;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @Author: CaoTing
 * @Description: 巡检人员实时轨迹
 * @Date: 2019/8/30
 */
@Log4j
@RestController
public class QuartzJobPlanChildLocus implements Job {


    @Resource
    private PatrolPlanChildLocusService patrolPlanChildLocusService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            //巡检人员轨迹
            this.patrolPlanChildLocusService = ApplicationContextProvider.getBean(PatrolPlanChildLocusService.class);
            patrolPlanChildLocusService.addPatrolPlanChildLocus();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Trigger getJobPatrolPlanChildLocusTrigger(){
        try {
            //获取当天23点
            Date sTime = GetDateUtil.getTimes23();
            Date eTime= DateFormatUtils.StringToDate("2020-12-31 23:59:59","yyyy-MM-dd HH:mm:ss");
            SimpleScheduleBuilder scheduleBuilder=SimpleScheduleBuilder.simpleSchedule().withIntervalInHours(24).repeatForever();
            return TriggerBuilder.newTrigger().withIdentity("flowPersonJob","group2").startAt(sTime).endAt(eTime).withSchedule(scheduleBuilder).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}

3. Create ApplicationContextProvider.

package com.troy.emergency.monitor.utils;

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

/**
 * @ProjectName:
 * @Package: com.ksky.common.config
 * @ClassName: ApplicationContextProvider
 * @Description: 获取bean对象的工具类
 * @Author: YangYeZhu
 * @CreateDate: 2019/2/15
 * @Version: 1.0
 */

/**
 * @Author: CaoTing
 * @Description: 
 * @Date: 2019/8/22
 */
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

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

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    } 

    / ** 
     * returns specified by name, and Bean Clazz 
     * 
     * @param name 
     * @param clazz 
     * @param <T> 
     * @return 
     * / 
    public  static <T> T the getBean (String name, Class <T> clazz ) {
         return getApplicationContext () the getBean (name, clazz);. 
    } 
}

4, start the project can be seen that the timer has been started to print words, after the timing data is added to the database according to the setting time, as shown below:

 

Guess you like

Origin www.cnblogs.com/baiyuer666/p/11448655.html