直接上代码
job任务
package com.app.quartz;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @Author: chao.zhu
* @description:
* @CreateDate: 2018/08/13
* @Version: 1.0
*/
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
context.getTrigger();
JobDetail jobDetail = context.getJobDetail();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
String name = jobDataMap.getString("name");
System.out.println("定时任务job1被调用,hello world:"+name);
}
}
调试类:
package com.app.quartz;
import java.util.Date;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkNodeExistsException;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
/**
* @Author: chao.zhu
* @description:
* @CreateDate: 2018/08/13
* @Version: 1.0
*/
public class QuartzTest implements Runnable{
private static final String ZK_CONNECTION = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
public static Scheduler scheduler ;
public static void main(String[] args) throws SchedulerException {
//创建一个定时任务调度类
scheduler = StdSchedulerFactory.getDefaultScheduler();
for(int i = 0 ; i < 10 ; i++){
QuartzTest quartzTest = new QuartzTest();
Thread thread = new Thread(quartzTest);
thread.start();
}
}
@Override
public void run() {
test(scheduler);
}
public static void test(Scheduler scheduler){
try {
ZkClient zkClient = new ZkClient(ZK_CONNECTION,5000,5000);
//创建job任务,这个job任务是我们之前创建的MyJob
for(int k = 0 ; k < 10 ;k++){
//创建触发器:触发执行job任务
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger"+k,"test")
.startAt(new Date())
.withSchedule(
SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10+k).repeatForever())
.build();
//job任务需要的参数
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("name","zhuchao");
JobDetail jobDetail = JobBuilder.newJob(MyJob.class).usingJobData(jobDataMap).withIdentity("job"+k,"test").build();
JobKey jobKey = jobDetail.getKey();
String znodeName = jobKey.toString();
try{
zkClient.createEphemeral("/locks/"+znodeName);
//将job任务和触发器捆绑在一起,添加到调度类中
scheduler.scheduleJob(jobDetail,trigger);
//启动调度类
System.out.println("线程:"+Thread.currentThread().getName()+"任务:"+znodeName);
}catch (ZkNodeExistsException e){
//zkClient.close();
}
}
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
输出结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49957:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/tools.jar:/Users/zhuchao/workspace_my_git/zookeepertest02/target/classes:/Users/zhuchao/maven_repository/repository/org/apache/zookeeper/zookeeper/3.4.6/zookeeper-3.4.6.jar:/Users/zhuchao/maven_repository/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar:/Users/zhuchao/maven_repository/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar:/Users/zhuchao/maven_repository/repository/log4j/log4j/1.2.16/log4j-1.2.16.jar:/Users/zhuchao/maven_repository/repository/jline/jline/0.9.94/jline-0.9.94.jar:/Users/zhuchao/maven_repository/repository/junit/junit/3.8.1/junit-3.8.1.jar:/Users/zhuchao/maven_repository/repository/io/netty/netty/3.7.0.Final/netty-3.7.0.Final.jar:/Users/zhuchao/maven_repository/repository/org/apache/commons/commons-lang3/3.7/commons-lang3-3.7.jar:/Users/zhuchao/maven_repository/repository/com/alibaba/fastjson/1.2.37/fastjson-1.2.37.jar:/Users/zhuchao/maven_repository/repository/com/101tec/zkclient/0.10/zkclient-0.10.jar:/Users/zhuchao/maven_repository/repository/org/quartz-scheduler/quartz/2.2.1/quartz-2.2.1.jar:/Users/zhuchao/maven_repository/repository/c3p0/c3p0/0.9.1.1/c3p0-0.9.1.1.jar:/Users/zhuchao/maven_repository/repository/org/quartz-scheduler/quartz-jobs/2.2.1/quartz-jobs-2.2.1.jar com.app.quartz.QuartzTest
objc[912]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x10f1e64c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x110a194e0). One of the two will be used. Which one is undefined.
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
线程:Thread-3任务:test.job1
线程:Thread-6任务:test.job2
线程:Thread-2任务:test.job3
线程:Thread-6任务:test.job4
线程:Thread-3任务:test.job5
线程:Thread-5任务:test.job7
线程:Thread-5任务:test.job8
线程:Thread-5任务:test.job9
定时任务job1被调用,hello world:zhuchao
定时任务job1被调用,hello world:zhuchao
定时任务job1被调用,hello world:zhuchao
定时任务job1被调用,hello world:zhuchao
网友评论