1,服务端
package com.quartz.fsl.test.example12;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.impl.StdSchedulerFactory;
import com.qdwb.mlxc.util.LogSelf;
public class RemoteServerExample {
public void run() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
LogSelf.fslLog.info("------- 手工启动Scheduler -----------------");
sched.start();
LogSelf.fslLog.info("------- Waiting 2 minutes... ------------");
try {
Thread.sleep(120L * 1000L);
} catch (Exception e) {
}
LogSelf.fslLog.info("------- 停止Shutting---------------------");
sched.shutdown(true);
SchedulerMetaData metaData = sched.getMetaData();
LogSelf.fslLog.info("Executed " + metaData.getNumberOfJobsExecuted()+" jobs.");
}
public static void main(String[] args) throws Exception {
RemoteServerExample example = new RemoteServerExample();
example.run();
}
}
2,客户端
package com.quartz.fsl.test.example12;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.qdwb.mlxc.util.LogSelf;
public class RemoteClientExample {
public void run() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
JobDetail job = JobBuilder.newJob(SimpleJob.class).withIdentity("remotelyAddedJob", "default").build();
JobDataMap map = job.getJobDataMap();
map.put("msg", "quartz2.2.1信息");
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(
"remotelyAddedTrigger", "default").forJob(job.getKey())
.withSchedule(CronScheduleBuilder.cronSchedule("/5 * * ? * *"))
.build();
LogSelf.fslLog.info("Add the given JobDetail to the Scheduler, and associate the given Trigger with it");
sched.scheduleJob(job, trigger);
}
public static void main(String[] args) throws Exception {
RemoteClientExample example = new RemoteClientExample();
example.run();
}
}
3,任务
package com.quartz.fsl.test.example12;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import com.qdwb.mlxc.util.LogSelf;
public class SimpleJob implements Job {
public static final String MESSAGE = "msg";
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey();
String message = (String) context.getJobDetail().getJobDataMap().get("msg");
LogSelf.fslLog.info("SimpleJob任务: " + jobKey+"----msg:"+message);
}
}
-------------------------------------------------------测试结果:
这个结果理论上应该是不对,期待效果是:
服务器端启动后,其实已经启动了schedule调度,而在客户端中生成并往schedule中配置好了job,但job并未执行,原因可能是schedule并非同一个实例导致
麻烦各位参考并指导真相,不胜感谢