The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Help w/ Runtime's exec() method and scheduled tasks

Posted by Hiran on November 20, 2001 at 10:04 AM

Please take a look at the following code:


package ExecuteApp;
import java.util.*;
import java.io.*;

public class TaskScheduleRun {
private int day;
private int month;
private int year;
private int ampm;
private int hour;
private int minute;
private String taskNumber;
private String taskName;
private String taskDate;
private String taskTime;
private String taskYear;
private String taskMonth;
private String taskDay;
private String taskHour;
private String taskMinute;
private String taskAMPM;
private String scheduleTasksFile;
private GregorianCalendar cal;
private BufferedReader fileIn;
private ExecuteProcess appToExecute; //A method that executes a process using Runtime.getRuntime.exec(String cmd);

public TaskScheduleRun(String scheduleTasksFile) {
this.scheduleTasksFile = scheduleTasksFile;
}

public void runTasks() {
while (true) {
cal = new GregorianCalendar();
try {
fileIn = new BufferedReader(new FileReader(scheduleTasksFile));
} catch (FileNotFoundException e) {
}
day = cal.get(cal.DATE);
month = cal.get(cal.MONTH);
year = cal.get(cal.YEAR);
ampm = cal.get(cal.AM_PM);
hour = cal.get(cal.HOUR);
minute = cal.get(cal.MINUTE);
try {
checkForEquivalence();
} catch (IOException e) {
}
}
}

private void checkForEquivalence() throws IOException {
while((taskNumber = fileIn.readLine()) != null) {
taskDate = fileIn.readLine();
taskTime = fileIn.readLine();
taskName = fileIn.readLine();
breakTaskDate();
breakTaskTime();
if (dateEqual() && timeEqual()) {
if (taskName.equals("start \"c:\\windows\\scandskw.exe\"") || taskName.equals("start \"c:\\windows\\defrag.exe\"")) {
appToExecute = new ExecuteProcess("start \"c:\\program files\\compstart controller\\apps\\processkiller.exe\"");
try {
Thread.currentThread().sleep(15 * 1000);
} catch (InterruptedException e) {
}
appToExecute = new ExecuteProcess(taskName);
appToExecute = new ExecuteProcess("start \"c:\\program files\\compstart controller\\apps\\rebooter.exe\"");
} else {
appToExecute = new ExecuteProcess(taskName);
}
}
}
}

private void breakTaskDate() {
int numBackSlash = 0;
int posFirstBackSlash = 0;
int posSecondBackSlash = 0;
for (int i=0; i if (taskDate.charAt(i) == '/') {
if (numBackSlash == 0) {
numBackSlash++;
posFirstBackSlash = i;
taskDay = taskDate.substring(0, i);
} else if(numBackSlash == 1) {
numBackSlash++;
posSecondBackSlash = i;
taskMonth = taskDate.substring(posFirstBackSlash + 1, i);
}
} else if(numBackSlash == 2) {
numBackSlash = -1;
taskYear = taskDate.substring(posSecondBackSlash + 1, taskDate.length());
}
}
}

private void breakTaskTime() {
int numColon = 0;
int posColon = 0;
for (int i=0; i if (taskTime.charAt(i) == ':') {
if (numColon == 0) {
numColon++;
posColon = i;
taskHour = taskTime.substring(0, i);
}
} else if (numColon == 1) {
numColon = -1;
taskMinute = taskTime.substring(posColon + 1, taskTime.length() - 2);
} else if (taskTime.charAt(i) == ' ') {
taskAMPM = taskTime.substring(i + 1, taskTime.length());
}
}
}

private boolean dateEqual() {
boolean equal = false;
if (taskDay.equals(String.valueOf(day)) && taskMonth.equals(String.valueOf(month)) && taskYear.equals(String.valueOf(year))) {
equal = true;
}
return equal;
}

private boolean timeEqual() {
boolean equal = false;
if (taskHour.equals(String.valueOf(hour)) && taskMinute.equals(String.valueOf(minute)) && taskAMPM.equals(String.valueOf(ampm))) {
equal = true;
}
return equal;
}
}


What's happening is that for each iteration of the while loop, I'm calculating the current day (date), month, year, minute, hour, and time of day (AM/PM). I then create a BufferedReader to read from a file that contains the schedule of tasks to run (including the time and date to run them). I then parse the strings containing the time and date of each task so that I can eventually check to see if it's time to run that task (ie, the current time and date is equal to the time and date specified for that task). (This is what the methods breakTaskDate(), breakTaskTime(), dateEqual() and timeEqual() do). If they are equal (ie, it's time to run the task), I then run the specified task. The only problem is that the while loop will loop through a number of times during a minute, which means that the task gets executed a number of times (when all that's needed is once). I thought of going down to the second and millisecond to overcome this, but even then a while loop iterates through quite a few times during a millisecond. The way I see it, the only way I can stop the task from being executed multiple times is to keep track of whether a task has been executed (possibly using a boolean variable). But I don't think that'll work because every time I iterate through the loop, I "close the file and open it up again to be able to read from the beginning" by creating a new BufferedReader object. Does anyone know how I can cause a task that's scheduled to be executed to execute only once at the precise moment, and not multiple times? Thanks in advance for the help!
Hiran



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us