#Scheduled and scheduledExecutorService.scheduleWithFixedRate in java - java

What is the difference between #Scheduled annotation in Spring and using ScheduledExecutorService's scheduleWithFixedDelay method in Springboot? With the help of code, can someone demonstrate the exact usage difference and when to opt for which one?

Please have a look at https://www.baeldung.com/spring-task-scheduler . The #Scheduled is a generic implementation, which you can extend and configure for your needs. While the scheduleWithFixedDelay is a specific implementation that you can directly use. Ideally, it is fine to use either of them, but if your functionality is not supposed to change, it is better to use the implementation (I mean scheduleWithFixedDelay)

#Scheduled without a cron expression, fixeddelay or fixedrate is nothing. How can it know the interval, if not specified?
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html
fixedDelay ensures that there is always n seconds duration
if first invocation is at 9:00:00 and fixedDelay = 10minutes, process
takes 15 seconds to complete, then next cycle start = 09:10:15

Related

Dynamically not running #Scheduled jobs based on current state of a bean in Spring Boot

I'm in the need of runnning/not running a #Scheduled job based on a boolean that's dynamically computed inside of a bean (periodically and also via #Scheduled the bean reads a file and check its content). I've spent a lot time thinking and Googling and haven't come up with a solution until now.
#ConditionalOn* would not work as that is only read once during initialization
Subclassing the scheduler doesn't work as the schedule methods are also only called once at startup
Checking for the property in the scheduled method doesn't work for me as it must not get called at all
Anyone got a clever idea?
Thanks!
You can start and stop the scheduler programmatically.
ScheduledFuture<?> scheduledFuture = taskScheduler.schedule(<runnable>, <interval>);
Then you can check for the condition and if it should run schedule (or let it run) or if it shouldn't run you cancel it:
scheduledFuture.cancel(true);
I solved it with an Aspect #Around Spring's #Scheduled annotation and then not calling proceedingJoinPoint.proceed():
#Around("#annotation(org.springframework.scheduling.annotation.Scheduled)")
Handling Shedlock wasn't as 'easy' but I was able to stop the executions with this #Around and then returning Optional.empty() (pretending Shedlock cannot get hold of a lock):
#Around("execution(* net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.lock(..)) "
+ "|| execution(* net.javacrumbs.shedlock.core.LockProvider.lock(..))")

Call Spring Component method with delay

I have a list of n UUIDs. Using each UUID I'm retrieving some data and do some logic. The problem is that if I will execute all of them at once it will create high load. So the target is to call Spring Component method for each UUID with fixed delay 1s. For example for the first UUID method will be called after 1 second delay, for the second after 2 second delay etc. And it should be executed only once. How can I do it correctly in Spring framework? I almost sure that Spring should have some mechanism for doing that. And I'm trying to avoid using Thread.sleep or pure Java ways.
Will the spring task executor work for you?
Task Execution and Scheduling

Java EE6 Schedule Range

I'm required to have a schedule that runs every 5 minutes from 10 am to 5:45pm, how do I do this with the #Schedule annotation?
So far, I'm limited to the #Schedule(hour=10-18;minute=*/5), but they insist I should have it until 5:45pm not 6pm.
As clearly stated in the documentation for #Schedule and #Schedules, you need to have two #Schedule annotations if you run two schedules - even if you don't like that fact.
Due to the cron-like limitations of having ranges only within individual elements (hours, minutes, seconds...), it's simply not posible to give that additional information of skipping the last two executions at *:50 and *:55 only at 5pm.
That said, you'd probably end up with something like
#Schedules({
#Schedule(hour="10-16" minute="*/5"),
#Schedule(hour="17" minute="0,5,10,15,20,25,30,35,40,45")
})
As you end up with schedule information into you sourcecode that way (even if it's in the form of an annotation) you could just as well run every five minutes and immediately return from the method if called after 5:49pm

Spring Boot - check if target date is reached for objects

I don't know if it's a real question or not... But i'd like to know how some of you will approach this...
I have a Spring Boot application.
Then I have a Interruttore.class, which has, among others this field timeoutDatewhich is a Date.
In the app, various instances of this class are used. The timeoutDate field can be updated, for every single object, by various factors. I need to know when the actual date reaches the timeutDate.
In a very simple (and not optimized) way i would have created a #Scheduled task, but the delay will be too short and i don't like it, how can i do?
In a very simple (and not optimized) way i would have created a
#Scheduled task, but the delay will be too short and i don't like it,
how can i do?
Why too short ?
You can use the delay you wish.
#Scheduled(fixedDelay=50000) // 50 secs
#Scheduled(fixedDelay=1000) // 1 secs
Look at the documentation for Spring's various task scheduling APIs: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html
You have plenty of choices. I think the "not optimised" idea you might have is to schedule a repeating task which searches your beans to find the expired ones. That would indeed be inefficient for large numbers of beans.
You could simply create a scheduled task for each bean with a timeoutDate, created at the same time as that bean, and when its timeoutdate is updated (Spring AOP could help with this).
Alternatively you could keep a list of beans, sorted by timeout date. Schedule a task for the time of the earliest expiry. It reaps that bean and any others who's time is past, then schedules a new task for the time of the next expiry.
If you do this, you need to make sure that:
- it handles new objects added to the list (perhaps with an expiry date earlier than the currently scheduled cull)
- it handles the case where an object is removed for a reason other than a timeout
(Unless neither of those things can happen -- in which case don't worry about it!)
You can use Quartz or Jesque(redis). Whatever task needs to be executed, you can schedule that task at that time.
If this time value can be updated anytime, you can cancel(unschedule) the previously scheduled task(using task identifiers or keys) and reschedule it with the updated time.

#Scheduled annotation Spring

I am using Spring 3 annotation #Scheduled to create scheduled jobs on server. But i am confused about the parameters(cron, fixedDelay ,fixedRate) of #Scheduled annotation. Please explain the difference between these parameter and the situations in which I can use these parameters.
I believe the difference among different options are made clear here. It depends on how you need to execute the task:
fixedRate makes Spring run the task on periodic intervals even if the last invocation may be still running.
fixedDelay specifically controls the next execution time when the last execution finishes.
cron is a feature originating from Unix cron utility and has various options based on your requirements.
cron : A cron-like expression, extending the usual UN*X definition to include triggers on the second as well as minute, hour, day of month, month and day of week.
fixedDelay : Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
fixedRate : Execute the annotated method with a fixed period between invocations.
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

Categories