spring-guides / gs-scheduling-tasks Goto Github PK
View Code? Open in Web Editor NEWScheduling Tasks :: Learn how to schedule tasks with Spring.
Home Page: https://spring.io/guides/gs/scheduling-tasks/
License: Apache License 2.0
Scheduling Tasks :: Learn how to schedule tasks with Spring.
Home Page: https://spring.io/guides/gs/scheduling-tasks/
License: Apache License 2.0
Missing a valid test for the example implementation.
The guide uses Spring Boot 3.2 so the requirements need to be updated.
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.
The guide says Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
but this is not a web application.
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
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 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.
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)
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
There are some version mismatches for the dependency. One part talks about using awaitility 3.1.2 but the finished build files have 4.2.0
The guide requests to add the awaitility dependency, though there is not test present, that dependency has test scope.
Proposal : oneOf
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.