因此,我将尝试尽可能全面地讨论我的程序的细节,同时仍然要理解要点。我有两个重要的对象,即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),以查看它们的深层/浅层副本是否有些奇怪。有任何想法吗?
编辑:
好吧,我完全不知道发生了什么。这是完整的课程。
http://ideone.com/hR7et
那里没有什么敏感的东西,所以没有后顾之忧。
这是我的日志输出:
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的计时器永远不会关闭。
最佳答案
我看到JobManager的计时器已关闭,但JobWorker
计时器未启动。
您要打JobWorker.start
吗?您需要包括更多代码,以便我们可以看到发生了什么。没有理由不执行这些run
方法:http://ideone.com/uEer3
暂时性仅与序列化相关。