ServiceGraph is a powerful tool for controlling service start and stop routine. Upon start up it creates a graph of all the required services based on provided dependencies. This graph is used to start and stop services concurrently which leads to faster startup time without services interfering with each other.
- Designed to be used in combination with ActiveJ Inject and Launcher as a means to start/stop application services according to their dependency graph
- It starts services by following the multithreaded graph traversal algorithm: leaf services first and so on
- It stops services in the opposite direction
- Services dependency graph is automatically built upon ActiveJ Inject dependencies graph, but can be customized by user-specified dependencies.
- Supports multiple standard services like ThreadPool, Closeables, DataSource as well as Active-specific services like eventloops, async servers and async services.
- Can be configured to support other services as well with user-provided adapters
To get a basic understanding of ServiceGraph's role, let's have a look at a very simple example of an HTTP Server:
- This application extends predefined
HttpServerLauncheruses two services:
Eventloop.graph LR AsyncHttpServer --> Eventloop
- According to this graph, Service Graph starts
Eventloopfirst. The dependent
AsyncHttpServeris started afterwards
- When the application stops, the services will be stopped in the opposite direction:
To run the examples, you need to clone ActiveJ from GitHub
And import it as a Maven project. Check out tag v4.3. Before running the examples, build the project.
These examples are located at
In this example we create an application that extends Launcher and has a simple custom service which basically only starts and stops:
Service Graph is also able to start and stop your custom services:
Service Graph can manage more complex service dependencies. For example, let's assume we have an e-mail service prototype. To work properly, it requires two services - an authorization service and a database service. The authorization service also requires a database service, along with Eventloop and Executor. As a result, we have the following service graph:
ServiceGraphModule will start and stop all those services in the proper order:
This application looks as follows: