Skip to content

🧵 🧶 (library) execute background tasks without affecting the main thread of the application

License

Notifications You must be signed in to change notification settings

RubensKj/background-tasks

Repository files navigation

Background Tasks

Author Languages License Stars Coverage Status Maven Version Contributors

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.

🐦 Direct Links

🏗️ Dependencies

  • You should have at least Java 1.8 in your computer
  • Maven

🚀 Getting Started

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

Creating a Subscribe method

Params:

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.

Constructor

Subscribe subscribeBasic = new Subscribe(
      subName,
      ICallBack
);

Here is some examples of subscribe

Basic

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
);

Creating a ICallback

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
   };
}

Passing params

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
   };
}

Executing

After creating a subscribe, you can execute by following this

subscribe.subscribe();

🎉 Want to Contribute?

Make your pull request following the contributing instructions and I'll accept :).

📕 License

License used in this project is MIT license

About

🧵 🧶 (library) execute background tasks without affecting the main thread of the application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages