第一个Quartz项目

本文介绍如何使用Quartz框架在Java项目中实现定时任务。通过具体示例,展示了如何配置Maven依赖,创建并调度一个每两秒执行一次的任务,打印当前时间和HelloWorld!。文章覆盖了从项目准备到任务调度的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 准备工作

1 建立Maven项目工程

2 引入Quartz jar包

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.3</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

二 项目简介

编写Quartz任务,让任务每两秒打印一次helloworld。

1 编写HelloJob

package com.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloJob implements Job{

    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //打印当前执行的时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Exec Time is:" + simpleDateFormat.format(date));
        //编写具体的业务逻辑
        System.out.println("Hello World!");
    }
}

2 编写HelloScheduler

package com.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloScheduler {

    public static void main(String[] args) throws SchedulerException {
        //创建一个JobDetail实例, 将该实例与hellojob class 绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob","group1").build();
        //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger","group1")
                .startNow().withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever()
                ).build();
        //创建Schedule实例
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Time is:" + simpleDateFormat.format(date));
        scheduler.scheduleJob(jobDetail,trigger);
    }
}

三 测试

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Current Time is:2018-11-15 21:49:14

Current Exec Time is:2018-11-15 21:49:14

Hello World!

Current Exec Time is:2018-11-15 21:49:16

Hello World!

Current Exec Time is:2018-11-15 21:49:18

Hello World!

Current Exec Time is:2018-11-15 21:49:20

Hello World!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值