Giter Site home page Giter Site logo

luismendes070 / gs-scheduling-tasks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-guides/gs-scheduling-tasks

0.0 2.0 0.0 1003 KB

Scheduling Tasks :: Learn how to schedule tasks with Spring.

Home Page: https://spring.io/guides/gs/scheduling-tasks/

License: Apache License 2.0

Java 74.37% Shell 6.13% Starlark 19.50%
android-application async netlify bazel-build

gs-scheduling-tasks's Introduction

This guide walks you through the steps for scheduling tasks with Spring.

What You Will Build

You will build an application that prints out the current time every five seconds by using Spring’s @Scheduled annotation.

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 Generate.

  4. Download the resulting ZIP file, which is an archive of a web 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.

Adding the awaitility Dependency

The tests in complete/src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java require the awaitility library.

Note
Later versions of the awaitility library do not work for this test, so you have to specify version 3.1.2.

To add the awaitility library to Maven, add the following dependency:

<dependency>
  <groupId>org.awaitility</groupId>
  <artifactId>awaitility</artifactId>
  <version>3.1.2</version>
  <scope>test</scope>
</dependency>

The following listing shows the finished pom.xml file:

link:complete/pom.xml[role=include]

To add the awaitility library to Gradle, add the following dependency:

	testImplementation 'org.awaitility:awaitility:3.1.2'

The following listing shows the finished build.gradle file:

link:complete/build.gradle[role=include]

Create a Scheduled Task

Now that you have set up your project, you can create a scheduled task. The following listing (from src/main/java/com/example/schedulingtasks/ScheduledTasks.java) shows how to do so:

link:complete/src/main/java/com/example/schedulingtasks/ScheduledTasks.java[role=include]

The Scheduled annotation defines when a particular method runs.

Note
This example uses fixedRate, which specifies the interval between method invocations, measured from the start time of each invocation. There are other options, such as fixedDelay, which specifies the interval between invocations measured from the completion of the task. You can also use @Scheduled(cron=". . .") expressions for more sophisticated task scheduling.

Enable Scheduling

Although scheduled tasks can be embedded in web applications and WAR files, the simpler approach (shown in the next listing) creates a standalone application. To do so, package everything in a single, executable JAR file, driven by a good old Java main() method. The following listing (from src/main/java/com/example/schedulingtasks/SchedulingTasksApplication.java) shows the application class:

link:complete/src/main/java/com/example/schedulingtasks/SchedulingTasksApplication.java[role=include]

The @EnableScheduling annotation ensures that a background task executor is created. Without it, nothing gets scheduled.

Logging output is displayed, and you can see from the logs that it is on a background thread. You should see your scheduled task fire every five seconds. The following listing shows typical output:

...
2019-10-02 12:07:35.659  INFO 28617 --- [   scheduling-1] c.e.schedulingtasks.ScheduledTasks       : The time is now 12:07:35
2019-10-02 12:07:40.659  INFO 28617 --- [   scheduling-1] c.e.schedulingtasks.ScheduledTasks       : The time is now 12:07:40
2019-10-02 12:07:45.659  INFO 28617 --- [   scheduling-1] c.e.schedulingtasks.ScheduledTasks       : The time is now 12:07:45
2019-10-02 12:07:50.657  INFO 28617 --- [   scheduling-1] c.e.schedulingtasks.ScheduledTasks       : The time is now 12:07:50
...

Summary

Congratulations! You created an application with a scheduled task. Also, this technique works in any type of application.

gs-scheduling-tasks's People

Contributors

gregturn avatar cbeams avatar royclarkson avatar btalbott avatar habuma avatar luismendes070 avatar bclozel avatar snicoll avatar heaven31415 avatar buzzardo avatar spring-operator avatar naftalmm avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.