因此,我将尝试尽可能笼统地讨论我的程序的细节,同时仍能理解一般要点。我有两个重要的对象,JobManager 和 JobWorkers。JobWorker 有一个任务,它应该以固定的时间间隔执行,直到完成。我会忽略工作完成后会发生什么,因为它们永远不会开始。我看到 JobManager 的计时器正在启动,但 JobWorker 计时器没有启动。下面是一些伪代码,可帮助您了解情况。
public class JobWorker
{
private String jobName;
private Timer timer;
private Calendar startTime;
public JobWorker(String jobName, Calendar startTime, otherFields)
{
this.jobName = jobName;
this.startTime = startTime;
this.timer = new Timer("EmailJob" + jobName + "-timer");
//set other fields
}
public void start()
{
timer.schedule(new TimerTask(){
public void run()
{
//Do stuff - this is where nothing is happening
}
}, 0, 60000);
}
//Equals method and other stuff
}
public class JobManager
{
private List<JobWorker> activeWorkers = new ArrayList<JobWorker>();
private List<JobWorker> pausedWorkers = new ArrayList<JobWorker>();
private Timer timer;
public JobManager()
{
//loads workers from database
timer = new Timer("JobManager-timer");
loadJobs();
timer.schedule(new TimerTask(){
public void run()
{
//I see this statement every minute as expected
logger.info("EmailJobManager timer run started.");
for(JobWorker worker : pausedWorkers)
{
Calendar c = new GregorianCalendar();
//I can see worker.getStartTime() in client gui so I know that they have already passed
if(c.compareTo(worker.getStartTime()) > 0)
{
//Never see these log statements
logger.info("Memory Address in manager timer loop 2 for " + worker.getEmailJobName() + " ({})", worker.toString());
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy - HH:mm");
logger.info("Comparing current time ({}) to JobWorker " + worker.getJobName() + " start time ({})", sdf.format(c.getTime()), sdf.format(worker.getStartDate()));
activeWorkers.add(worker);
pausedWorkers.remove(worker);
worker.start();
}
}
}
}, 0, 60000);
}
public void addWorker(necessary fields)
{
//Create JobWorker newWorker = new JobWorker(jobName, blah blah)
pausedWorkers.add(newWorker);
}
/**
* Checks to see if any jobs were not completed as expected due to hardware error. Recreates the EmailJob and EmailJobWorker responsible.
*/
private void loadJobs()
{
String SQL = "SELECT * FROM email_jobs";
List<Map<String, Object>> rows = jt.queryForList(SQL);
for(Map<String, Object> row : rows)
{
JobWorker worker = new JobWorker(stuff it needs from SQL);
//I see these debug statements
logger.info("Worker " + worker.getJobName() + " has been added to pausedWorkers list with memory address ({})", worker.toString());
pausedWorkers.add(worker);
logger.info("Job " + jobName + " loaded sucessfully.");
}
}
我将继续放入更多调试语句,即打印出 JobWorker 内存地址(本质上是 Object.toString),看看它们的深/浅副本是否有什么奇怪的地方。有任何想法吗?
编辑:好的,我对正在发生的事情完全不知所措。所以这是完整的课程。
里面没有敏感的东西,所以不用担心。
这是我的日志输出:
2012-04-17 16:34:18,548 INFO [http-bio-8081-exec-97] c.c.e.EmailJobWorker [EmailJobWorker.java:98] EmailJobWorker test was created sucessfully. (batchSize, timeInterval, currentId, startTime) (1, 5, 1, 04/17/12 - 15:15)
2012-04-17 16:34:18,552 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:297] Worker test has been added to pausedWorkers list with memory address (EmailJobWorker@7e7df7d)
2012-04-17 16:34:18,552 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:301] EmailJob test loaded sucessfully.
2012-04-17 16:34:18,557 INFO [http-bio-8081-exec-97] c.c.e.EmailJobWorker [EmailJobWorker.java:98] EmailJobWorker test2 was created sucessfully. (batchSize, timeInterval, currentId, startTime) (1, 5, 1, 04/17/12 - 15:15)
2012-04-17 16:34:18,558 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:297] Worker test2 has been added to pausedWorkers list with memory address (EmailJobWorker@349be26c)
2012-04-17 16:34:18,558 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:301] EmailJob test2 loaded sucessfully.
2012-04-17 16:34:18,558 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:117] EmailJobManager timer started sucesfully. (sleepTime) (1)
2012-04-17 16:34:18,559 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:74] EmailJobManager timer run started.
2012-04-17 16:34:18,561 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:85] Size of pausedWorkers: 2
2012-04-17 16:34:18,561 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:89] Memory Address in manager timer loop 2 for test (EmailJobWorker@7e7df7d)
2012-04-17 16:34:19,501 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:34:31,984 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:34:34,562 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:04,840 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:19,501 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:31,984 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:34,562 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:36:04,840 INFO [EmailJobManager] c.c.e.EmailJobManager
正如您所看到的,即使 EmailJobManager 只创建一次,并且 JobWorkers 的计时器从未设置过,计时器也不会每分钟都关闭。