简体   繁体   中英

Java library for asynchronous dependency initialization

I'm working on a project written in Java, which has a dependency initialization system written in-house. 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.

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. This is because most of the services are making calls to our web service as part of their initialization.
  • 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.
  • The system should allow analysis of the system in its current state, so we can track what is initialized, and what depends on what.

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. 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
  • 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.
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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