我想使用Java的ScheduledExecutorService
来计划延迟服务,以删除数据库中给定过期日期的某些行。但是我发现即使在指定的到期日期之后,数据库中的行也不会被删除。所以计划任务不会被执行。我的代码如下:为什么我无法使用调度程序安排固定延迟任务
public int addNewGroup(final String groupName, Category category,
Timestamp expireDate) {
String sqlQuery = "INSERT INTO Groups (name, category, expireDate) VALUES (?, ?, ?)";
int numRows = jdbcTemplate.update(sqlQuery, new Object[] { groupName,
category.toString(), expireDate });
//schedule the deleting temporary group task
if (category == Category.temp) {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable task = new Runnable() {
@Override
public void run() {
String query = "DELETE FROM Groups WHERE name=?";
jdbcTemplate.update(query, new Object[] {groupName});
}
};
scheduler.schedule(task, expireDate.getTime()-System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
return numRows;
}
这是我的测试代码中插入一条记录到有到期日的DB:
public class StoregroupDaoTest {
private StoregroupDao dao;
@Before
public void setup() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
dao = context.getBean(StoregroupDao.class);
}
@Test
public void testAdd() {
String groupName = "ABC";
Category category = Category.temp;
@SuppressWarnings("deprecation")
Timestamp expireTime = new Timestamp(114, 8, 3, 16, 57, 0, 0); //2014-09-03 16:57:00
int numRows = dao.addNewGroup(groupName, category, expireTime);
}
}
的记录插入到数据库。预计将在到期时间之后按照上面给出的计划任务进行删除。但是在指定的到期时间之后,当我查询数据库时,记录仍然存在,并且不会被删除。所以看起来计划任务根本就没有执行。这里有什么问题?
嗨Devarsh,谢谢你的建议。我在调度程序中放了一些日志打印输出,但它看起来并没有运行,因为在到期时间之前,根本没有打印输出。我也尝试在我的数据库中使用相同的DELETE查询,它工作正常。使用存储过程很好,但我真的想用Java代码来删除记录。我不知道为什么任务调度程序根本不运行。 – tonga 2014-09-03 21:28:43
嘿汤加,是的!这通常是ScheduledExecuter的哈利部分 - 在你想要时运行:/你可以看看这些教程:http://www.java2s.com/Code/JavaAPI/java.util.concurrent/ScheduledExecutorServicescheduleRunnablecommandlongdelayTimeUnitunit.htm和http ://tutorials.jenkov.com/java-util-concurrent/scheduledexecutorservice.html;确保你的线程池大小足够大;请让我知道你做了什么进展,如果你有任何其他问题! :-) – 2014-09-03 21:36:43
我还打印出调度程序的fed参数:'expireDate.getTime()'。毫无疑问,从时代开始毫无疑问。所以真的是一个难题。 – tonga 2014-09-03 21:43:46