Timer.ScheduleAtFixedRate(Timer t,Date d,Long l) is failing after 3days
807603Jan 16 2008 — edited Jan 18 2008We have an application which downloads mails from GroupWise Server. We have implemented with SwingWorker abstract class(java.sun.com/products/jfc/tsc/articles/threads/src/SwingWorker.java) and thread pool. Thread pool will download mails from user MailBox. Same time more than one user mails can be down loaded based on no. of threads define in thread pool. SwingWorker invokes this thread pool.
On top of this I have implemented a Timer to Schedule this process once in a day at a particular time. I used Timer.ScheduleAtFixedRate(Timer t,Date d,Long l). I set to run at every day at 5.30PM. I am starting SwingWorker thread inside this timer task method. It runs perfectly for first three days. Fourth day it runs twice. That is after one round of mail download is over for all user and after few minutes it downloads one more time. Next three days fine. It will run only once 5.30Pm and again 4th day same problem.
Can any one help to find what could be the probem?
Here is the timer code.
String dailyFreq = options.getSchedularTime();
processTimer = new Timer(true);//java.util timer class
Date startDate = getDailyFreq();
ScheduledProcess scheduledProcess = new ScheduledProcess(iia_DefaultMutableTreeNodeProcessorHandle, iia_frame, pool, startDate);
processTimer.scheduleAtFixedRate(scheduledProcess, startDate , 1000 * 60 * 60 * 24);
-----------------------------------------------------------------------------------------------------------------------------
ScheduledProcess.java
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.computhink.iia.Constants;
import com.computhink.iia.logging.VWLogger;
import com.computhink.iia.options.IAAGeneralOptions;
import com.computhink.iia.ui.util.SwingWorker;
import com.computhink.iia.ui.util.ThreadPool;
class ScheduledProcess extends TimerTask {
private static Logger logger = Logger.getLogger("com.computhink.iia");
private ThreadPool tempPool;
IIA_JFrame iia_frm = null;
IIA_DefaultMutableTreeNodeProcessor iia_DefaultMutableTreeNodeProcessorHdle = null;
Date startDate = null;
private static int count = 0;
public ScheduledProcess(IIA_DefaultMutableTreeNodeProcessor iia_DefaultMutableTreeNodeProcessorHandle,
IIA_JFrame iia_frame, ThreadPool pool, Date startDate){
this.iia_DefaultMutableTreeNodeProcessorHdle = iia_DefaultMutableTreeNodeProcessorHandle;
this.tempPool = pool;
this.iia_frm = iia_frame;
this.startDate = startDate;
}
public void doScheduledProcess(){
SwingWorker worker = new SwingWorker() {
public Object construct() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
int addDays = 1;
while (!Constants.stopFlag ) {
Date now = Calendar.getInstance().getTime();
// This method 'pollFoldersAndFiles' calls a recursive method 'polls' which downloads mails
// from each folders(Inbox , Sent Items) of each user.
iia_DefaultMutableTreeNodeProcessorHdle.pollFoldersAndFiles(iia_frm.firstTreeNode,tempPool);
try {
long snooze = 1000 * 60 * 60 * 24;
Calendar calendarNow = Calendar.getInstance();
snooze = 1000 * 60 * 60 * 24 - (calendarNow.getTimeInMillis() - now.getTime());
if(now.after(startDate)&& count == 0)
{
count = 1;
snooze = 1000 * 60 * 60 * 24 - (now.getTime()-startDate.getTime());
}
calendar.add(calendar.DATE, addDays);
if(!Constants.stopFlag)
IIA_JFrame.statusInformation.setText("Status: Next polling will start at: "+calendar.getTime().toString());
// Here I am making this Swing Worker thread to sleep for 24 hours for daily schedule. The third param in 'scheduleAtFixedRate' didn't help
// me to stop after one round of poll.
Thread.currentThread().sleep(snooze);
}catch (InterruptedException exception){
VWLogger.log(Level.SEVERE, "Exception", exception);
}
addDays++;
}
Constants.stopFlag = false; //set stop to false
return null;
}
public void finished() {
tempPool.stopRequestAllWorkers();
IIA_Monitor.processTimer.cancel();
try{
Thread.currentThread().interrupt();
tempPool = null;
}catch(Exception ex){
}
iia_frm.statusInformation.setText("Status: ");
}
};
worker.start();
if(Constants.stopFlag){
//Should cancel the timer here
IIA_Monitor.processTimer.cancel();
worker.interrupt();
}
}
public void run(){
doScheduledProcess();
}
}