龙空技术网

Java,任务调度,Quartz,4种触发器和2种调度工厂

古怪今人 163

前言:

今天我们对“javavoid可以return”可能比较关怀,大家都想要了解一些“javavoid可以return”的相关文章。那么小编在网摘上汇集了一些关于“javavoid可以return””的相关知识,希望各位老铁们能喜欢,同学们快快来了解一下吧!

org.quartz.SchedulerFactory:

StdSchedulerFactory,是SchedulerFactory接口的一个实现,使用一套属性(NameValueCollection)来创建和初始化Quartz Scheduler。这些属性通常在文件中存储和加载,也可以通过编写程序来直接操作工厂,简单地调用工厂的getScheduler()就可以产生一个scheduler,初始化(以及它的ThreadPool、JobStore和DataSources),并且返回一个公共的接口。

DirectSchedulerFactory,是SchedulerFactory的另一个实现,更加程序化的方式创建Scheduler非常有用,不鼓励使用的原因:1、需要用户非常了解想要干什么,2、不允许声明式的配置,需要使用硬编码的方式设置scheduler。

org.quartz.Trigger:

SimpleTrigger,简单触发器,在一个指定时间段内执行一次作业任务或是在指定时间间隔内执行多次作业任务;

CronTrigger,Cron触发器,Cron表达式,是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:

1、Seconds Minutes Hours DayofMonth Month DayofWeek Year

2、Seconds Minutes Hours DayofMonth Month DayofWeek

DailyTimeIntervalTrigger,日期触发器,指定每天的某个时间段内,以一定的时间间隔执行任务,并且可以支持指定星期。适合的需求类似于:一个任务每天9点开始执行,下午5点执行完毕,每隔一小时执行一次,并且只有周一到周四执行;有如下属性:

1、startTimeOfDay 每天开始时间

2、endTimeOfDay 每天结束时间

3、daysOfWeek 需要执行的星期

4、interval 执行间隔

5、repeatCount 重复次数

CalendarIntervalTrigger,日历触发器。

代码案例(2种调度工厂):

package com.what21.boot.timer.quartz.demo;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.impl.DirectSchedulerFactory;import org.quartz.impl.StdSchedulerFactory;import org.quartz.simpl.RAMJobStore;import org.quartz.simpl.SimpleThreadPool;import org.quartz.spi.JobStore;public class MyScheduler {    public static void main(String[] args) {        int threadCount = 10;        Scheduler scheduler1 = createScheduler(threadCount);        System.out.println("scheduler1=" + scheduler1);        Scheduler scheduler2 = createScheduler(threadCount, "myScheduler", "SIMPLE_NON_CLUSTERED");        System.out.println("scheduler2=" + scheduler2);        Scheduler scheduler3 = createScheduler();        System.out.println("scheduler3=" + scheduler3);        /*        JobStore也可以是:        JNDIConnectionProvider connectionProvider = new JNDIConnectionProvider("java:myDatasource", false);        DBConnectionManager.getInstance().addConnectionProvider("myDatasource", connectionProvider);        JobStoreTX jdbcJobStore = new JobStoreTX();        jdbcJobStore.setDataSource("myDatasource");        jdbcJobStore.setTablePrefix("QT_");        jdbcJobStore.setInstanceId("myInstance");        */    }    public static Scheduler createScheduler(int threadCount, String schedulerName, String schedulerInstanceId) {        Scheduler scheduler = null;        try {            // Quartz在执行jobs时使用的线程,池中的线程越多,并发运行的jobs数越多,线程太多可能会破坏的系统;            // 线程池:参数1:线程个数,参数2:线程优先级            SimpleThreadPool threadPool = new SimpleThreadPool(threadCount, Thread.NORM_PRIORITY);            threadPool.initialize();            // job存储器            JobStore jobStore = new RAMJobStore();            // DirectSchedulerFactory是另一个SchedulerFactory实现            DirectSchedulerFactory.getInstance().createScheduler(schedulerName, schedulerInstanceId, threadPool,                    jobStore);            scheduler = DirectSchedulerFactory.getInstance().getScheduler(schedulerName);        } catch (Exception e) {            e.printStackTrace();        }        return scheduler;    }    public static Scheduler createScheduler(int threadCount) {        Scheduler scheduler = null;        try {            // Quartz在执行jobs时使用的线程,池中的线程越多,并发运行的jobs数越多,线程太多可能会破坏的系统;            // 线程池:参数1:线程个数,参数2:线程优先级            SimpleThreadPool threadPool = new SimpleThreadPool(threadCount, Thread.NORM_PRIORITY);            threadPool.initialize();            // job存储器            JobStore jobStore = new RAMJobStore();            // DirectSchedulerFactory是另一个SchedulerFactory实现            DirectSchedulerFactory.getInstance().createScheduler(threadPool, jobStore);            scheduler = DirectSchedulerFactory.getInstance().getScheduler();        } catch (Exception e) {            e.printStackTrace();        }        return scheduler;    }    public static Scheduler createScheduler() {        Scheduler scheduler = null;        try {            //1.创建Scheduler的工厂            SchedulerFactory factory = new StdSchedulerFactory();            //2.从工厂中获取调度器实例            scheduler = factory.getScheduler();        } catch (SchedulerException e) {            e.printStackTrace();        }        return scheduler;    }}
代码案例(4种触发器):
package com.what21.boot.timer.quartz.demo;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Random;public class MyJob implements Job {    @Override    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {        String printTime = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(new Date());        System.out.println("MyJob start at:" + printTime + ", Hello Job-" + new Random().nextInt(100));    }}
package com.what21.boot.timer.quartz.demo;import com.what21.boot.timer.demo.MyScheduler;import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;import java.util.concurrent.TimeUnit;public class MyScheduler1 {    public static void main(String[] args) {        // 1、创建调度器Scheduler        SchedulerFactory schedulerFactory = new StdSchedulerFactory();        Scheduler scheduler = null;        try {            scheduler = schedulerFactory.getScheduler();            // 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)            JobDetail jobDetail = JobBuilder.newJob(MyScheduler.MyJob.class)                    .withIdentity("job1", "jobGroup1").build();            // SimpleTrigger——简单触发器            // 3、构建Trigger实例(每隔2s执行一次)            SimpleTrigger trigger = TriggerBuilder.newTrigger()                    .withIdentity("trigger1", "triggerGroup1")                    .startNow() // 立即生效                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()                            .withIntervalInSeconds(2)// 每隔2s执行一次                            .repeatForever()).build();// 一直执行            //4、执行            scheduler.scheduleJob(jobDetail, trigger);            System.out.println("--------scheduler start ! ------------");            scheduler.start();        } catch (SchedulerException e) {            e.printStackTrace();        }        try {            //睡眠            TimeUnit.MINUTES.sleep(1);        } catch (InterruptedException e) {            e.printStackTrace();        }        try {            scheduler.shutdown();        } catch (SchedulerException e) {            e.printStackTrace();        }        System.out.println("--------scheduler shutdown ! ------------");    }}
package com.what21.boot.timer.quartz.demo;import com.what21.boot.timer.demo.MyScheduler;import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;import java.util.concurrent.TimeUnit;public class MyScheduler2 {    public static void main(String[] args) {        // 1、创建调度器Scheduler        SchedulerFactory schedulerFactory = new StdSchedulerFactory();        Scheduler scheduler = null;        try {            scheduler = schedulerFactory.getScheduler();            // 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)            JobDetail jobDetail = JobBuilder.newJob(MyScheduler.MyJob.class)                    .withIdentity("job1", "jobGroup1").build();            // CronTrigger——Cron触发器            // 3、构建Trigger实例(每两秒执行)            CronTrigger trigger = TriggerBuilder.newTrigger()                    .withIdentity("trigger1", "triggerGroup1")                    .withSchedule(                            CronScheduleBuilder.cronSchedule("/2 * * * * ?")                    ).build();            //4、执行            scheduler.scheduleJob(jobDetail, trigger);            System.out.println("--------scheduler start ! ------------");            scheduler.start();        } catch (SchedulerException e) {            e.printStackTrace();        }        try {            //睡眠            TimeUnit.MINUTES.sleep(1);        } catch (InterruptedException e) {            e.printStackTrace();        }        try {            scheduler.shutdown();        } catch (SchedulerException e) {            e.printStackTrace();        }        System.out.println("--------scheduler shutdown ! ------------");    }}
package com.what21.boot.timer.quartz.demo;import com.what21.boot.timer.demo.MyScheduler;import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;import java.util.concurrent.TimeUnit;public class MyScheduler3 {    public static void main(String[] args) {        // 1、创建调度器Scheduler        SchedulerFactory schedulerFactory = new StdSchedulerFactory();        Scheduler scheduler = null;        try {            scheduler = schedulerFactory.getScheduler();            // 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)            JobDetail jobDetail = JobBuilder.newJob(MyScheduler.MyJob.class)                    .withIdentity("job1", "jobGroup1").build();            // DailyTimeIntervalTrigger——日期触发            // 3、构建Trigger实例(每2秒执行一次)===>            DailyTimeIntervalTrigger trigger = TriggerBuilder.newTrigger()                    .withIdentity("trigger1", "triggerGroup1")                    .withSchedule(                            DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule()                                    .withInterval(2, DateBuilder.IntervalUnit.SECOND)                    ).build();            //4、执行            scheduler.scheduleJob(jobDetail, trigger);            System.out.println("--------scheduler start ! ------------");            scheduler.start();        } catch (SchedulerException e) {            e.printStackTrace();        }        try {            //睡眠            TimeUnit.MINUTES.sleep(1);        } catch (InterruptedException e) {            e.printStackTrace();        }        try {            scheduler.shutdown();        } catch (SchedulerException e) {            e.printStackTrace();        }        System.out.println("--------scheduler shutdown ! ------------");    }}
package com.what21.boot.timer.quartz.demo;import com.what21.boot.timer.demo.MyScheduler;import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;import java.util.concurrent.TimeUnit;public class MyScheduler4 {    public static void main(String[] args) {        // 1、创建调度器Scheduler        SchedulerFactory schedulerFactory = new StdSchedulerFactory();        Scheduler scheduler = null;        try {            scheduler = schedulerFactory.getScheduler();            // 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)            JobDetail jobDetail = JobBuilder.newJob(MyScheduler.MyJob.class)                    .withIdentity("job1", "jobGroup1").build();            // CalendarIntervalTrigger——日历触发            // 3、构建Trigger实例(每2秒执行一次)            CalendarIntervalTrigger trigger = TriggerBuilder.newTrigger()                    .withIdentity("trigger1", "triggerGroup1")                    .withSchedule(                            CalendarIntervalScheduleBuilder.calendarIntervalSchedule()                                    .withInterval(2, DateBuilder.IntervalUnit.SECOND)                    ).build();            //4、执行            scheduler.scheduleJob(jobDetail, trigger);            System.out.println("--------scheduler start ! ------------");            scheduler.start();        } catch (SchedulerException e) {            e.printStackTrace();        }        try {            //睡眠            TimeUnit.MINUTES.sleep(1);        } catch (InterruptedException e) {            e.printStackTrace();        }        try {            scheduler.shutdown();        } catch (SchedulerException e) {            e.printStackTrace();        }        System.out.println("--------scheduler shutdown ! ------------");    }}

标签: #javavoid可以return #datejavastring #javaseq