Creating background tasks with subscribe 🧶
Background Tasks is a library that provide a subscribe method with a callback, it will run the callback after you subscribe, however all things will be run in a second thread.
- You should have at least
Java 1.8
in your computer - Maven
By following this steps you can take advantage of using background tasks
Adding the dependency on pom.xml
<dependency>
<groupId>com.github.rubenskj</groupId>
<artifactId>background-tasks</artifactId>
<version>2.2.0</version>
</dependency>
After resolving from maven, you can create a Subscribe
Subscribe name (String)
: This name will be shown in the console.
Retry (Int)
: Times that will retry the callback if something throws any exception.
ICallback
: The method that will be executed in another thread.
Consumers (int)
: How many thread will have for this callback.
Subscribe subscribeBasic = new Subscribe(
subName,
ICallBack
);
Here is some examples of subscribe
Subscribe subscribe = new Subscribe("Name of subscribe", this.handleCallback("passing param"));
Some variation of constructor
// With retry
Subscribe subscribe1 = new Subscribe(
"Name of subscribe",
2,
this.handleCallback("passing param")
);
// With just consumers
Subscribe subscribe2 = new Subscribe(
"Name of subscribe"
this.handleCallback("passing param"),
8
);
// With retry and consumers
Subscribe subscribe3 = new Subscribe(
"Name of subscribe",
4
this.handleCallback("passing param"),
8
);
You can create inline callback in order to execute some small peace of code, create like this
Subscribe subscribe = new Subscribe(Sub name, () -> {
// Your code here
});
Or for big codes you can use this way
Subscribe subscribe = new Subscribe(Sub name, this::handleCallback);
// ...
// Below the method that subscribe was created
public ICallback handleCallback() {
return () -> {
// Your code here
};
}
You can pass some params to callback
Subscribe subscribe = new Subscribe(Sub name, this.handleCallback(params...));
// ...
// Below the method that subscribe was created
public ICallback handleCallback(params...) {
return () -> {
// Your code here
// using params here
};
}
After creating a subscribe, you can execute by following this
subscribe.subscribe();
Make your pull request following the contributing instructions and I'll accept :).
License used in this project is MIT license