zoukankan      html  css  js  c++  java
  • Quartz的Hello world

    1.准备环境jar包

    Your project will need (at least) the Quartz core library, named quartz-x.y.z.jar (where x.y.z is a version number), in its classpath.if you are a Maven user you can add the dependencies (only the first below is required):

     <dependency>
          <groupId>org.quartz-scheduler</groupId>
          <artifactId>quartz</artifactId>
          <version>2.2.1</version>
      </dependency>
      <dependency>
          <groupId>org.quartz-scheduler</groupId>
          <artifactId>quartz-jobs</artifactId>
          <version>2.2.1</version>
      </dependency>
    2.通过工厂调用Schedulers,只有当start()方法被调用时,才会触发定时器操作。
    // Grab the Scheduler instance from the Factory
      Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    
      // and start it off
      scheduler.start();
    3.引入相关API
     import org.quartz.Scheduler;
      import org.quartz.SchedulerException;
      import org.quartz.impl.StdSchedulerFactory;
      import static org.quartz.JobBuilder.*;
      import static org.quartz.TriggerBuilder.*;
     import static org.quartz.SimpleScheduleBuilder.*;

    4.实现Job接口,并编写execute方法
    public class MyJob implements org.quartz.Job {
    
          public MyJob() {
          }
    
          public void execute(JobExecutionContext context) throws JobExecutionException {
              System.err.println("Hello World!  MyJob is executing.");
          }
      }
    5.And then schedule those jobs with triggers that define at what time(s) the job should run.
    // define the job and tie it to our MyJob class
      JobDetail job = newJob(MyJob.class)
          .withIdentity("job1", "group1")
          .build();
    
      // Trigger the job to run now, and then repeat every 40 seconds
      Trigger trigger = newTrigger()
          .withIdentity("trigger1", "group1")
          .startNow()
          .withSchedule(simpleSchedule()
                  .withIntervalInSeconds(40)
                  .repeatForever())
          .build();
    
      // Tell quartz to schedule the job using our trigger
      scheduler.scheduleJob(job, trigger);

  • 相关阅读:
    Struts2 HelloWorld_1
    Java Web JavaMail 邮件发送
    Struts2 ActionWildcard(通配符配置)约定优于配置
    Struts2 Action
    Struts2 struts2简介
    Struts2 ActionMethod DMI(动态方法调用)
    Java Web Servlet过滤器
    com学习笔记(2)基本的com接口QueryInterface的实现
    com学习笔记(4)动态链接
    silverlight Visifire图表转图片偷天换日的做法
  • 原文地址:https://www.cnblogs.com/ningJJ/p/5364171.html
Copyright © 2011-2022 走看看