Spring Batch Admin provides a web-based user interface (UI) that allows you to manage Spring Batch jobs. The project, however, is end-of-life since December 31, 2017.
Let’s show how you can configure Spring Cloud Data Flow to run a batch job.
We re-use the Spring Batch capitalize names project. It contains a batch job that converts person names from lower case into upper case.
We then start a Spring Cloud Data Flow server and configure the batch job. Using the web-based UI we launch the job and check the status.
2. General Project Overview
We will use the following tools/frameworks:
Spring Batch 4.1
Spring Boot 2.1
Maven 3.6
We will create two Maven projects.
One for the batch job:
And one for Spring Cloud Data Flow:
3. Creating a Spring Batch Task
The basic Maven and Spring Boot setup of this project are identical to a previous Spring Batch example. As such we will not cover them in this post.
Spring Cloud Data Flow is a toolkit for building data processing pipelines. The pipelines consist of Spring Boot applications. This means we can run a Spring Boot batch job using a Data Flow server.
All we need to do is annotate our existing SpringBatchApplication with @EnableTask as shown below.
This class-level annotation tells Spring Cloud Task to bootstrap its functionality. It enables a TaskConfigurer that registers the application in a TaskRepository.
Spring Cloud Task will also associate the execution of a batch job with a task’s execution so that one can be traced back to the other. This association is by default in any context that has both a Spring Batch Job configured and the spring-cloud-task-batch JAR available within the classpath.
We can now define a Spring Cloud Task as we will see in the next section.
Spring Cloud Task uses a datasource for storing the results of task executions. When running on Spring Cloud Data Flow we need to make sure that common datasource settings are shared among both.
By default Spring Cloud Data Flow uses an in-memory instance of H2 with the following URL: jdbc:h2:tcp://localhost:19092/mem:dataflow.
We set the same datasource on our Spring Batch Task using the application.yml properties file located in src/main/resources.
To enable the above configuration changes we need to add extra dependencies in the Maven pom.xml file.
The spring-cloud-starter-task starter includes the dependencies for testing Spring Boot applications. It imports libraries that include JUnit, Hamcrest and Mockito.
We also declare a dependency on h2. Spring Boot will take care of the auto-configuration of the datasource when it finds the H2 library on the classpath.
The last thing left to do is to package our Spring Batch Task application as a Spring Boot JAR.
Open a command prompt and navigate to the spring-batch-task project. Execute following Maven command:
The result is a spring-batch-task-0.0.1-SNAPSHOT.jar JAR file in the spring-batch-task/target directory:
4. Running a Spring Cloud Data Flow Server
In this example, we will run Spring Cloud Data Flow on a local server.
Create a new spring-cloud-data-flow-server Maven project.
The spring-boot-starter starter in the pom.xml will import the needed Spring Boot dependencies.
spring-cloud-starter-dataflow-server takes care of the Spring Cloud Data Flow dependencies.
Add the @EnableDataFlowServer annotation to the Spring Boot main class. This activates a Spring Cloud Data Flow Server implementation.
We also disable the auto-configuration of the CloudFoundryClient by adding exclude = {CloudFoundryDeployerAutoConfiguration.class} to the @SpringBootApplication annotation.
That’s it. We can now start our Local Data Flow Server.
Fire up a command prompt in the spring-cloud-data-flow-server project directory. Execute following Maven command:
This will open the Spring Cloud Data Flow dashboard as shown below.
Now it’s time to add our Spring Batch Task application. Click on the Add Application(s) button.
Click on Register one or more applications.
Enter capitalize-names-app as name of our application and select Task as type.
For the URI we enter the location of our Spring Boot Task JAR. In this example, the JAR file is located at: file://C:/Users/Codenotfound/repos/spring-batch/spring-batch-admin/spring-batch-task/target/spring-batch-task-0.0.1-SNAPSHOT.jar
Once done click on Register the application(s).
Our application is now registered.
Now click on the Task menu to create a new task that we can execute.
Click on the Create task(s) button.
Drag the Capitalize-Names-App application on the canvas and connect the START and END nodes as shown below.
Click on Create Task.
Enter capitalize-names-task as task name and click on Create the task.
Our capitalize-names-task is now ready to be used. Click on the play icon to start an instance.
Click on the Launch the task button.
A new Executions tab now appears under the Task section. Click on it to consult the status of the task that we started.
We can see that there is one execution instance for our capitalize-names-task. Click on the information icon to see the details.
Click on the Job Execution Ids identifier to see detailed information on the batch job that was executed.
A new page opens that shows us the details on the Spring Batch capitalizeNamesJob job. We can even see the status and information on the step that was executed.
That’s it, we successfully used a Spring Batch Admin UI to launch a Spring Batch job!
Note that you can also consult the log files of the executed batch job. Check the console output of the Spring Cloud Server for the location of the log files.
If you would like to run the above code sample you can get the full source code here.
In this tutorial, we illustrated an end-to-end scenario in which the Spring Cloud Data Flow user interface was used to launch and monitor a Spring Batch job.