java mongoclient 崩溃_java - Spring Boot:通过控制涉及MongoClient的终止顺序来正常关闭 - SO中文参考 - www.soinside.com...

不要使用SimpleAsyncTaskExecutor-SimpleAsyncTaskExecutor为每个请求创建一个新线程,而是使用ThreadPoolTaskExecutor并配置以下两个属性。/**

* Set whether to wait for scheduled tasks to complete on shutdown,

* not interrupting running tasks and executing all tasks in the queue.

*

Default is "false", shutting down immediately through interrupting

* ongoing tasks and clearing the queue. Switch this flag to "true" if you

* prefer fully completed tasks at the expense of a longer shutdown phase.

*

Note that Spring's container shutdown continues while ongoing tasks

* are being completed. If you want this executor to block and wait for the

* termination of tasks before the rest of the container continues to shut

* down - e.g. in order to keep up other resources that your tasks may need -,

* set the {@link #setAwaitTerminationSeconds "awaitTerminationSeconds"}

* property instead of or in addition to this property.

* @see java.util.concurrent.ExecutorService#shutdown()

* @see java.util.concurrent.ExecutorService#shutdownNow()

*/

public void setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown) {

this.waitForTasksToCompleteOnShutdown = waitForJobsToCompleteOnShutdown;

}

/**

* Set the maximum number of seconds that this executor is supposed to block

* on shutdown in order to wait for remaining tasks to complete their execution

* before the rest of the container continues to shut down. This is particularly

* useful if your remaining tasks are likely to need access to other resources

* that are also managed by the container.

*

By default, this executor won't wait for the termination of tasks at all.

* It will either shut down immediately, interrupting ongoing tasks and clearing

* the remaining task queue - or, if the

* {@link #setWaitForTasksToCompleteOnShutdown "waitForTasksToCompleteOnShutdown"}

* flag has been set to {@code true}, it will continue to fully execute all

* ongoing tasks as well as all remaining tasks in the queue, in parallel to

* the rest of the container shutting down.

*

In either case, if you specify an await-termination period using this property,

* this executor will wait for the given time (max) for the termination of tasks.

* As a rule of thumb, specify a significantly higher timeout here if you set

* "waitForTasksToCompleteOnShutdown" to {@code true} at the same time,

* since all remaining tasks in the queue will still get executed - in contrast

* to the default shutdown behavior where it's just about waiting for currently

* executing tasks that aren't reacting to thread interruption.

* @see java.util.concurrent.ExecutorService#shutdown()

* @see java.util.concurrent.ExecutorService#awaitTermination

*/

public void setAwaitTerminationSeconds(int awaitTerminationSeconds) {

this.awaitTerminationSeconds = awaitTerminationSeconds;

}

相关部分设置此执行程序应该的最大秒数在关机时阻塞,以等待其余任务完成在其余容器继续关闭之前执行它们的执行下。如果您可能需要完成其他任务,则此功能特别有用需要访问其他资源,这些资源也由容器。

您可以使用spring自动配置进行配置,以控制任务执行属性(首选)或使用@Bean注释以编程方式进行配置

2.1.0中的Spring boot为任务执行者提供了自动配置,并用于@EnableAsync和Spring MVC Async支持。

无需从应用程序配置任务执行者bean / webMvcConfigurer。因此,删除您所拥有的应该是好的。

您可以在spring.task.execution.*中使用applicaion属性/ yml文件进行调整。spring.task.execution.shutdown.await-termination=true

spring.task.execution.shutdown.await-termination-period=60

可以找到完整列表here

更多详细信息here和here

或here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值