Quartz of Cron Job 使用

Java 定时器之 Quartz:



package com.wx.email.cronjob;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

import com.wx.email.smtp.EmailNewsBiz;

/**
* @Class name: CronJobScheduler.java
*
* Short description on the purpose of the program.
*
* @author: wangxiang
* @modified: Jan 7, 2011
*
*/

public class CronJobScheduler {

private static final long serialVersionUID = -5838740332643296770L;

private static CronJobScheduler cronJobSCheduler;

public static CronJobScheduler getCronJobSchedulerInstance() {
if (null == cronJobSCheduler) {
cronJobSCheduler = new CronJobScheduler();
}
return cronJobSCheduler;
}

private Scheduler scheduler = null;

public void initCronJobScheduler() {
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();

JobDetail job = new JobDetail("job1", Scheduler.DEFAULT_GROUP, EmailNewsBiz.class);

String qz = DefaultSCProperties.getPropertyAsString("email.scheduler.quartz", "0 0 1 * * ?");
CronTrigger trigger = new CronTrigger("trigger1", Scheduler.DEFAULT_GROUP, qz);

scheduler.scheduleJob(job, trigger);

} catch (Exception e) {
e.printStackTrace();
}
}

public void destroyCronJobScheduler() {
try {
if (scheduler != null)
scheduler.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}

}
}



Biz Class:

public class EmailNewsBiz implements Job {

public void execute(JobExecutionContext arg0) throws JobExecutionException {
sendMailNews();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quartz is a popular open-source job scheduling library in Java that allows developers to schedule tasks to run at specific times or intervals. It supports a cron-like syntax for defining schedules, known as Cron Triggers. A cron expression is a string that represents a schedule in a specific format. It consists of six fields (second, minute, hour, day of month, month, day of week) separated by spaces. Each field can contain a specific value, a range of values, or wildcards to represent all possible values. For example, the cron expression "0 0 12 * * ?" represents a schedule that triggers at 12 PM (noon) every day. Quartz provides a CronTrigger class to create and manage cron-based schedules in Java applications. It allows you to define the schedule using the cron expression and associate it with a specific job to be executed. Here's an example of using Quartz to schedule a job with a cron trigger in Java: ```java import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class QuartzExample { public static void main(String[] args) throws SchedulerException { // Define a job JobDetail job = JobBuilder.newJob(MyJob.class) .withIdentity("myJob", "group1") .build(); // Define a trigger with a cron expression Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("0 0 12 * * ?")) .build(); // Create a scheduler factory SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); // Schedule the job with the trigger scheduler.scheduleJob(job, trigger); // Start the scheduler scheduler.start(); } } public class MyJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { // Job logic goes here System.out.println("Hello, Quartz!"); } } ``` In this example, the `QuartzExample` class schedules a job (`MyJob`) to run at 12 PM every day using a cron trigger. When the trigger fires, the `execute` method of the `MyJob` class will be executed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值