- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Spring Boot 应用程序,它使用 AsyncTaskExecutor
生成许多线程(数量是预定义的)
线程执行一个无限循环,从一些队列和进程对象中读取,所以我并没有真正的拒绝策略机制(比如接受 tasks
的 ThreadPool)
问题在于,当应用程序关闭时,线程可能(并且可能)忙于处理一个项目,其中包括使用 MongoTemplate
对 Mongo 进行的操作。
因此,当应用程序关闭时,MongoClient 会自动close()
,然后我会从 Mongo 收到一些错误,例如:
java.lang.IllegalStateException: The pool is closed
at com.mongodb.internal.connection.ConcurrentPool.get(ConcurrentPool.java:137)
at com.mongodb.internal.connection.DefaultConnectionPool.getPooledConnection(DefaultConnectionPool.java:262)
at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:103)
at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:92)
at com.mongodb.internal.connection.DefaultServer.getConnection(DefaultServer.java:85)
如何优雅地关闭应用程序?例如在尚未关闭 MongoClient 的同时中断线程?
代码:
创建 bean :
@Bean
AsyncTaskExecutor getTaskExecutor() {
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
return executor;
}
简单地执行:
executor.execute(runnable);
最佳答案
不要使用 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.
* <p>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.
* <p>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.
* <p>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.
* <p>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;
}
相关部分
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.
您可以使用spring自动配置来控制任务执行属性(首选)或以编程方式使用@Bean
注解
2.1.0 中的 Spring boot 为任务执行器提供自动配置,并用于 @EnableAsync
和 Spring MVC 异步支持。
应用程序不需要任务执行器 bean/webMvcConfigurer 配置。所以删除你有的,应该是好的。
您可以使用带有 spring.task.execution.*
的应用程序属性/yml 文件进行调整。
spring.task.execution.shutdown.await-termination=true
spring.task.execution.shutdown.await-termination-period=60
可以找到完整列表 here
或
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(5);
taskExecutor.waitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(60);
return taskExecutor;
}
关于java - Spring 启动 : gracefully shutdown by controlling termination order involving MongoClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61187699/
const { MongoClient, ObjectID } = require('mongodb'); const debug = require('debug')('mongodb-connec
当我启动我的一些服务时,它会报告此类警告并且服务会停止: /usr/lib64/python2.6/site-packages/pymongo/topology.py:75: UserWarning:
我不喜欢新的 mongo,MongoDB 在 PHP7 中需要几个库。 PHP 5 中的 MongoClient(已弃用)更加舒适和轻便! 我决定启动一个脚本并比较两个版本,结果非常令人惊讶: Mon
我正在尝试实现 MongoDB 连接处理程序,但遇到了一个问题,我的 Database即使我的 MongoClient() 调用成功,调用也会返回 None。 #mongoconn.py from p
我对 MongoDB 有点陌生,我对 MongoClient 类感到困惑,因为在不同的包中有两个( com.mongodb.client.MongoClient 和 com.mongodb.Mongo
我正在尝试熟悉从 C# 程序写入 MongoDB。我已经按照 http://mongodb.github.io/mongo-csharp-driver/1.11/getting_started/ 的建
我正在使用 Celery 和 MongoEngine 作为我的 Django 应用程序的一部分。 当 celery @shared_task 通过 mongoengine 模型类访问 mongodb
我有一个非常简单的 PyMongo 配置,连接到两个主机: from pymongo import MongoClient host = os.getenv('MONGODB_HOST', '127.
这个问题已经有答案了: MongoDB - Java | How to manage the connection (1 个回答) 已关闭 5 年前。 我正在用 Java 开发一个应用程序,该应用程序
我有一个副本集设置,其中包含 1 个主节点 (mongo1.test.com)、1 个辅助节点 (mongo2.test.com) 和 1 个仲裁节点 (mongo3.test.com)。当我使用 M
我是 NodeJS 新手,也是 JS can-kicker 第一次尝试 DI。以下是我在决定问我之前看过的问题,因为它们显示相同的错误:[1] [2] 运行我的入口点产生: this.client =
使用 java MongoClient 库,如何在集合中查找文档并仅返回特定的对象?我知道这对于 1 个对象 是可能的,但不确定多个。 对于 1 个对象: DBCursor cursor = db.g
这是我的服务器告诉我的内容: MongoClient.connect('mongodb://:@ds235778.mlab.com:35778/satr-wars-quotes', (err, cli
我有一个使用 MongoDB 作为数据库的 ASP.Net MVC 应用程序。网站和数据库位于不同的服务器上。 目前,我有一个看起来像这样的类: public class Mongo { pr
我无法做到这一点: from pymongo import MongoClient 我明白了: >>> import pymongo >>> from pymongo import MongoClie
我正在尝试让这段代码运行: tilbud; ?> 每次我遇到同样的错误: Fatal error: Class 'MongoClient' not found in C:\xampp\htdocs\c
我正在努力更好地理解 this documentation关于如何重用 MongoClient 的实例。 我们通过在构造函数中传递连接字符串来创建 MongoClient 实例,this is the
我正在使用 Mongo DB java 驱动程序连接到 mongo 实例。下面是我用来创建 MongoClient 实例的代码。 try { new MongoClient("
什么是 Mongoclient?它在下面的语句中做了什么? var MongoClient = require('mongodb').MongoClient; 最佳答案 您示例中的 MongoClie
假设我有一个副本集,其中包含一个主 P 和三个辅助 S1、S2 和 S3 。假设应用程序使用此 constructor 创建一个 Mongo 客户端。 。 种子列表为{P、S1、S2}。请注意,该列表
我是一名优秀的程序员,十分优秀!