erosynthesis

erosynthesis

Implementing distributed scheduled tasks with Spring Cloud Task and Spring Cloud Scheduler.

  1. Create a Spring Boot application and add dependencies

    First, create a Spring Boot application and add the following dependencies to the build.gradle or pom.xml file:

    dependencies {
        implementation 'org.springframework.cloud:spring-cloud-starter-task'
        implementation 'org.springframework.cloud:spring-cloud-starter-scheduler'
        // Other dependencies
    }
    
  2. Write the TaskLauncher class

    Next, you need to write a TaskLauncher class that is responsible for launching and stopping tasks. This class can implement the TaskLauncher interface provided by Spring Cloud Task, as shown below:

    import org.springframework.cloud.task.launcher.TaskLaunchRequest;
    import org.springframework.cloud.task.launcher.TaskLauncher;
    
    public class MyTaskLauncher {
    
        private final TaskLauncher taskLauncher;
    
        public MyTaskLauncher(TaskLauncher taskLauncher) {
            this.taskLauncher = taskLauncher;
        }
    
        public void launchTask(String taskName, String commandLineArgs) {
            TaskLaunchRequest request = new TaskLaunchRequest(
                    "classpath:/tasks/" + taskName + ".jar",
                    commandLineArgs,
                    null, null, null);
            taskLauncher.launch(request);
        }
    
        public void stopTask(long taskId) {
            taskLauncher.stop(taskId);
        }
    }
    
    

    In this example, the MyTaskLauncher class contains the launchTask() method and stopTask() method. The launchTask() method starts a new task based on the provided task name and command line arguments, and returns the task ID. The stopTask() method stops the specified task.

  3. Write the TaskScheduler class

    Next, you need to write a TaskScheduler class that is responsible for scheduling tasks and sending them to the TaskLauncher for execution. You can implement this class using the ScheduledTaskRegistrar and ScheduledTask classes provided by Spring Cloud Scheduler, as shown below:

    import org.springframework.cloud.scheduler.ScheduledTask;
    import org.springframework.cloud.scheduler.ScheduledTaskRegistrar;
    import org.springframework.scheduling.support.CronTrigger;
    
    public class MyTaskScheduler {
    
        private final ScheduledTaskRegistrar taskRegistrar;
        private final MyTaskLauncher taskLauncher;
    
        public MyTaskScheduler(ScheduledTaskRegistrar taskRegistrar, MyTaskLauncher taskLauncher) {
            this.taskRegistrar = taskRegistrar;
            this.taskLauncher = taskLauncher;
        }
    
        public void scheduleTask(String taskName, String cronExpression, String commandLineArgs) {
            ScheduledTask task = taskRegistrar.scheduleCronTask(() -> {
                taskLauncher.launchTask(taskName, commandLineArgs);
            }, new CronTrigger(cronExpression));
            task.getTaskId(); // Optional: Save the task ID for later task stopping
        }
    
        public void unscheduleTask(long taskId) {
            taskRegistrar.removeScheduledTask(taskId);
            taskLauncher.stopTask(taskId);
        }
    }
    
    

    In this example, the MyTaskScheduler class includes the scheduleTask() method and unscheduleTask() method. The scheduleTask() method schedules a new task that will be executed when the specified cron expression triggers. The unscheduleTask() method cancels the scheduling of the task and stops its execution if it is currently running.

  4. Define and register tasks

    Now, you can define and register the tasks to be executed. These tasks can be regular Java classes, Spring Batch jobs, or other types of tasks. In this example, we will create a simple Java class to represent a task:

    public class MyTask implements Runnable {
    
        private final String name;
    
        public MyTask(String name) {
            this.name = name;
        }
    
        @Override
        public void run() {
            System.out.println("Running task " + name);
        }
    }
    
    

    Then, you can register the task by creating an instance of MyTask and passing it to the scheduleTask() method of MyTaskScheduler, as shown below:

    @Autowired
    private MyTaskScheduler taskScheduler;
    
    @PostConstruct
    public void init() {
        taskScheduler.scheduleTask("my-task", "0 0/5 * * * ?", "arg1 arg2");
    }
    
    

    In this example, we use the @PostConstruct annotation to ensure that the task is automatically registered and scheduled when the application starts.

  5. Start the application

    Finally, you can start the application.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.