简体   繁体   English

Java 异步依赖初始化库

[英]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:我们需要图书馆外的东西:

  • Maintain the 'initialization state' of dependencies in the system.维护系统中依赖项的“初始化状态”。 This would mean if each 'service', as I'll call them, is already instantiated, but can be uninitialized, initializing, initialized.这意味着如果每个“服务”,正如我所说的,都已经实例化,但可以未初始化、正在初始化、正在初始化。 Most of these are singletons.其中大多数是单身人士。
  • Maintain a dependency graph of each service, and only attempt to initialize when all dependencies have been initialized.维护每个服务的依赖图,并且仅在所有依赖项都已初始化时才尝试初始化。
  • Most importantly, this must allow for asynchronous initialization.最重要的是,这必须允许异步初始化。 When a service is called to initialize, the library should not expect this to be done after calling 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 服务作为其初始化的一部分。
  • The system needs to support uninitialization.系统需要支持未初始化。 eg.例如。 If we lose connection to the web service, or your session expires for one of the services, we need to be able to set it as uninitialized, invalidating everything that depends on it, and queuing for re-initialization.如果我们失去与 web 服务的连接,或者您的 session 对于其中一项服务过期,我们需要能够将其设置为未初始化,使依赖它的所有内容无效,并排队等待重新初始化。
  • The system should allow analysis of the system in its current state, so we can track what is initialized, and what depends on what.系统应该允许在其当前 state 中分析系统,因此我们可以跟踪初始化了什么,什么依赖于什么。

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:这对于分布式服务非常有用,可以满足以下几个要求:

  • Start the service first and report healthy state先启动服务,报健康 state
  • Initialize mandatory components (database, messaging service etc')初始化强制组件(数据库、消息服务等)
    • in a background task for not holding service startup.在不保持服务启动的后台任务中。
    • Retry mechanism – per component, not for all components together.重试机制——每个组件,而不是所有组件一起。
  • Circuit breaker for retrying for waiting for some cluster state which can hold service initialization.用于重试等待某些可以保持服务初始化的集群 state 的断路器。
  • Optional non-mandatory components can be retried and initialized in a background task without holding application from starting and serving requests and/or do its work.可选的非强制组件可以在后台任务中重试和初始化,而无需阻止应用程序启动和服务请求和/或执行其工作。
    • Serve requests and/or do the work only after all mandatory components initialization tasks are done.仅在完成所有必需的组件初始化任务后才提供请求和/或执行工作。

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.

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