/gs-spring-cloud-task

Spring Cloud Task :: Learn how to build and test Spring Cloud Task Applications

Primary LanguageJava

Getting Started with Spring Cloud Task

This guide walks you through an overview of Spring Cloud Task and the process of creating and launching a short-lived application.

Starting with Spring Initializr

You can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.

To manually initialize the project:

  1. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  3. Click Dependencies and select Task, H2 Database, JDBC API, and MariaDB Driver.

  4. Click Generate.

  5. Download the resulting ZIP file, which is an archive of a task application that is configured with your choices.

Note
If your IDE has the Spring Initializr integration, you can complete this process from your IDE.
Note
You can also fork the project from Github and open it in your IDE or other editor.

What is Spring Cloud Task?

Spring Cloud Task is a framework for building short-lived Spring Boot microservices, such as batch data processing jobs. You can learn more about the framework from the project-site, documentation, and samples.

Task Application Overview

In this guide, we develop a Spring Boot application that uses Spring Cloud Task and deploy it to your local machine. We use an H2 database to initially demonstrate Spring Cloud Task functionality, but the value of Spring Cloud Task is in its ability to store the results of executed tasks in a persistent datastore. For this example, we progress by using MariaDB as our persistent datastore.

Spring Cloud Task using In Memory H2

For the first step, we use Spring Cloud Task with an embedded H2 datastore. If no spring.datasource properties are detected on project initialization, Spring Cloud Task spins up a new H2 instance to store the executed task results. This happens without any extra configuration. This facilitates a quick way to get up and running, but the results of your executed task are lost when the application completes and the H2 database shuts down. For this step in the guide, ensure that the spring.datasource properties in application.properites are commented out.

Spring Cloud Task Using Persistent MariaDB

To persist data that lives longer than our short-lived task, we need an externally available datastore. In this guide, we use a MariaDB instance running in a Docker container.

Note
To follow along with this guide, you need to run Docker locally.

To run MariaDB locally, run the following command:

docker run -p 3306:3306 --name mariadb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=task -d mariadb:latest

When the MariaDB instance is running, supply the configuration to your application through application.properties:

spring.datasource.url=jdbc:mariadb://localhost:3306/task
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver

Running the application again should now store the results of the Task execution inside of the MariaDB instance. You can verify this by getting a shell into the Docker container:

docker exec -it mariadb mariadb --user root -ppassword

And then running the commands

use task;
show tables; --show all tables that Spring Cloud Task created
select * from task_execution;

You should be able to see all program runs that you executed while the MariaDB instance is running.

Summary

Congratulations! You have completed Spring Cloud Task’s high-level overview, and you were able to build, test, and launch a Spring Cloud Task application.