quartz-2实例

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzManager {
	private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
	private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
	private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
	public static void addJob(String jobName, Class cls, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			// 任务
			JobDetail job = newJob(cls).withIdentity(jobName, JOB_GROUP_NAME)
					.build();
			// 触发器
			CronTrigger trigger = newTrigger()
					.withIdentity(TRIGGER_GROUP_NAME, JOB_GROUP_NAME)
					.withSchedule(cronSchedule(time)).build();
			sched.scheduleJob(job, trigger);
			// 启动
			if (!sched.isShutdown()) {
				sched.start();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static void addJob(String jobName, Class cls, int time) {

		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			// 任务
			JobDetail job = newJob(cls).withIdentity(jobName, JOB_GROUP_NAME)
					.build();
			// 触发器
			SimpleTrigger trigger = (SimpleTrigger) newTrigger()
					.withIdentity(TRIGGER_GROUP_NAME, JOB_GROUP_NAME)
					.startAt(new Date())
					.withSchedule(
							simpleSchedule().withIntervalInSeconds(time)
									.repeatForever()).build();
			sched.scheduleJob(job, trigger);
			// 启动
			if (!sched.isShutdown()) {
				sched.start();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private static void startJobs() {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			sched.start();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private static void shutdownJobs() {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			if (!sched.isShutdown()) {
				sched.shutdown();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static void removeJob(String jobName) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			// 停止触发器
			sched.pauseTrigger(TriggerKey.triggerKey(jobName,
					TRIGGER_GROUP_NAME));
			// 移除触发器
			sched.unscheduleJob(TriggerKey.triggerKey(jobName,
					TRIGGER_GROUP_NAME));
			// 删除任务
			sched.deleteJob(JobKey.jobKey(jobName, JOB_GROUP_NAME));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static void modifyJobTime(String jobName, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey
					.triggerKey(jobName, TRIGGER_GROUP_NAME));
			if (trigger == null) {
				return;
			}
			String oldTime = trigger.getCronExpression();
			if (!oldTime.equalsIgnoreCase(time)) {
				JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName,
						JOB_GROUP_NAME));
				Class objJobClass = jobDetail.getJobClass();
				removeJob(jobName);
				addJob(jobName, objJobClass, time);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static void modifyJobTime(String jobName, int time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			SimpleTrigger trigger = (SimpleTrigger) sched.getTrigger(TriggerKey
					.triggerKey(jobName, TRIGGER_GROUP_NAME));
			if (trigger == null) {
				return;
			}
			JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName,
					JOB_GROUP_NAME));
			Class objJobClass = jobDetail.getJobClass();
			removeJob(jobName);
			addJob(jobName, objJobClass, time);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static boolean isExist(String jobName) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			SimpleTrigger trigger = (SimpleTrigger) sched.getTrigger(TriggerKey
					.triggerKey(jobName, TRIGGER_GROUP_NAME));
			if (trigger == null) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class QuartzJob implements Job {
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
	}
}
public class QuartzTest {
	public static void main(String[] args) {
		try {
			String job_name = "动态任务调度";
			System.out.println("【系统启动】开始(每1秒输出一次)...");
			QuartzManager.addJob(job_name, QuartzJob.class, "0/1 * * * * ?");

			Thread.sleep(5000);
			System.out.println("【修改时间】开始(每2秒输出一次)...");
			QuartzManager.modifyJobTime(job_name, "0/2 * * * * ?");
			Thread.sleep(6000);
			System.out.println("【移除定时】开始...");
			QuartzManager.removeJob(job_name);
			System.out.println("【移除定时】成功");

			System.out.println("【再次添加定时任务】开始(每10秒输出一次)...");
			QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?");
			Thread.sleep(60000);
			System.out.println("【移除定时】开始...");
			QuartzManager.removeJob(job_name);
			System.out.println("【移除定时】成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
时间: 2024-10-18 07:38:40

quartz-2实例的相关文章

quartz定时器实例

配置web <!--启动任务监听器--> <listener> <listener-class>com.suntek.web.pay.listener.ContextListener</listener-class> </listener> 监听类 public class ContextListener implements ServletContextListener { public void contextDestroyed(Servle

Quartz入门实例13-spirng4和quartz2实现动态任务调用

pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <model

quartz入门实例

一 Quarta介绍 1 Quartz是什么 Quartz就是一个纯 Java 实现的作业调度工具,相当于数据库中的 Job.Windows 的计划任务.Unix/Linux 下的 Cron,但 Quartz 可以把排程控制的更精细. 2 Quartz 框架的发展历程 Quartz项目是由James House创立的,在1998年就有该框架最初的构思,包括作业队列的概念,使用线程池来处理作业,在2001年春天在SourceForge 上创立了该项目. Quartz 的目录结构和内容 Docs Q

Spring结合java Quartz配置实例代码

原文:Spring结合java Quartz配置实例代码 源代码下载地址:http://www.zuidaima.com/share/1787232442715136.htm 各种企业应用几乎都会碰到任务调度的需求,就拿论坛来说:每隔半个小时生成精华文章的RSS文件,每天凌晨统计论坛用户的积分排名,每隔30分钟执行锁定用户解锁任务.对于一个典型的MIS系统来说,在每月1号凌晨统计上个月各部门的业务数据生成月报表,每半个小时查询用户是否已经有快到期的待处理业务--,这样的例子俯拾皆是,不胜枚举.

Quartz入门实例14-让Quartz的Job使用Spring注入的Bean

<!-- 由spring生成scheduler --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!--<property name="jobFactory"> <bean class="cn.zto.job.JobBeanJobFactory"

Java任务调度框架Quartz教程实例

介绍: Quartz框架是一个全功能.开源的任务调度服务,可以集成几乎任何的java应用程序-从小的单片机系统到大型的电子商务系统.Quartz可以执行上千上万的任务调度. 核心概念 Quartz核心的概念:scheduler任务调度.Job任务.Trigger触发器.JobDetail任务细节 转自这篇文章对于Quartz定时器写的不错: http://blog.csdn.net/yuebinghaoyuan/article/details/9045471

Quartz简单实例

依赖: 看一下整体结构: 首先定义Job public class HelloJob implements Job { private static Logger _log = LoggerFactory.getLogger(HelloJob.class); public HelloJob() { } public void execute(JobExecutionContext context) throws JobExecutionException { // Say Hello to th

定时执行程序-Quartz简单实例

1.加jar包:Quartz自己quartz-1.8.3.jar和依赖包commons-logging.jar  .slf4j-log4j12-1.5.10.jar .slf4j-api-1.5.10.jar 2.一个类搞定!!!,直接run import  org.quartz.CronTrigger; import  org.quartz.Job; import  org.quartz.JobDetail; import  org.quartz.JobExecutionContext; im

Spring + Quartz配置实例

Spring为创建Quartz的Scheduler.Trigger和JobDetail提供了便利的FactoryBean类,以便能够在Spring 容器中享受注入的好处.此外Spring还提供了一些便利工具类直接将Spring中的Bean包装成合法的任务.Spring进一步降低了使用Quartz的难度,能以更具Spring风格的方式使用Quartz.概括来说它提供了两方面的支持:     1)为Quartz的重要组件类提供更具Bean风格的扩展类:     2)提供创建Scheduler的Bea

quartz定时任务实例

一.spring注解方式 <!--<!–配置文件添加允许Spring注解–>--> <!--<task:annotation-driven scheduler="qbScheduler"/>--> <!--<task:scheduler id="qbScheduler" pool-size="10"/>--> <!--类--> import org.springf