All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.cronjob.CronJob Maven / Gradle / Ivy

There is a newer version: 1.25
Show newest version
package com.github.siwenyan.cronjob;

import com.github.siwenyan.common.DateTimeTools;
import com.github.siwenyan.common.StringTools;
import org.apache.log4j.Logger;

import java.security.InvalidParameterException;
import java.time.DayOfWeek;
import java.util.*;

class CronJob {

    private static final Logger log = Logger.getLogger(CronJob.class.getName());

    private Timer timer = new Timer(true);

    private IWorker worker;

    private String cronJobName = null;

    private boolean available = true;

    private boolean repeating = false;

    private Set repeatDaysOfWeek = new HashSet();

    private Calendar scheduleDate = null;

    private Calendar startTime = null;

    private T subject = null;

    private Date nextTime = null;

    public CronJob(IWorker worker) {
        if (worker == null) {
            throw new InvalidParameterException("Parameter required: worker.");
        }

        this.worker = worker;
    }

    public void resetAlarm() {
        this.nextTime = getNextTime();
        if (this.nextTime != null && this.nextTime.after(new Date())) {
            this.timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    try {
                        worker.work(subject);
                    } catch (WorkException e) {
                        log.error(e.getMessage());
                    } finally {
                        resetAlarm();
                    }
                }

            }, this.nextTime);
        } else {
            this.setAvailable(false);
        }
    }

    private Date getNextTime() {
        Calendar startTime = this.getStartTime();
        if (this.isAvailable() && startTime != null) {

            Calendar now = Calendar.getInstance();

            if (this.isRepeating()) {
                if (repeatDaysOfWeek != null && repeatDaysOfWeek.size() > 0) {
                    startTime.set(now.get(Calendar.YEAR),
                            now.get(Calendar.MONTH), now.get(Calendar.DATE));
                    while (startTime.before(now)
                            || !repeatDaysOfWeek.contains(startTime
                            .get(Calendar.DAY_OF_WEEK))) {
                        startTime.add(Calendar.DATE, 1);
                    }

                    return startTime.getTime();

                } else {
                    return null;
                }
            } else {
                Calendar due = this.getScheduleDate();
                startTime.set(due.get(Calendar.YEAR), due.get(Calendar.MONTH),
                        due.get(Calendar.DATE));
                if (startTime.after(now)) {
                    return startTime.getTime();
                } else {
                    return null;
                }
            }

        } else {
            return null;
        }

    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
        this.nextTime = null;
        if (!this.available) {
            this.timer.cancel();
        }
    }

    public String getCronJobName() {
        return cronJobName;
    }

    public void setCronJobName(String cronJobName) {
        this.cronJobName = cronJobName;
    }

    public boolean isRepeating() {
        return repeating;
    }

    public void setRepeating(boolean repeating) {
        this.repeating = repeating;
    }

    public boolean isRepeatOn(DayOfWeek dayOfWeek) {
        return this.repeatDaysOfWeek.contains(dayOfWeek.getValue());
    }

    public void setRepeatOn(int dayOfWeek, boolean isRepeat) {
        if (isRepeat) {
            this.repeatDaysOfWeek.add(dayOfWeek);
        } else {
            this.repeatDaysOfWeek.remove(dayOfWeek);
        }
    }

    public void setRepeatOn(Set repeatDaysOfWeek, boolean isRepeat) {
        if (repeatDaysOfWeek != null) {
            for (int dayOfWeek : repeatDaysOfWeek) {
                this.setRepeatOn(dayOfWeek, isRepeat);
            }
        }
    }

    public Calendar getScheduleDate() {
        return scheduleDate == null ? null : (Calendar) scheduleDate.clone();
    }

    public void setScheduleDate(Date scheduleDate) {
        this.scheduleDate = DateTimeTools.calendar(scheduleDate);
    }

    public Calendar getStartTime() {
        return startTime == null ? null : (Calendar) startTime.clone();
    }

    public void setStartTime(Date startTime) {
        this.startTime = DateTimeTools.calendar(startTime);
    }

    public T getSubject() {
        return subject;
    }

    public void setSubject(T subject) {
        this.subject = subject;
    }

    @Override
    public String toString() {
        String display = "\r\n";
        display += StringTools.padRight(this.cronJobName, 15, ' ');

        display += "["
                + CronJobManager.formatTime.format(this.startTime.getTime())
                + "]";
        if (repeating) {
            Set repeatDowOut = new HashSet();
            for (int dow : this.repeatDaysOfWeek) {
                dow--;
                if (dow < 1) {
                    dow = 7;
                }
                repeatDowOut.add(DayOfWeek.of(dow));
            }
            display += repeatDowOut.toString();
        } else {
            display += "["
                    + CronJobManager.formatDate.format(this.scheduleDate
                    .getTime()) + "]";
        }
        if (available && this.nextTime != null) {
            display += " Next:";
            display += "[" + CronJobManager.formatDate.format(this.nextTime)
                    + "]";
            display += "[" + CronJobManager.formatTime.format(this.nextTime)
                    + "]";
        } else {
            display += " OFF";
        }

        return display;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy