public static void main(String[] args) throws SchedulerException { // 实现 Job 接口,可使 Java 类变为可调度的任务 // 创建描述 Job 的 JobDetail 对象 JobDetailImpl jobDetailImpl = new JobDetailImpl(); jobDetailImpl.setName("myJob2"); jobDetailImpl.setGroup("tem"); jobDetailImpl.setJobClass(MyJob2.class); jobDetailImpl.setName("myJob"); jobDetailImpl.setGroup("Two"); jobDetailImpl.setJobClass(MyJob.class); // 创建 SimpleTrigger 对象 SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setName("myTrigger"); trigger.setGroup("Two"); // 设置触发 Job 执行的时间规则 trigger.setStartTime(new Date()); trigger.setRepeatCount(10); trigger.setRepeatInterval(1000 * 3); // 通过 SchedulerFactory 获取 Scheduler 对象 StdSchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler(); // 向 SchedulerFactory 中注册 JobDetail 和 Trigger scheduler.scheduleJob(jobDetailImpl, trigger); // 启动调度任务 scheduler.start(); }
public void runQuarts() throws Exception { // 定时任务 ,分组名称信息,分组名 如果不设置分组名 是默认分组Default JobDetail jobDetail = new JobDetailImpl("myJobName", "myJobGroupName", QuartsDemo.class); // 设置触发器 SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl("myTiggerName"); // 触发器 从当前时间开始 simpleTrigger.setStartTime(new Date()); // 调用10次 simpleTrigger.setRepeatCount(10); // 每隔2000毫秒 / 2秒 simpleTrigger.setRepeatInterval(1000); // 定义调度 Scheduler scheudle = new StdSchedulerFactory().getScheduler(); scheudle.scheduleJob(jobDetail, simpleTrigger); // 调度启动 scheudle.start(); Thread.currentThread().sleep(10000); // 调度关闭 System.err.println("关闭 "); scheudle.shutdown(); }