java调度器

 首先添加所需要的类包:quartz-1.5.2.jar,quartz-all-1.5.2.jar,quartz-jboss-1.5.2.jar
//设置定时作业
public void startScheduler(HttpServletRequest requests)throws SchedulerException{
try{
   //建立作业调度器
   Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
   //判断作业调度内是否有作业,如果有将其删除
   if (!scheduler.isShutdown()) {
                scheduler.deleteJob("ReceiveOnTimed", Scheduler.DEFAULT_GROUP);
            }
   //删除调度器的作业后,新建一个我们现在要完成的作业,该作业所需要的类是ReceiveJobd.class,作业名字是ReceiveOnTimes,所属分组是Scheduler.DEFAULT_GROUP
            JobDetail jobDetail = new JobDetail("ReceiveOnTimed",
                                                Scheduler.DEFAULT_GROUP,
                                                ReceiveJobd.class);
            int m = ips.length;
            int n = tablenames.length;
            //向作业内设置要传入的参数
            jobDetail.getJobDataMap().put("scheduler", scheduler);
            jobDetail.getJobDataMap().put("ipLength", m);
            jobDetail.getJobDataMap().put("tablenameLength", n);
            jobDetail.getJobDataMap().put("wcx",wc);
            for (int i = 0; i < m; i++) {
                jobDetail.getJobDataMap().put("ip" + i, ips[i]);
                for (int j = 0; j < n; j++) {
                    jobDetail.getJobDataMap().put("tablename" + j, tablenames[j]);
                }
            }
            //建立触发器,判断何时触发该作业,参数为触发器的名称,触发器分组,作业名称,作业分组,时间设定
            Trigger trigger = new CronTrigger("ReceiverOnTimeTriggerd",
                                              scheduler.DEFAULT_GROUP,
                                              "ReceiveOnTimed",
                                              scheduler.DEFAULT_GROUP, time);
            //将作业和触发器添加到调度器
            scheduler.scheduleJob(jobDetail, trigger);
            //按照设置开始调度
            scheduler.start();
}catch(Exception e){
   e.printStackTrace();
}
}
具体作业类的处理:
package com.bodhiya.schsystem.business;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.springframework.web.context.WebApplicationContext;

public class ReceiveJobd implements Job{
public void ReceiveJobd(){
}
//作业具体实现的功能
public void execute(JobExecutionContext context) throws
    JobExecutionException {
     try {
          
            //建立JobDataMap对象,用来接收传递的参数
            JobDataMap dataMap = context.getMergedJobDataMap();
            int ipLength = dataMap.getInt("ipLength");
            int tablenameLength = dataMap.getInt("tablenameLength");
            WebApplicationContext wc=(WebApplicationContext)dataMap.get("wcx");
          
            String ip = "";
            String tablename = "";
            for (int i = 0; i < ipLength; i++) {
                ip = dataMap.getString("ip" + i);
                for (int j = 0; j < tablenameLength; j++) {
                    tablename = dataMap.getString("tablename" + j);
                    ExchangeAccess ea=new ExchangeAccess(ip,tablename,wc);
                     //启动线程
                    ea.start();
                }
            }
            Scheduler scheduler = (Scheduler) dataMap.get("scheduler");
            scheduler.deleteJob("ReceiveOnTimed", Scheduler.DEFAULT_GROUP);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
}
}

你可能感兴趣的:(java调度器)