[英]Java library for asynchronous dependency initialization
I'm working on a project written in Java, which has a dependency initialization system written in-house.我正在做一个用 Java 编写的项目,它有一个内部编写的依赖初始化系统。 I've been looking for a robust third party library which could manage this, so we don't have to do maintenance on it as much.我一直在寻找一个强大的第三方库来管理它,这样我们就不必对其进行太多维护。 So far my search has not been very helpful.到目前为止,我的搜索并不是很有帮助。 Most results have come back suggesting Spring, but I don't think that is satisfies our needs.大多数结果都返回建议 Spring,但我认为这不能满足我们的需求。
Things we need out of the library:我们需要图书馆外的东西:
init()
, but allow the service to call back.当调用服务进行初始化时,库不应期望在调用init()
后完成此操作,而应允许服务回调。 This is because most of the services are making calls to our web service as part of their initialization.这是因为大多数服务都在调用我们的 web 服务作为其初始化的一部分。I've looked at Spring and Guice already, and from what I can tell, they won't work, because they don't allow your dependencies to be in an intermediate initialization state, and will block the thread if you ask for an uninitialized object, until it is fully constructed.我已经看过 Spring 和 Guice,据我所知,它们不会工作,因为它们不允许您的依赖项处于中间初始化 state,如果您要求未初始化的,它们将阻塞线程object,直至完全建成。 I'm also not sure if they permit uninitialization.我也不确定他们是否允许取消初始化。
I'm fully aware that this may be very specific, and possibly a strange system (it is quite old), so I expect that there may not be an alternative to an in-house solution.我完全意识到这可能非常具体,并且可能是一个奇怪的系统(它很旧),所以我预计可能没有内部解决方案的替代方案。
Initializer for service initialization logic.服务初始化逻辑的初始化程序。 This can be very useful for distributed services, to meet several requirements:这对于分布式服务非常有用,可以满足以下几个要求:
During initialization, application service can check health of the components and reflect it accordingly.在初始化期间,应用服务可以检查组件的健康状况并相应地反映出来。
As it is intended mainly for connecting to services, retry mechanism and failure handling, health check for dependencies services is intended to be managed separately.由于它主要用于连接服务、重试机制和故障处理,因此依赖服务的健康检查旨在单独管理。
Example usage:用法示例:
Initializer initializer = Initializer.builder().circuitBreaker(circuitBreaker)
.mandatoryTask("initDB", () -> {return initDB();})
.mandatoryTask("initMessagingService", () -> {return initMessagingService();})
.nonMandatoryTask("initStats", () -> {return initStats();})
.postInitTask("serveRequests", () -> {return serveRequests();})
.build();
initializer.initAsync();
Note: I am the author of this Initializer.注意:我是这个 Initializer 的作者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.