https://github.com/ncornette/superinit
Async dependency tree loader
https://github.com/ncornette/superinit
dependency-graph init-system thread threadpool
Last synced: about 1 year ago
JSON representation
Async dependency tree loader
- Host: GitHub
- URL: https://github.com/ncornette/superinit
- Owner: ncornette
- License: apache-2.0
- Created: 2016-09-30T15:39:47.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-09-18T12:39:05.000Z (over 7 years ago)
- Last Synced: 2025-01-06T07:49:07.205Z (about 1 year ago)
- Topics: dependency-graph, init-system, thread, threadpool
- Language: Java
- Size: 151 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://travis-ci.org/ncornette/superinit)
[](https://www.codacy.com/app/nicolas-cornette/superinit?utm_source=github.com&utm_medium=referral&utm_content=ncornette/superinit&utm_campaign=Badge_Grade)
[](https://codecov.io/gh/ncornette/superinit)
[  ](https://bintray.com/ncornette/maven/superinit/_latestVersion)
# Superinit
Async dependency tree loader
## Gradle
```groovy
repositories {
jcenter()
}
dependencies {
// Your dependencies
...
// Async dependency tree loader
// https://github.com/ncornette/superinit
compile 'com.ncornette.superinit:superinit:0.9.7'
}
```
## Usage
Wrap any `Runnable` into a `InitNode` object, then define dependencies :
```java
InitNode nodeA = new InitNode(runnableA);
InitNode nodeB = new InitNode(runnableB);
InitNode nodeC = new InitNode(runnableC);
// Setup dependencies
nodeA.dependsOn(nodeB);
```
Then use `InitLoader.load()` to execute all nodes in order based on dependencies, and in parallel when possible.
```java
InitLoader initLoader = new InitLoader(3); // N Threads in Thread pool executor
// Execute tasks
initLoader.load(loaderCallback, nodeA, nodeB, nodeC);
```
Use the `InitLoaderCallback` interface to be notified of the `InitLoader`
```java
public interface InitLoaderCallback {
// Is called once on success, even if error occurred
// Not called if InitLoader.interrupt() is called during execution
void onFinished();
// Is called each time a node execution fails with an Exception
void onNodeError(NodeExecutionError nodeError);
// Is called when an exception occurs outside a node execution
void onError(Throwable error);
}
```
## Features
A `ThreadpoolExecutor` with a fixed number of Threads will excute the tasks. Even with One Thread, execution order
is guaranteed to be respected.
Circular dependencies are prevented as early as possible, and will throw `IllegalArgumentException` :
- When adding a dependency with `dependsOn()` for direct circular dependencies.
- When calling `Initloader.load()` for indirect circular dependencies.
Nodes use `CountDownLatch` to wait for execution of their dependencies and to notify their execution is finished.
## Reference
[Dependency Resolving Algorithm](http://www.electricmonk.nl/docs/dependency_resolving_algorithm/dependency_resolving_algorithm.html)
## License
Copyright 2016 Nicolas Cornette
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.