⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scheduler.java

📁 以前做的一个j2ee的项目
💻 JAVA
字号:
package gov.gdlt.ssgly.taxcore.taxblh.htdl;

import java.sql.Time;
import java.util.*;
import java.util.Date;

import gov.gdlt.ssgly.taxcore.comm.config.ApplicationContext;
import gov.gdlt.ssgly.taxcore.comm.log.LogWritter;
import gov.gdlt.ssgly.taxcore.taxdao.htdl.*;


public class Scheduler {
    private static Timer timer = null;
    private static Date first = null;
    private long period; // in millisecond
    private static boolean running = false; //定时器是否运行的标识
    /**
     * Scheduler的静态变量,经过类的初始化后成为全局的一个实例.
     */
    private static Scheduler scheduler = null;
    public Scheduler() {
    }

    public static Scheduler singleton() {
        if (scheduler == null) {
            scheduler = new Scheduler();
        }
        return scheduler;
    }

    public void startScheduler() {
//        HTDLdtrwlbDAO dtrwDAO = new HTDLdtrwlbDAO();
//        if (dtrwDAO.setTimerStarted()) {
        if (!running) {
              timer = new Timer();
              HTDLexecDtrwMultiThread execMutiThread = new
                                                       HTDLexecDtrwMultiThread(); //多线程
          //            HTDLexecDtrw execSingleThread = new HTDLexecDtrw();  //单线程
//              HTDLcreateDtrw createDtrw = new HTDLcreateDtrw();
              timer.schedule(execMutiThread, this.getFirst(),
                             ApplicationContext.HTDL_SCHEDULER_PERIOD);
//              timer.schedule(execMutiThread, 0, ApplicationContext.HTDL_SCHEDULER_PERIOD); //for test
//              timer.schedule(createDtrw,0,ApplicationContext.HTDL_SCHEDULER_PERIOD);  //for test
              running = true;
              LogWritter.sysInfo(
                      "=== Succeed in starting scheduler. The first time will be at " +
                      this.getFirst() +
                      " === period " +
                      ApplicationContext.HTDL_SCHEDULER_PERIOD /
                      3600000 +
                      "H ===");

        } else {
            LogWritter.sysInfo("Warning: the scheduler has been started before!!");
//            throw new TaxBaseBizException("自动调度功能已经启动的情况下,无法再执行启动操作!");
        }
    }

    public void stopScheduler() {
//        HTDLdtrwlbDAO dtrwDAO = new HTDLdtrwlbDAO();
//        if (dtrwDAO.setTimerStopped()) {
        if (running) {
            if (timer != null) timer.cancel();
            running = false;
            LogWritter.sysInfo("The timer used in scheduler has been destroyed!");
        } else {
            LogWritter.sysInfo("Fail to stop scheduler: there is no timer!");
        }
    }

    public Timer getTimer() {
        return timer;
    }

    public long getPeriod() {
//        return period;
        return ApplicationContext.HTDL_SCHEDULER_PERIOD;
    }

//修改,需要读数据库,判断是否启动, zhushi
    public boolean isRunning() {
        return running;
//        boolean result = false;
//        try {
//            HTDLdtrwlbDAO dtrwDAO = new HTDLdtrwlbDAO();
//            result = dtrwDAO.isTimerRunning();
//        } catch (Exception e) {
//            LogWritter.sysDebug(e.getMessage());
//        }
//        return result;
    }

    public Date getFirst() {
//        if (first == null) {
            //从配置文件中读取相关的参数,如任务启动时间,间隔时间
            String tmp = ApplicationContext.singleton().getValueAsString(
                    ApplicationContext.HTDL_SCHEDULER_FIRSTTIME);
            Time firstTime = Time.valueOf(tmp);
            //比较现在的时间,如果今天的时间点已经pass的话,则设为明天
            Calendar c2 = Calendar.getInstance(); //当前时间
//            LogWritter.sysDebug("系统时间: " + c2.getTime());

            Calendar c1 = Calendar.getInstance(); //设定的时间,以时分秒的形式,需要加上当前的年月日
            c1.setTimeInMillis(firstTime.getTime());
            c1.set(Calendar.YEAR, c2.get(Calendar.YEAR));
            c1.set(Calendar.MONTH, c2.get(Calendar.MONTH));
            c1.set(Calendar.DAY_OF_MONTH, c2.get(Calendar.DAY_OF_MONTH));
//            LogWritter.sysDebug("设定的今天时间: " + c1.getTime());

            if (c2.after(c1)) { //如果今天的时间点pass的话,则日期加1天
                c1.add(Calendar.DATE, 1);
//                LogWritter.sysDebug("最后确定的启动时间: " + c1.getTime());
            }
            first = c1.getTime();
//        }
        return first;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public void setFirst(Date first) {
        this.first = first;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -