1、web项目
quarts的配置文件
<!-- 要调用的工作类 -->
<bean id="quartzJobMonitorController" class="*.controller.DeviceStationController"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<!-- 站点监控 -->
<bean id="jobtaskjobtiming"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="quartzJobMonitorController" />
</property>
<property name="targetMethod">
<value>getNewStation_dv</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTimejobtiming"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtaskjobtiming" />
</property>
<!-- 每小时半点处理 -->
<property name="cronExpression">
<value>0 0/5 * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTimejobtiming" />
</list>
</property>
</bean>
2、java项目
2.1quarts的实现
public class QuartzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "PRICE_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "PRICE_TRIGGERGROUP_NAME";
private static String TRIGGER_NAME="PRICE_TRIGGER_NAME";
/**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param jobName
* 任务名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName, String jobClass, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(jobClass)).withIdentity(jobName,
JOB_GROUP_NAME).build();
System.out.println(jobDetail.getKey());
// 触发器
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(
TRIGGER_NAME, TRIGGER_GROUP_NAME).withSchedule(
CronScheduleBuilder.cronSchedule(time)).build();
// 调度器绑定Job 和 Trigger
Date jobRuningTime = sched.scheduleJob(jobDetail, trigger);// 返回Job运行的时间
System.out.println(new SimpleDateFormat("HH:mm:ss").format(jobRuningTime));
// 启动
if (!sched.isShutdown()){
sched.start();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 添加一个定时任务
*
* @param jobName
* 任务名
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param triggerGroupName
* 触发器组名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, String jobClass, String time){
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(jobClass)).withIdentity(jobName,
jobGroupName).build();// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(// 触发器名,触发器组
triggerName, triggerGroupName).withSchedule(
CronScheduleBuilder.cronSchedule(time)).build();
// 调度器绑定Job 和 Trigger
Date jobRuningTime = sched.scheduleJob(jobDetail, trigger);// 返回Job运行的时间
System.out.println(new SimpleDateFormat("HH:mm:ss").format(jobRuningTime));
// 启动
if (!sched.isShutdown()){
sched.start();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 启动所有定时任务
*/
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 关闭所有定时任务
*/
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if(!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public class DeleteDataCrawlJob implements Job{
public void execute(JobExecutionContext context)
throws JobExecutionException {
DeleteTable deleteTable = new DeleteTable();
StringTablename = "WEATHER_TWODAY_DATA";
String field = "FBDATE";
deleteTable.deleteDatesUtil(Tablename,field);
}
}
QuartzManager.addJob("dataJob", "dataJobGroup",
"dataJobTrigger", "dataJobTriggerGroup",
"*.utils.quartz.DeleteDataCrawlJob", "0 0 1 * * ?");
2.2
public class askTimer implements Job{
public staticRealTime part = new RealTime();
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
logger.info("代次计算定时任务启动开始。。。。。。");
TimerTask task = new TimerTask() {
@Override
public void run() {
// 得所有方案ID
part.AllPlanid();
}
};
try {
// 一天的毫秒数
long daySpan = 24 * 60 * 60 * 1000;
// 规定的每天时间02:00:00运行
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd '12:33:00'");
// 首次运行时间
Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(new Date()));
// 如果今天的已经过了 首次运行时间就改为明天
if(System.currentTimeMillis() > startTime.getTime())
startTime = new Date(startTime.getTime() + daySpan);
Timer timer = new Timer(); // 设置执行时间
timer.scheduleAtFixedRate(task, startTime, daySpan);
} catch (ParseException e) {
e.printStackTrace();
}
logger.info("代次计算定时任务启动结束。。。。。。");
}
}