Code Monkey home page Code Monkey logo

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 Framework’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 guide.

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 and Gradle.

  3. Click Generate.

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

Note
If your IDE has the Spring Initializr integration, you can complete this process from your IDE.

Enable Scheduling

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

@SpringBootApplication
@EnableScheduling
public class SchedulingTasksApplication {

Spring Initializr adds the @SpringBootApplication annotation to our main class. @SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration: Tags the class as a source of bean definitions for the application context.

  • @EnableAutoConfiguration: Spring Boot attempts to automatically configure your Spring application based on the dependencies that you have added.

  • @ComponentScan: Tells Spring to look for other components, configurations, and services. If specific packages are not defined, recursive scanning begins with the package of the class that declares the annotation.

Additionally, add the @EnableScheduling annotation. This annotation enables Spring’s scheduled task execution capability.

Create a Scheduled Task

Create a new class src/main/java/com/example/schedulingtasks/ScheduledTasks.java called:

@Component
public class ScheduledTasks {

	private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

	@Scheduled(fixedRate = 5000)
	public void reportCurrentTime() {
		log.info("The time is now {}", dateFormat.format(new Date()));
	}
}

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. Other options are cron() and fixedDelay(). For periodic tasks, exactly one of these three options must be specified, and optionally, initialDelay(). For a one-time task, it is sufficient to just specify an initialDelay()

Running the Application

You should now be able to run the application by executing the main method in SchedulingTasksApplication. You can run the program from your IDE, or by executing the following Gradle command in the project root directory:

./gradlew bootRun

Doing so starts the application, and the method annotated with @Scheduled runs. You should see log messages similar to:

20yy-mm-ddT07:23:01.665-04:00  INFO 19633 --- [   scheduling-1] c.e.schedulingtasks.ScheduledTasks       : The time is now 07:23:01
20yy-mm-ddT07:23:06.663-04:00  INFO 19633 --- [   scheduling-1] c.e.schedulingtasks.ScheduledTasks       : The time is now 07:23:06
20yy-mm-ddT07:23:11.663-04:00  INFO 19633 --- [   scheduling-1] c.e.schedulingtasks.ScheduledTasks       : The time is now 07:23:11
Note
This example uses fixedRate() scheduling, so the application runs indefinitely until you interrupt it manually.

Testing with the awaitility Dependency

To properly test your application, you can use the awaitility library. Since Spring Boot 3.2, this is a dependency that Boot manages. You can create a new test or view the existing test at src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java:

@SpringBootTest
public class ScheduledTasksTest {

	@SpyBean
	ScheduledTasks tasks;

	@Test
	public void reportCurrentTime() {
		await().atMost(Durations.TEN_SECONDS).untilAsserted(() -> {
			verify(tasks, atLeast(2)).reportCurrentTime();
		});
	}
}

This test automatically runs when you run the ./gradlew clean build task.

Summary

Congratulations! You created an application with a scheduled task.

gs-scheduling-tasks's People

Contributors

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gs-scheduling-tasks's Issues

Job running multiple times

Hi!

I have scheduled 3 jobs :

@Slf4j
@Component
public class ScheduledTasks {

   @Autowired
   JobLauncher jobLauncher;

   @Autowired
   JobRegistry jobRegistry;


   // scheduled every 14 hours
   @Scheduled(cron = "* * */14 * * *")
   public void doPopulateCacheWithARIDataJob()
           throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

      // given
      log.info("Running Scheduled task [ populateCacheWithARIData ]");
      Job ariJob = jobRegistry.getJob("populateCacheWithARIData");

      // then
      jobLauncher.run(ariJob, new JobParametersBuilder().addString("time", LocalDateTime.now().toString()).toJobParameters());
   }


   // scheduled every 14 hours
   @Scheduled(cron = "* * */12 * * *")
   public void doMigrateBookingsJob()
           throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

      // given
      log.info("Running Scheduled task [ MigrateBookings ]");
      Job migrationJob = jobRegistry.getJob("migrateReservasToBookings");

      // then
      jobLauncher.run(migrationJob, new JobParametersBuilder().addString("time", LocalDateTime.now().toString()).toJobParameters());
   }


   // scheduled every 4 hours
   @Scheduled(cron = "* * */4 * * *")
   public void doPopulateCacheWithCalendarDataJob()
           throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

      // given
      log.info("Running Scheduled task [ populateCacheWithCalendarData ]");
      Job calendarJob = jobRegistry.getJob("populateCacheWithCalendarData");

      // then
      jobLauncher.run(calendarJob, new JobParametersBuilder().addString("time", LocalDateTime.now().toString()).toJobParameters());
   }
}

And added @enableScheduling in my Spring boot's Application.java.

The thing is that the jobs run multiple times even after the first execution finishes with COMPLETED.

I don't know if this is due to some misconfiguration but can't fix it.

Any thoughts?

thanks in advance

Multiple Triggers from Cloud

Hi Everyone. I have an issue with the Jobs being triggered from Multiple instances of Pivotal Cloud environments. How to handle this issue. Thanks!!

use variables from varenv for delay / rate

I think using variables is way more powerful

Since the jar execution is in the guide, it can also be modified to show how scheduling can be parametrized

I would use expressions this way :

	@Value("${schedulingtasks.report.skip:false}")
	private boolean skipReport;

	@Scheduled(fixedRateString = "${schedulingtasks.report.period:5000}", initialDelayString = "${schedulingtasks.report.delay:0}")
	public void reportCurrentTime() {
		if(skipReport) return;

And would change the invocation to have the delay modified to 10s with

java -jar target/gs-scheduling-tasks-0.1.0.jar -Dschedulingtasks.report.delay=10000

(I guess this is how to transmit the variable with jar)

does not use awaitility

The guide requests to add the awaitility dependency, though there is not test present, that dependency has test scope.

Proposal : oneOf

  1. add test that require that library
  2. remove that dependancy.

Needs a rewrite

Needs to be structured to have an initial and a complete project and instructions that go from a pre-populated Initializr project to a running project with outputs that the reader can confirm.

Testing schedulers

It would be interesting to see, how we could actually properly test the schedulers (I mean without using Thread.sleep(x) :) ). Whether there's at least good configuration somehow from the context or anything alike.

Cannot autowire mongoRepositories inside the ScheduledTasks component

I want to extend the ScheduledTasks component to wire MongoRepositories. I want to make an external call to an api (using RESTTemplate) and persist the results to a mongoRepository.

The stacktrace
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private hello.VideoRepository hello.ScheduledTasks.videoRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hello.VideoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:683)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
at hello.Application.main(Application.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:695)
Caused by: org.springframework.beans.factory.BeanCr

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.