How to quickly start using Taskun Scheduler framework in the Java class library
How to quickly start using Taskun Scheduler framework in the Java class library
Introduction:
Taskun Scheduler is a powerful Java scheduling framework for regular execution tasks in applications.It provides a flexible task scheduling configuration option and has excellent scalability and reliability.This article will introduce how to quickly start using the Taskun Scheduler framework in the Java class library.
Step 1: Add Taskun SCHEDULER dependencies
First, you need to add Taskun Scheduler to your project.You can add the following dependencies to Maven or Gradle Construction Tools:
<dependency>
<groupId>com.github.taskun</groupId>
<artifactId>taskun-scheduler</artifactId>
<version>1.5.1</version>
</dependency>
Step 2: Create a task class
Create a task class to be executed regularly.This class must implement the `task` interface and rewrite the` run () "method, which will be regularly called.
import com.github.kagkarlsson.scheduler.task.Task;
import java.time.Duration;
public class MyTask implements Task {
@Override
public void run() {
// Place your task logic here
System.out.println ("Regularly executed task");
}
@Override
public Duration getDuration() {
// Return to the task execution interval time
return Duration.ofSeconds(60);
}
}
Step 3: Create and configure Taskun Scheduler instance
Next, create an instance of `Scheduler` and configure parameters to be used for task scheduling.You can complete this operation by creating a `Schedulerbuilder` and using its different methods.
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.SchedulerBuilder;
public class SchedulerExample {
public static void main(String[] args) {
Scheduler scheduler = SchedulerBuilder
.create()
.singleNodeMode()
.threads(5)
.build();
// Register your task class
scheduler.schedule(MyTask.class);
// Start scheduling program
scheduler.start();
}
}
Step 4: Run and monitor the dispatch task
Now, you can run your application and start the execution of monitoring and scheduling tasks.Taskun Scheduler will call the `Run ()" method regularly to call the task regularly in the time interval you defined in the task category.
public class SchedulerExample {
public static void main(String[] args) {
Scheduler scheduler = SchedulerBuilder
.create()
.singleNodeMode()
.threads(5)
.build();
scheduler.schedule(MyTask.class);
// The execution of the monitoring task
scheduler.listener(new TaskListener() {
@Override
public void taskStarted(Execution execution) {
System.out.println ("The task starts to execute:" + Execution.gettaskinstance ());
}
@Override
public void taskCompleted(Execution execution) {
System.out.println ("task execution is completed:" + Execution.gettaskinstance ());
}
@Override
public void taskFailed(Execution execution, Throwable throwable) {
System.out.println ("Mission execution failed:" + Execution.gettaskinstance ());
}
});
scheduler.start();
}
}
The above is the method of quickly start using the Taskun Scheduler framework in the Java library.You can configure more parameters according to your needs and write more task classes to perform different tasks on a regular basis.Taskun Scheduler will help you effectively manage task scheduling in the application.