[size=large]问题1:我想在一个任务执行后在执行第二个任务怎么办呢
Quartz:使用JobListener(任务监听器可实现)
下面示例中演示了相互传递,请参见注释
Job1Listener.java
public class Job1Listener implements JobListener {
private static Logger _log = LoggerFactory.getLogger(Job1Listener.class);
public String getName() {
return "job1_to_job2";
}
public void jobToBeExecuted(JobExecutionContext inContext) {
//任务监听器执行任务1之前你可以选择的操作
_log.error("Job1Listener says: Job Is about to be executed.");
//向任务1传递数据
inContext.getJobDetail().getJobDataMap().put("username", "天涯");
}
public void jobExecutionVetoed(JobExecutionContext inContext) {
_log.error("Job1Listener says: Job Execution was vetoed.");
}
public void jobWasExecuted(JobExecutionContext inContext,JobExecutionException inException) {
_log.error("Job1Listener says: Job was executed.");
//执行任务2
JobDetail job2 = newJob(SimpleJob2.class)
.withIdentity("job2")
.usingJobData("username", "david") //向任务2传递数据传递可成功接收
.build();
Trigger trigger = (SimpleTrigger) newTrigger()
.withIdentity("job2Trigger")
.startNow()
.build();
//向任务2传递数据,这里不知怎么回事,传递参数在任务2取值为: null
inContext.getJobDetail().getJobDataMap().put("sex", "男");
try {
// schedule the job to run!
inContext.getScheduler().scheduleJob(job2, trigger);
} catch (SchedulerException e) {
_log.error("出错了...");
e.printStackTrace();
}
}
}
ListenerExample.java
Quartz2个人感觉设计相当灵活,脱离了相互依赖,只需要添加监听器相关设置就行了
注意下面代码:
JobListener listener = new Job1Listener();
Matcher<JobKey> matcher = KeyMatcher.keyEquals(job.getKey());
sched.getListenerManager().addJobListener(listener, matcher);
这个任务1实现了监听器 joblistener,在这个 joblistener中有四个方法实现,在
public void jobWasExecuted(JobExecutionContext inContext,JobExecutionException inException) {
}
中去执行任务2的操作,以次类推的话,它可执行一个任务集合了
public void run() throws Exception {
Logger log = LoggerFactory.getLogger(ListenerExample.class);
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
JobDetail job = newJob(SimpleJob1.class)
.withIdentity("job1")
.build();
Trigger trigger = newTrigger()
.withIdentity("trigger1")
.startNow()
.build();
JobListener listener = new Job1Listener();
Matcher<JobKey> matcher = KeyMatcher.keyEquals(job.getKey());
sched.getListenerManager().addJobListener(listener, matcher);
sched.scheduleJob(job, trigger);
sched.start();
try {
Thread.sleep(30L * 1000L);
} catch (Exception e) {
}
sched.shutdown(true);
SchedulerMetaData metaData = sched.getMetaData();
log.error("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
}
SimpleJob1.java
public class SimpleJob1 implements Job {
private static Logger _log = LoggerFactory.getLogger(SimpleJob1.class);
public SimpleJob1() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey();
String userName = (String)context.getJobDetail().getJobDataMap().get("username");
_log.error("SimpleJob1 says: " + jobKey + " executing at " + new Date() + ",姓名: " + userName);
}
}
SimpleJob2.java
public class SimpleJob2 implements Job {
private static Logger _log = LoggerFactory.getLogger(SimpleJob2.class);
public SimpleJob2() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey();
String userName = (String)context.getJobDetail().getJobDataMap().getString("username");
String sex = (String)context.getJobDetail().getJobDataMap().getString("sex");
_log.error("SimpleJob1 says: " + jobKey + " executing at " + new Date() + ",英文名: " + userName+",性别: " + sex);
}
}
输出结果为:
[ERROR] 2011-08-12 03:56:44.750 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example9.Job1Listener]
Job1Listener says: Job Is about to be executed.
[ERROR] 2011-08-12 03:56:44.765 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example9.SimpleJob1]
SimpleJob1 says: DEFAULT.job1 executing at Fri Aug 12 15:56:44 CST 2011,姓名: 天涯
[ERROR] 2011-08-12 03:56:44.765 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example9.Job1Listener]
Job1Listener says: Job was executed.
[ERROR] 2011-08-12 03:56:44.765 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example9.SimpleJob2]
SimpleJob1 says: DEFAULT.job2 executing at Fri Aug 12 15:56:44 CST 2011,英文名: david,性别: null
[/size]
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = CCMSScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.instanceIdGenerator.class=org.quartz.simpl.HostnameInstanceIdGenerator
org.quartz.scheduler.skipUpdateCheck = true
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#============================================================================
# Configure JobStore
#============================================================================
#don't fire the job if timeout>=2 hours
org.quartz.jobStore.misfireThreshold = 7200000
org.quartz.jobStore.dontSetAutoCommitFalse=false
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT
#org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.RamJobStore
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.tablePrefix = TWF_QRTZ_
org.quartz.jobStore.isClustered = false