java定时器的使用(Scheduler )

编程技术  /  houtizong 发布于 3年前   107
首先添加所需要的类包: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();
        }
}
}
上一篇:JDBC基础知识
下一篇:JAVA定时器

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客