How to have a single spring scheduler that support multiple timezones? - java

I have a specific scenario in which I need to run a scheduler at 00:00 AM every day for different timezones. Currently, I am using spring scheduler in fixed rate of 10 minutes so that I will not miss any timezones(Not an ideal solution though!).
But what I need is, I want this scheduler to be running at the specfic time instead of running every 10 minutes. To explain it in detail
Assume I have 3 time zones: T1, T2 and T3.
I want the scheduler to be triggered only 3 times a day. 00:00 AM of T1, T2 and T3 respectively.
I dont want to write 3 schedulers for this because the business logic is the same.
Does springs scheduler provides that flexibility of running a scheduler at different timezones?
If it doesn't are there any library out there which does this job for us?

You can try using quartz to create triggers based on crons. Spring has a native support for quartz with spring-boot-starter-quartz dependency. I would start with this https://www.baeldung.com/spring-quartz-schedule and https://howtodoinjava.com/spring-batch/batch-quartz-java-config-example for spring batch incase you are interested in batch
Quartz supports multiple triggers too for a single job.

Related

Cron Expression With Complex Timing

I have a java application using quartz that needs to run a job at different intervals over the course of the day. It should run every 30 minutes between 8AM and 4PM, skip 4PM to 6PM, and then run every other hour between 6PM and 8AM. I can create a cron expression for any one of those timings but I can't figure out how to combine them into a single expression.
All of the solutions I have found so far (CRON Expression
and
Cron expression with start and end time)
seem to suggest using multiple cron expressions. I'm using quartz (or in my case guartz for guice compatibility) annotations and I'm not sure how to do that.
Is there some way to define the scheduling for my job with a single cron expression? If not, what are my alternatives using quartz?

Streaming data to SQL database every 10 minutes with Springboot

Currently I am trying to improve my skills with Springboot applications and I wanted to know if it is possible for a Springboot application to insert into a MySQL database every 10 minutes (or some quantum of time) while the application is deployed on a server (I am using Elastic Beanstalk), and if so how would I be able to do this and if I would need additional tools to accomplish this.
You can use the #Scheduled annotation.
Here is a pretty nice example using cron, fixedRate, and fixedDelay.
Just be mindful that if you are using dynamic schedules (as shown below)
#Scheduled("${my.dynamic.schedule}")
public myScheduledMethod() {
//do some tasks here
}
that you may also introduce logic to ensure all instances are not running at the same time, performing the same task, to avoid redundant behavior.

Asynchronous Scheduling in Java using Quartz

I need a mechanism for implementing asynchronous job scheduling in Java and was looking at the Quartz Scheduler, but it seems that it does not offer the necessary functionality.
More specifically, my application, which runs across different nodes, has a web-based UI, via which users schedule several different jobs. When a job is completed (sometime in the future), it should report back to the UI, so that the user is informed of its status. Until then, the user should have the option of editing or cancelling a scheduled job.
An implementation approach would be to have a Scheduler thread running constantly in the background in one of the nodes and collecting JobDetail definitions for job execution.
In any case, there are two the questions (applicable for either a single-node or multi-node scenario):
Does Quartz allow a modification or a cancellation of an already scheduled job?
How can a "callback" mechanism be implemented so that a job execution result is reported back to the UI?
Any code examples, or pointers, are greatly appreciated.
Does Quartz allow a modification or a cancellation of an already scheduled job?
You can "unschedule" a job:
scheduler.unscheduleJob(triggerKey("trigger1", "group1"));
Or delete a job:
scheduler.deleteJob(jobKey("job1", "group1"));
As described in the docs:
http://quartz-scheduler.org/documentation/quartz-2.x/cookbook/UnscheduleJob
Note, the main difference between the two is unscheduleing a job removes the given trigger, while deleting the job removes all triggers to the given job.
How can a "callback" mechanism be implemented so that a job execution result is reported back to the UI?
Typically, in the web world, you will poll the web server for changes. Depending upon your web framework, there may be a component available (Push, Pull, Poll?) that makes this easy. Also, what you will need to do is store some state about your job on the server. Once the job finishes you may update a value in the database or possibly in memory. This, in turn, would be picked up by the polling and displayed to the user.
To complete the answer of #johncarl with Quarz also have this
handle event's before and after the excution implementing SchedulerListener interface
Groups your jobs
Support with Enterprise Edition
Most important the cron expressions here's the documentation

How to "make something" in a specific time and day of the week in java?

I have an application that needs to play some specifics audios(mp3) periodically. Let me give one example: Every Monday at 8:00am -> hello.mp3.
How can I do that in Java??
I´m trying using Calendar, JodaTime, but I cant do it.
Depends, if you are using Java EE, you can use EJB Timer. And there is always Quartz http://www.quartz-scheduler.org/
What you want is a scheduled task. Timer class can provide this for you. Here is an example to get you started example
What a Timer can help you to achieve is so minimum, Quartz Scheduler is a well-known (and easy-to-use) scheduler that accepts cron-like expression.
If you use Spring in your application, it has a scheduling module that can keep your Quartz code even cleaner.
You should use java.util.Timer to define schedules and java.util.TimerTask to define job. You can also use well-known Quartz Scheduler.
If you use Spring, you can use its scheduling service (it can be configured to use either Quartz Scheduler or java.util.Timer in its work).
If you run desktop App, the easiest way is to use java.util.Timer. For more complicated schedules use Quartz Scheduler.

nested jobs in Quartz

I have nested task to schedule:
(1). A daily master task downloading scheduling information, which is a List of job names with timestamps
(2). schedule the job in the scheduling information I just downloaded according to its timestamp
I am not sure how the nested jobs work in Quartz. It seems that I need a CronTrigger triggering a job, which contains multiple SimpleTriggered jobs. Are there any way to do that? Are there any alternatives?
Thanks.
Lily
They aren't really nested jobs.
You are correct in that the master job needs a CronTrigger. But when that job runs it will cycle through the list of jobs downloaded creating a job and a SimpleTrigger for each entry. You can get a Scheduler from the CronJob's JobExecutionContext and add the jobs to it.
And bingo, all your jobs are scheduled.
You basically need to run Quartz inside of your Quartz job. This doesn't really make sense, why don't your just run all of the jobs with Quartz configurations? I don't think this nesting is a good idea.

Categories