我需要实现一个定期读取目录并处理在其中找到的文件的服务。我想经常阅读目录,例如每 5 秒。问题是发送文件可能需要一些时间。发送文件时将它们移走。我的想法是使用ThreadPoolTaskScheduler
单个线程池并将任务安排为每 5 秒运行一次。该解决方案似乎有效,我经常阅读目录,当任务为空时,任务很快完成。当我发现一些文件时,任务需要更长的时间,但由于池只有一个线程,因此没有执行其他并发任务。
我的问题是我无法理解由于池已满而无法运行的计划任务发生了什么?他们是被取消还是刚刚入队。我担心排队太多任务并造成内存泄漏。
这是我的 spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
....
<bean id="inviaFattureTask" class="com.sirio.fatturazionepa.invio.InviaFattureTask">
<property name="fileSystemUtils" ref="fileSystemUtils"></property>
<property name="fattureManager" ref="fattureManager"></property>
</bean>
<task:scheduler id="myScheduler" pool-size="1"/>
...
</beans>
和java代码。
TaskScheduler ts = appContext.getBean(TaskScheduler.class);
InviaFattureTask task = appContext.getBean(InviaFattureTask.class);
ts.schedule(task, new CronTrigger("*/5 * * * * ?"));
谢谢大卫