com.daioware.processManagement.ScheduledDateProcess Maven / Gradle / Ivy
package com.daioware.processManagement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import com.daioware.stream.Printer;
public class ScheduledDateProcess extends Process implements Iterable{
private ArrayList dates=new ArrayList<>();
protected ScheduledDate nearestDate;
public ScheduledDateProcess(Runnable procedure, Printer printer, String name, long sleepingMilis) {
super(procedure, printer, name, sleepingMilis);
}
public ScheduledDateProcess(Runnable procedure, String name) {
super(procedure, name);
}
public ScheduledDateProcess(Runnable procedure) {
super(procedure);
}
public Process shallowCopy() {
synchronized (this) {
ScheduledDateProcess copy=new ScheduledDateProcess(getRunnable());
copy(this,copy);
copy.dates=dates;
copy.nearestDate=nearestDate;
return copy;
}
}
public void addDates(Date... dates) {
for(Date date:dates) {
addDate(date);
}
}
public void addDates(List dates) {
for(Date date:dates) {
addDate(date);
}
}
public boolean addDate(Date date) {
return addDate(new ScheduledDate(date));
}
public boolean addDate(ScheduledDate date) {
return dates.add(date);
}
public ScheduledDate removeDate(int index) {
return dates.remove(index);
}
public ScheduledDate getDate(int index) {
return dates.get(index);
}
public int datesSize() {
return dates.size();
}
@Override
public Iterator iterator() {
return dates.iterator();
}
public boolean allDatesExecuted() {
for(ScheduledDate sDate:this) {
if(!sDate.isExecuted()) {
return false;
}
}
return true;
}
protected long getNextExecutionSleepTime() {
if(datesSize()<1 || allDatesExecuted()) {
setActive(false);
return 1;
}
Date now=new Date();
nearestDate=null;
long nowTime=now.getTime();
long nearestTime=-1,auxTime;
for(ScheduledDate sDate:this) {
if(!sDate.isExecuted()) {
auxTime=Math.abs(nowTime-sDate.getDate().getTime());
if(nearestTime==-1) {
nearestTime=auxTime;
nearestDate=sDate;
}
else if(auxTime>=0 && nearestTime>auxTime) {
nearestTime=auxTime;
nearestDate=sDate;
}
//else nearestTime and nearestDate remains the same
}
}
return nearestTime;
}
public void setRunningFields() {
if(nearestDate!=null) {
nearestDate.setExecuted(true);
nearestDate=null;
}
}
public boolean isTimeToRun() {
if(nearestDate!=null) {
return true;
}
else {
setSleepingMillis(getNextExecutionSleepTime());
return false;
}
}
private static void proc() {
System.out.println("Executing process");
}
public static void main(String[] args) {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
ProcessManager manager=new ProcessManager();
ScheduledDateProcess proc=new ScheduledDateProcess(ScheduledDateProcess::proc);
Calendar c=Calendar.getInstance();
System.out.println("Starting main at "+dateFormat.format(c.getTime()));
for(int i=0;i<3;i++) {
c.add(Calendar.HOUR,1);
proc.addDate(c.getTime());
}
proc.setFormatSleepingDuration(Process.FORMAT_SECONDS);
proc.setSleepBetweenRuns(false);
manager.startProcess(proc);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy