81,094
社区成员
发帖
与我相关
我的任务
分享
/*
* Copyright 2005 - 2009 Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package com.sanss.job;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MisfireExample {
public static final String SERVICE_ID = "SERVICEID";
public ApplicationContext app;
public Scheduler sched;
public void start(Service service) throws Exception {
String serviceName = service.getServiceName();
Integer serviceId = service.getServiceId();
JobDetail job = new JobDetail(serviceName + "&start", serviceName,
MyQuartzJobBean.class);
job.getJobDataMap().put(MisfireExample.SERVICE_ID, serviceId);
String startTime = service.getServiceStartDate() + " "
+ service.getSmsStartTime() + ":00"; // 开始时间
startTime = "2012-05-23 09:56:00";
CronTrigger ct = new CronTrigger(serviceName + "&start", serviceName,
getCronExpression(startTime));
System.out.println(ct.isVolatile());
sched.pauseTrigger(serviceName + "&start", serviceName);// 停止触发器
sched.unscheduleJob(serviceName + "&start", serviceName);// 移除触发器
sched.scheduleJob(job, ct); // 开始任务
sched.start();
}
public void end(Service service) throws Exception {
String serviceName = service.getServiceName();
Integer serviceId = service.getServiceId();
JobDetail job = new JobDetail(serviceName + "&end", serviceName,
MyQuartzJobBean.class);
job.getJobDataMap().put(MisfireExample.SERVICE_ID, serviceId);
String endTime = service.getServiceEndDate() + " "
+ service.getSmsEndTime() + ":00";// 结束时间
endTime = "2012-05-23 18:53:00";
CronTrigger ct = new CronTrigger(serviceName + "&end", serviceName,
getCronExpression(endTime));
System.out.println(ct.isVolatile());
sched.pauseTrigger(serviceName + "&end", serviceName);// 停止触发器
sched.unscheduleJob(serviceName + "&end", serviceName);// 移除触发器
sched.scheduleJob(job, ct); // 开始任务
sched.start();
// sched.shutdown(true);
}
public static void main(String[] args) {
try {
MisfireExample example = new MisfireExample();
example.app = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
example.sched = (Scheduler) example.app.getBean("quartzScheduler");
ServiceDAO serviceDAO = (ServiceDAO) example.app
.getBean("serviceDAO");
Service service = serviceDAO.findById(8665);
example.start(service);
example.end(service);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getCronExpression(String time) throws Exception {
System.out.println(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse(time);
int seconds = startDate.getSeconds(); // 秒
int minutes = startDate.getMinutes();// 分
int hours = startDate.getHours();// 时
int date = startDate.getDate();// 天
int month = startDate.getMonth() + 1;// 月
System.out.println(hours);
return (seconds + " " + minutes + " " + hours + " " + date + " "
+ month + " ?");
}
}