• quartz定时任务-job


    1,首先添加对quartz组建的引用 quartz-2.2.3.jar,slf4j-api-1.7.7.jar

    https://files.cnblogs.com/files/renjing/quartz.zip

    2,新增类实现job接口

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    public class TestJob implements Job {
    
        private static final Logger logger = LogManager.getLogger(TestJob.class);
        
        @Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            logger.debug("TestJob执行啦!");
        }
    }

    3.新增一个Servlet,并且设置为在网站启动的时候自动初始化

    import java.util.TimeZone;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.quartz.CronScheduleBuilder;
    import org.quartz.CronTrigger;
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
    
    //loadOnStartup表示:网站启动的时候自动初始化QuartzInitServlet(即init被调用)
    @WebServlet(value = "/QuartzInitServlet", loadOnStartup = 1)
    public class QuartzInitServlet extends HttpServlet {
        
        private static final Logger logger = LogManager.getLogger(QuartzInitServlet.class);
        @Override
        public void init() throws ServletException {
            super.init();
            
            try
            {
                Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
                JobDetail job = JobBuilder.newJob(TestJob.class).build();
                CronScheduleBuilder cronScheduleBuilder = 
                        CronScheduleBuilder.cronSchedule("0 0 0/1 * * ? ").inTimeZone(TimeZone.getTimeZone("GMT+08:00"));//每小时执行一次
                        //CronScheduleBuilder.cronSchedule("10/5 * * * * ? ").inTimeZone(TimeZone.getTimeZone("GMT+08:00"));//从10秒开始,每隔5秒执行一次
                        //CronScheduleBuilder.cronSchedule("45 53 23 * * ? ").inTimeZone(TimeZone.getTimeZone("GMT+08:00"));//每天23:53:45
                        //CronScheduleBuilder.dailyAtHourAndMinute(23, 48).inTimeZone(TimeZone.getTimeZone("GMT+08:00"));
                CronTrigger trigger = TriggerBuilder.newTrigger().withSchedule(cronScheduleBuilder).build();
                scheduler.scheduleJob(job, trigger);
                scheduler.start();//不要忘了启动
            }
            catch(Throwable ex)
            {
                logger.error("定时任务安排出错",ex);
            }
        }
    }
  • 相关阅读:
    Android拷贝工程不覆盖原工程的配置方法
    Android中BindService方式使用的理解
    getApplicationContext()、Activity.this、 getBaseContext区别
    深入理解ASP.NET MVC(4)
    深入理解ASP.NET MVC(1)
    深入理解ASP.NET MVC(2)
    深入理解ASP.NET MVC(目录)
    C# 代码优化
    NPOI 创建Excel 设置宽度 样式 颜色对比表
    2. Mysql数据库的入门知识
  • 原文地址:https://www.cnblogs.com/renjing/p/quartz-job.html
Copyright © 2020-2023  润新知