我有一个 Web 应用程序(使用 Spring 3.1),它使用 @Scheduled Annotation 定期执行工作任务(计划延迟)。工作任务打开与 AWS DynamoDb 的连接并执行一些数据库读取/更新。当我停止 webapp(来自 Tomcat 管理器)时,我在 catalina.out 中收到此消息:
“严重:Web 应用程序 [] 似乎已经启动了一个名为 [java-sdk-http-connection-reaper] 的线程,但未能阻止它。这很可能会造成内存泄漏。”
我感觉这与我的计划任务在 Tomcat 停止后仍在运行有关。
@Service
public class TaskScheduler implements ApplicationListener<ContextClosedEvent>{
@Autowired
private WorkerTask workerTask;
AmazonDynamoDBClient myDbConn = null;
private TaskScheduler() {
myDbConn = new AWSConnector("aws.properties").getDynamoConnection();
}
/*
* Will be repeatedly called, 10 seconds after the finish of the previous
* invocation.
*/
@Scheduled(fixedDelay=100000)
public void process() {
System.out.println("Scheduling worker task");
//worker task does some db read/writes
Future<String> status = workerTask.work(myDbConn);
if (status.isDone()) {
System.out.println("Completed Task");
return;
}
}
@Override
public void onApplicationEvent(ContextClosedEvent arg0) {
if(event instanceof ContextClosedEvent) {
// TODO Auto-generated method stub
if(myDbConn != null) {
this.myDbConn.shutdown();
}
}
}
调度程序-servlet.xml:
<task:annotation-driven scheduler="taskScheduler"/>
<task:scheduler id="taskScheduler" pool-size="2"/>
......
<bean id="TaskScheduler" class="com.sample.TaskScheduler"/>
Am I doing this correctly? a) I don't explicitly start the TaskScheduler. So i'm assuming spring takes care of starting this service. The 'this.myDbConn.shutdown()' is called. Despite this, I get the error. I'm using Spring MVC.