问题
I have a service class that I need to execute a method on a regular basis (fixed rate) based upon a configuration parameter. Originally I was going to use the Java TimerTask and then configure a timer to run it. Then also tie in spring application context lifecycle events to stop, start the said timer.
When parsing their documentation I came across this. This offers a nice benefit that this task's life cycle can be managed by spring automatically for me. Also, allows scale-ability such that if I wanted to throw this into Quartz or implement some sort of other scheduling implementation, it is more of a configuration change as opposed to code changes.
It sounded really enticing but then I checked out their java docs and discovered that the class is deprecated
The deprecation notes indicate that developers should be using the spring.scheduling.concurrent package instead. However, that package does not include any similar class that offers abstraction of the Runnable interface (which I would like to do). Is this functionality not available anymore in spring? Should my service class implement the Runnable interface then and simply have that method invoke my Service Interface defined method?
回答1:
I ended up going with the following solution:
<!-- Scheduled tasks to be exectued -->
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="myClass" method="myMethod" fixed-rate="${some.value}"/>
</task:scheduled-tasks>
<!-- The scheduler that executes scheduled tasks -->
<task:scheduler id="scheduler" pool-size="5"/>
回答2:
You can use @Scheduled Annotation for this.
来源:https://stackoverflow.com/questions/9825827/methodinvokingtimertaskfactorybean-is-deprecated-what-class-should-i-be-using-i