java定时器的使用(Scheduler )

首先添加所需要的类包: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();
        }
 }
}

转载于:https://www.cnblogs.com/wyqtomorrow/archive/2007/04/28/730963.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值