application.xml配置文件:
<bean id="SpringQtzJob" class="cn.aodesoft.test.SpringQtz" /> <!-- 定义调用对象和调用对象的方法 job的配置开始 --> <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject"> <ref bean="SpringQtzJob" /> </property> <!-- 调用类中的方法 --> <property name="targetMethod"> <value>execute</value> </property> <property name="concurrent" value="false" /> </bean> <!-- 调度触发器 触发器Trigger--> <bean id="CronTriggerBean " class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="SpringQtzJobMethod"></property> <property name="cronExpression" value="0/5 * * * * ?"></property> </bean> <!-- 调度工厂--> <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="CronTriggerBean" /> </list> </property> </bean>
任务是过了5秒后,开始执行任务,在执行任务的时候,你会暂停,等任务执行完了,你再重新开始
public class SpringQtz implements InterruptableJob{ @Resource protected UserService userService; @Resource protected MetterService metterService; @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { // 查询事件中 字段是空的,然后查询消息是为发送的状态,假如都成立 List<Integer> userIds = metterService.findStateAndPost(); if (userIds != null) { //不为空 if (userService.findUserIdAndIp_addr(userIds)) { //暂停任务,等待任务执行完成,再开始执行任务,,,但本人不知道怎么做了这一步? //本人要怎么样暂停?怎么样重新开始任务? 求指导 } } } @Override public void interrupt() throws UnableToInterruptJobException { System.out.println("日了狗了"); }
本人用的是spring 3.0 Quartz1.8.5
解决方案:20分
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
// 暂停任务
scheduler.pauseJob(jobKey);
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
// 暂停任务
scheduler.pauseJob(jobKey);
解决方案:20分
需要先获取全部的定时任务,然后遍历获取需要操作的scheduleJob对象,再对这个scheduleJob对象暂停。
获取全部运行中定时任务代码如下:
// SpringJobSchedulerFactoryBean命名首字母小写
Scheduler scheduler = SpringJobSchedulerFactoryBean.getScheduler();
List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(executingJobs.size());
for (JobExecutionContext executingJob : executingJobs) {
ScheduleJob job = new ScheduleJob();
JobDetail jobDetail = executingJob.getJobDetail();
JobKey jobKey = jobDetail.getKey();
Trigger trigger = executingJob.getTrigger();
job.setJobName(jobKey.getName());
job.setJobGroup(jobKey.getGroup());
job.setDesc(“触发器:” + trigger.getKey());
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
job.setJobStatus(triggerState.name());
if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
String cronExpression = cronTrigger.getCronExpression();
job.setCronExpression(cronExpression);
}
jobList.add(job);
}
获取全部运行中定时任务代码如下:
// SpringJobSchedulerFactoryBean命名首字母小写
Scheduler scheduler = SpringJobSchedulerFactoryBean.getScheduler();
List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(executingJobs.size());
for (JobExecutionContext executingJob : executingJobs) {
ScheduleJob job = new ScheduleJob();
JobDetail jobDetail = executingJob.getJobDetail();
JobKey jobKey = jobDetail.getKey();
Trigger trigger = executingJob.getTrigger();
job.setJobName(jobKey.getName());
job.setJobGroup(jobKey.getGroup());
job.setDesc(“触发器:” + trigger.getKey());
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
job.setJobStatus(triggerState.name());
if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
String cronExpression = cronTrigger.getCronExpression();
job.setCronExpression(cronExpression);
}
jobList.add(job);
}