简体   繁体   English

@Scheduler是否启动新线程?

[英]Does @Scheduler start a new thread?

I have a Spring application and it has several scheduled methods 我有一个Spring应用程序,它有几个scheduled方法

It seems Lo4J stop rolling to new file and grow to 10 Gbytes after the application running for a while. 在应用程序运行一段时间后,Lo4J似乎停止滚动到新文件并增长到10 GB。

All configuration looks fine, and I only use notepad ++ to open log file(meaning no lock on log file by editor) 所有配置看起来都不错,我只使用记事本++打开日志文件(表示编辑器未锁定日志文件)

So I ponder there might be another thread running in the application. 因此,我认为应用程序中可能会运行另一个线程。 I dong recall any muti-threading implementation in current application. 我回想起当前应用程序中的任何多线程实现。

Then is it possible that the @Scheduled method causes the issue? 那么@Scheduled方法是否可能导致此问题?

@Scheduled causes the code to be run in a separate thread, not necessarily new as it might come from a thread pool. @Scheduled导致代码在单独的线程中运行,不一定是新的,因为它可能来自线程池。

The non rollover of the log file happens because when log4j tries to rename the file for doing the rollover, some application thread is logging to the file at that precise moment. 之所以发生日志文件的非翻转,是因为当log4j尝试重命名文件以进行翻转时,某个应用程序线程正在该时刻记录到该文件。

According to the code of the log4j rolling file implementation RollingFileAppender , when the time to rollover comes ( rollOver() method called), an attempt is made to rename the file. 根据log4j滚动文件实现RollingFileAppender的代码,当过渡时间到来时(调用rollOver()方法),将尝试重命名该文件。

if the file is locked then no rollover occurs, and log4j continues to use the same file, until the next time the rollover policy is triggered and a rename is again attempted. 如果文件被锁定,则不会发生翻转,并且log4j继续使用同一文件,直到下次触发翻转策略并再次尝试重命名为止。

So the @Scheduled annotations might be contributing to this, but it might not be the only responsible, if there are for example a high volume of requests if it's a web application, etc. 因此@Scheduled批注可能对此有所贡献,但它可能不是唯一的责任,例如,如果它是Web应用程序等,则存在大量请求。

To decrease the likelyhood of rollover failure, try to the change the @Scheduled threads to run at a different moment than when the rollover attempt occurs. 若要降低发生翻转失败的可能性,请尝试将@Scheduled线程更改为在发生翻转尝试时的不同时刻运行。

Also decreasing the logging level to ERROR will reduce the likelihood of rollover failure. 同样,将日志记录级别降低为ERROR也将减少翻转失败的可能性。 See also How to find which thread is locking a file . 另请参阅如何查找哪个线程正在锁定文件

It should use one of the threads that are are allotted by the configuration's thread pool. 它应使用配置的线程池分配的线程之一。 The thread will be picked up every time one of the scheduled tasks are executed and once the task has finished, the thread will be freed up back into the pool. 每次执行计划任务之一时,都会拾取该线程,并且一旦任务完成,该线程将被释放回池中。 The amount of logging is dependent on how often the task is run & how many log statements you have. 日志记录的数量取决于任务运行的频率以及您拥有多少条日志语句。 So, unless there is A LOT of logging then it's probably not your issue. 因此,除非有记录的很多 ,然后它可能不是你的问题。

You should use something similar to this in your scheduler configuration to determine pool size. 您应该在调度程序配置中使用与此类似的内容来确定池大小。 Your pool size should also be determined by the available threads on your machine. 池的大小还应该由计算机上的可用线程确定。 Number of cores, threads per core, jvms, etc. 核心数,每个核心的线程,jvm等

<task:annotation-driven scheduler="taskScheduler"/>
<task:scheduler id="taskScheduler" pool-size="2"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM