`
BBjava
  • 浏览: 123797 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Quartz定时器

阅读更多
本例子取自quartz官方。
/* 
 * Copyright 2005 - 2009 Terracotta, Inc. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package org.quartz.examples.example1;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.DateBuilder.*;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This Example will demonstrate how to start and shutdown the Quartz 
 * scheduler and how to schedule a job to run in Quartz.
 * 
 * @author Bill Kratzer
 */
public class SimpleExample {

    
    public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(SimpleExample.class);

        log.info("------- Initializing ----------------------");

        // First we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        log.info("------- Initialization Complete -----------");

        // computer a time that is on the next round minute
        Date runTime = evenMinuteDate(new Date());

        log.info("------- Scheduling Job  -------------------");

        // define the job and tie it to our HelloJob class
        JobDetail job = newJob(HelloJob.class)
            .withIdentity("job1", "group1")
            .build();
        
        // Trigger the job to run on the next round minute
        Trigger trigger = newTrigger()
            .withIdentity("trigger1", "group1")
            .startAt(runTime)
            .build();
        
        // Tell quartz to schedule the job using our trigger
        sched.scheduleJob(job, trigger);
        log.info(job.getKey() + " will run at: " + runTime);  

        // Start up the scheduler (nothing can actually run until the 
        // scheduler has been started)
        sched.start();

        log.info("------- Started Scheduler -----------------");

        // wait long enough so that the scheduler as an opportunity to 
        // run the job!
        log.info("------- Waiting 65 seconds... -------------");
        try {
            // wait 65 seconds to show job
            Thread.sleep(65L * 1000L); 
            // executing...
        } catch (Exception e) {
        }

        // shut down the scheduler
        log.info("------- Shutting Down ---------------------");
        sched.shutdown(true);
        log.info("------- Shutdown Complete -----------------");
    }

    public static void main(String[] args) throws Exception {

        SimpleExample example = new SimpleExample();
        example.run();

    }

}


/* 
 * Copyright 2005 - 2009 Terracotta, Inc. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package org.quartz.examples.example1;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * <p>
 * This is just a simple job that says "Hello" to the world.
 * </p>
 * 
 * @author Bill Kratzer
 */
public class HelloJob implements Job {

	private static Logger _log = LoggerFactory.getLogger(HelloJob.class);

	/**
	 * <p>
	 * Empty constructor for job initilization
	 * </p>
	 * <p>
	 * Quartz requires a public empty constructor so that the scheduler can
	 * instantiate the class whenever it needs.
	 * </p>
	 */
	public HelloJob() {
	}

	/**
	 * <p>
	 * Called by the <code>{@link org.quartz.Scheduler}</code> when a
	 * <code>{@link org.quartz.Trigger}</code> fires that is associated with the
	 * <code>Job</code>.
	 * </p>
	 * 
	 * @throws JobExecutionException
	 *             if there is an exception while executing the job.
	 */
	public void execute(JobExecutionContext context)
			throws JobExecutionException {

		// Say Hello to the World and display the date/time
		_log.info("Hello World! - " + new Date());
	}

}


创建java project,添加上面两个java文件,jar包可以从官方下载,可以直接运行。

HelloJob 继承了 Job,HelloJob就是你要添加的任务,也就是你要实现的业务逻辑,
从SimpleExample中可以看到(注释中)可以看出如何添加一个定时任务。

Trigger,Scheduler



下面是运行结果:
引用
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/quartz-lib/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/G:/workspace/lottery1/WebContent/WEB-INF/lib/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[INFO] 26 三月 03:26:33.067 下午 main [org.quartz.examples.example1.SimpleExample]
------- Initializing ----------------------

[INFO] 26 三月 03:26:33.095 下午 main [org.quartz.impl.StdSchedulerFactory]
Using default implementation for ThreadExecutor

[INFO] 26 三月 03:26:33.097 下午 main [org.quartz.simpl.SimpleThreadPool]
Job execution threads will use class loader of thread: main

[INFO] 26 三月 03:26:33.111 下午 main [org.quartz.core.SchedulerSignalerImpl]
Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

[INFO] 26 三月 03:26:33.112 下午 main [org.quartz.core.QuartzScheduler]
Quartz Scheduler v.2.1.1 created.

[INFO] 26 三月 03:26:33.113 下午 main [org.quartz.simpl.RAMJobStore]
RAMJobStore initialized.

[INFO] 26 三月 03:26:33.113 下午 main [org.quartz.core.QuartzScheduler]
Scheduler meta-data: Quartz Scheduler (v2.1.1) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.


[INFO] 26 三月 03:26:33.113 下午 main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'

[INFO] 26 三月 03:26:33.113 下午 main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler version: 2.1.1

[INFO] 26 三月 03:26:33.113 下午 main [org.quartz.examples.example1.SimpleExample]
------- Initialization Complete -----------

[INFO] 26 三月 03:26:33.114 下午 main [org.quartz.examples.example1.SimpleExample]
------- Scheduling Job  -------------------

[INFO] 26 三月 03:26:33.145 下午 main [org.quartz.examples.example1.SimpleExample]
group1.job1 will run at: Mon Mar 26 15:27:00 CST 2012

[INFO] 26 三月 03:26:33.145 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.

[INFO] 26 三月 03:26:33.146 下午 main [org.quartz.examples.example1.SimpleExample]
------- Started Scheduler -----------------

[INFO] 26 三月 03:26:33.146 下午 main [org.quartz.examples.example1.SimpleExample]
------- Waiting 65 seconds... -------------

[INFO] 26 三月 03:26:35.483 下午 Timer-0 [org.quartz.utils.UpdateChecker]
New Quartz update(s) found: 2.1.3 [http://www.terracotta.org/kit/reflector?kitID=default&pageID=QuartzChangeLog]

[INFO] 26 三月 03:27:00.008 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example1.HelloJob]
Hello World! - Mon Mar 26 15:27:00 CST 2012

[INFO] 26 三月 03:27:38.147 下午 main [org.quartz.examples.example1.SimpleExample]
------- Shutting Down ---------------------

[INFO] 26 三月 03:27:38.147 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.

[INFO] 26 三月 03:27:38.147 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.

[INFO] 26 三月 03:27:38.602 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.

[INFO] 26 三月 03:27:38.602 下午 main [org.quartz.examples.example1.SimpleExample]
------- Shutdown Complete -----------------

分享到:
评论

相关推荐

    Quartz定时器介绍与简单使用

    ### Quartz定时器介绍与简单使用 #### 1.1 Quartz介绍 Quartz 是一款功能强大的开源任务调度框架,它完全采用 Java 编写而成。该框架允许开发人员以灵活的方式定义作业及其触发规则,从而实现对任务的定时调度。...

    quartz定时器源码jar包下载

    作为一个专业的IT行业大师,我很高兴为你解析Quartz定时器的核心概念、功能以及如何通过源码学习。 Quartz定时器允许开发者创建、调度和管理任务,这些任务可以是简单的函数调用或者复杂的业务流程。它支持多种调度...

    Spring Quartz 定时器示例(Web工程版)

    Spring Quartz 定时器示例(Web工程版),欢迎下载。

    Quartz定时器从入门到进阶

    Quartz定时器是一个开源的作业调度框架,专为J2SE和J2EE应用程序设计,完全用Java编写。它的核心优势在于提供强大的灵活性和简单性,使得开发者可以轻松创建简单的或复杂的任务调度。Quartz支持多种特性,如数据库...

    quartz定时器api

    Quartz定时器API是Java平台上一个强大的作业调度框架,它被广泛用于构建自动化任务和后台作业,例如数据备份、报表生成、系统维护等。Quartz提供了丰富的API来创建、管理和控制作业(Jobs)和触发器(Triggers),...

    Quartz定时器事例

    下面,我们将深入探讨Quartz定时器的工作原理、配置、API使用以及在实际项目中的应用案例。 1. **Quartz简介** - Quartz是一个基于JDBC存储的可扩展的作业调度框架,能够用于执行计划性的任务。 - 它支持复杂的...

    java Spring OpenSymphony的Quartz定时器的时间设置

    在Java Spring框架中,开发者有多种选择来实现定时任务的功能,其中最为流行的两种方式分别是使用Java自带的`Timer`类以及OpenSymphony的Quartz定时器。本文将重点探讨Quartz定时器的配置与使用,尤其是其时间设置的...

    一个简单的quartz定时器的demo

    这个"一个简单的quartz定时器的demo"是展示如何在项目中集成和使用Quartz的基本步骤,包括创建任务、配置调度器以及管理任务的生命周期。 首先,Quartz的核心组件包括Scheduler(调度器)、Job(任务)和Trigger...

    Quartz定时器表.sql

    Quartz定时器表 执行语句 方便部署处理数据

    spring boot集成quartz定时器

    本文将详细讲解如何在Spring Boot项目中集成Quartz定时器,以及如何利用Spring的依赖注入特性来实现Job。 一、集成Quartz定时器 1. 添加依赖:首先,你需要在Spring Boot项目的`pom.xml`或`build.gradle`文件中...

    Spring Quartz定时器的jar包

    Spring Quartz定时器是Java开发中常用的一个任务调度框架,它结合了Spring框架的强大功能与Quartz的灵活性,使得开发者能够方便地在应用中实现定时任务。在这个压缩包中,包含了三个核心的jar文件:`quartz-all-...

    Quartz定时器,表达式自动生成工具

    Quartz定时器是一款广泛应用于Java开发中的开源任务调度框架,其功能强大且灵活,能够帮助开发者轻松实现定时任务的管理。在Java应用中,我们常常需要执行一些周期性的任务,如数据备份、清理缓存或者发送邮件等,而...

    [Quartz]Quartz定时器的j2ee系统使用

    Quartz的Hibernate模型 博文链接:https://xmkevinchen.iteye.com/blog/196392

    quartz定时器2.2.1JAR包

    Quartz定时器是一款开源的、功能强大的作业调度框架,它为Java应用程序提供了精确且可扩展的任务调度能力。在Java世界中,Quartz以其灵活性、稳定性和广泛的社区支持而备受推崇。2.2.1版本是Quartz的一个稳定版本,...

    一个基础的Quartz定时器案例

    在这个基础的Quartz定时器案例中,我们将探讨如何使用Quartz API来创建、配置和执行定时任务。 首先,Quartz的核心组件包括Job(任务)、Trigger(触发器)和Scheduler(调度器)。Job是实际需要执行的任务,...

    quartz定时器配置与jar包

    本资料包将详细介绍如何在Spring框架中配置和使用Quartz定时器,并涉及到cron表达式的使用。 一、Quartz简介 Quartz是一个完全由Java编写的作业调度框架,能够精确地调度任务,支持简单或复杂的调度需求。Quartz的...

    quartz 定时器

    #### 一、Quartz定时器概述 ##### 特点: 1. **灵活性**:Quartz能嵌入到任何独立的应用中运行,无论是桌面应用程序还是服务器端应用。 2. **事务支持**:Quartz能够在应用服务器或Servlet容器中实例化,并且能够...

    quartz定时器不依赖任何框架

    Quartz定时器是一款强大且灵活的开源作业调度框架,它允许开发者在Java应用程序中安排复杂的任务执行。Quartz不依赖任何特定的Web或应用服务器框架,因此可以独立使用,这正是"quartz定时器不依赖任何框架"这个主题...

    quartz定时器mysql 脚本

    quartz定时器mysql的脚本,如果需要定时器持久化到数据库,可以使用

    简单实现Spring Quartz定时器

    Spring Quartz定时器是一种在Java应用中实现定时任务的流行框架,它允许开发者精确地调度任务执行。本篇文章将深入探讨如何在Spring框架中简单实现Quartz定时器,并结合源码和工具来帮助理解其工作原理。 首先,让...

Global site tag (gtag.js) - Google Analytics