我需要实现一项服务,定期读取目录并处理在其中找到的文件。
我想经常阅读目录,例如每 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 * * * * ?"));
谢谢大卫
最佳答案
使用 <task:scheduler />
时使用的默认策略标记是中止新任务并抛出 RejectedExecutionException
.
如果您使用普通 bean 来配置 ThreadPoolTaskScheduler
您可以设置更多可以影响该行为的属性,所选的“executor”和“rejectedExecutionHandler”在其中起着重要作用。
您还使用 java 来配置您的任务,您可以将其移动到 xml。
<task:scheduled-tasks>
<task:scheduled ref="inviaFattureTask" cron="*/5 * * * * ?"/>
</task:scheduled-tasks>
有关更多信息,请查看 reference guide .
关于java - 池已满时的 ThreadPoolTaskScheduler 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23111402/