To schedule a Spring Batch job to run automatically when the application starts up, you can use the @Scheduled
annotation on a method in a @Configuration
class.
First, annotate the configuration class with @EnableScheduling
so that Spring knows to look for scheduled tasks.
Then, create a method in the configuration class and annotate it with @Scheduled
to indicate that it should be run according to a fixed schedule.
Inside the method, use the JobLauncher
to launch the Spring Batch job.
Here is an example:
@Configuration
@EnableScheduling
public class BatchConfig {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job myJob;
@Scheduled(fixedDelay = 1000) // run every second
public void runJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
jobLauncher.run(myJob, new JobParameters());
}
}
In this example, the runJob()
method will be automatically executed every second due to @Scheduled(fixedDelay = 1000)
. Inside the method, the JobLauncher
launches the myJob
Spring Batch job with an empty JobParameters
object.
Note: If you don't want your job to be executed repeatedly, you can change fixedDelay
to something like fixedRate
or use a Cron expression to schedule your job according to your needs.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-06 11:12:43 +0000
Seen: 9 times
Last updated: May 06 '23
Is it possible to globally install System.Data.OleDb?
How to debug NextJS application in VSCode?
Why does the app shut down when switching to the Google Maps section in a Flutter application?
How can an application Manager be put in place to manage the system?
How can a Qt application detect a trigger triggered by a batch script?
How can I execute a React Native application on my Android device?
What conditions could cause Wildfly to automatically undeploy an EAR?