Spring Cloud Task

Spring Cloud Data Flow can also be used to create and schedule batch applications. For the last decade, Spring Batch has been the framework of choice to develop batch applications. Spring Cloud Task extends this and enables execution of batch programs on the Cloud.

Let's use Spring Initializr (https://start.spring.io) to set up the application. Provide the details listed here and click on Generate Project:

  • Group: com.mastering.spring.cloud.data.flow
  • Artifact: simple-logging-task
  • Dependencies: Cloud Task

Update the SimpleLoggingTaskApplication class with the following code:

@SpringBootApplication
@EnableTask

public class SimpleLoggingTaskApplication {

@Bean
public CommandLineRunner commandLineRunner() {
return strings -> System.out.println(
"Task execution :" + new SimpleDateFormat().format(new Date()));
}
public static void main(String[] args) {
SpringApplication.run(SimpleLoggingTaskApplication.class, args);
}
}

This code simply puts a sysout with the current timestamp. The @EnableTask annotation enables the task features in a Spring Boot application.

We can register the task on the data flow shell using the following commands:

app register --name simple-logging-task --type task --uri maven://com.mastering.spring.cloud.data.flow:simple-logging-task:jar:0.0.1-SNAPSHOT
task create --name simple-logging-task-definition --definition "simple-logging-task"

The commands are very similar to those used to register the stream apps we created earlier. We are adding a task definition to be able to execute the task.

The task can be launched using the following command:

task launch simple-logging-task-definition

Task executions can be triggered and monitored on the Spring Cloud Flow dashboard as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset