zoukankan      html  css  js  c++  java
  • 【Quartz】一个小Demo

      Quartz是一个优秀的框架,功能强大,用法简单,比如说下面这个例子。

    (1)引入Jar包

    <!-- quartz 模块 -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>

    (2)写一个任务(Job)

    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    import java.util.Date;
    
    public class TimeJob implements Job {
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            System.out.println("Now is " + new Date());
        }
    }

    (3)写执行类

    import org.quartz.*;
    
    import static org.quartz.JobBuilder.newJob;
    import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
    import static org.quartz.TriggerBuilder.newTrigger;
    
    public class QuartzDemo {
        public static void main(String[] args) throws SchedulerException {
            SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
    
            Scheduler sched = schedFact.getScheduler();
    
            sched.start();
    
            // define the job and tie it to our HelloJob class
            JobDetail job = newJob(TimeJob.class)
                    .withIdentity("myJob", "group1")
                    .build();
    
            // Trigger the job to run now, and then every 5 seconds
            Trigger trigger = newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .startNow()
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(5)
                            .repeatForever())
                    .build();
    
            // Tell quartz to schedule the job using our trigger
            sched.scheduleJob(job, trigger);
        }
    }

     

  • 相关阅读:
    pyspark 知识点
    pyspark 读取csv文件创建DataFrame的两种方法
    iOS开发改变字符串中指定字符颜色,大小等等
    iOS开发解决页面滑动返回跟scrollView左右划冲突
    iOS开发自定义试图切换
    iOS开发UIColor,CGColor,CIColor三者的区别和联系
    iOS开发热更新JSPatch
    Swift-创建UIButton(其他UI组件雷同)
    Swift-枚举enum理解
    Swift-闭包理解(二)
  • 原文地址:https://www.cnblogs.com/xiongxx/p/8960832.html
Copyright © 2011-2022 走看看