I have a requirement to run a batch job at a fixed interval and have the ability to change the time of this batch job at runtime. For this I came across @Scheduled annotation provided under Spring framework. But I'm not sure how I'd change the value of fixedDelay at runtime. I did some googling around but didn't find anything useful.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
create interface , something like that:
Next, lets implement Trigger interface that is located at org.springframework.scheduling in the spring-context project.
now configuration:
and usage:
In spring boot, you can use an application property directly!
For example:
Note that you can also have a default value in case the property isn't defined, eg to have a default of "60" (seconds):
Other things I discovered:
private
I found being able to use
private
visibility handy and used it in this way:Being
private
, the scheduled method can be local to your service and not become part of your Service's API.Also, this approach allows the
process()
method to return a value, which a@Scheduled
method may not. For example, yourprocess()
method can look like:to provide some information about what happened during processing.
You can use a
Trigger
to dynamically set the next execution time. See my answer here:Scheduling a job with Spring programmatically (with fixedRate set dynamically)
AFAIK the Spring API won't let you access the internals you need to change the trigger. But you could instead configure manually the beans:
Then as documented in SchedulerFactoryBean: