GithubHelp home page GithubHelp logo

spring-guides / gs-scheduling-tasks Goto Github PK

View Code? Open in Web Editor NEW
111.0 27.0 154.0 994 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 92.39% Shell 7.61%

gs-scheduling-tasks's People

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

gs-scheduling-tasks's Issues

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.

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!!

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.

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)

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

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.

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.