Cron Expression With Complex Timing - java

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?

Related

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

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.

Need to schedule (cron job) a Java app which is deployed in three instances from pipeline

We have a Java app that will be deployed into three instances at the same time due to the pipeline policy, which does the MongoDB clean-up (deletion of unnecessary records). The Java app will use the cron expression to start its cleanup activity, since all three instances will have the same cron expression, the 3 apps will start to do the cleanup in parallel which is going to create a mess. So, I want to avoid all three instances starting at once instead any one instance should be eligible for cleanup.
Can someone help me to resolve this issue?

Spring Cron expression to not execute job at all at any time? [duplicate]

This question already has answers here:
Spring Cron scheduler “disable pattern”
(3 answers)
Closed 3 years ago.
I am working on job schedule in Spring app. I want to stop job execution. So, Can someone help me to provide CRON expresssion for the same.
I have tried few links in googling but as there is no year field(Which is available in other CRON like Unix etc) in the SPRING CRON expression, we can not give previous year to disable the same.
I can comment OR remove bean code, that will disable it BUT my requirement is to achieve the same via spring CRON expression.
Thanks in Advance.
quartz..cron=0/90 * * * * ?
This webpage helped me for all my cron needs on my schedulers:
Cron Maker
I don´t get how often you want your tasked to be performed but in anycase, hopefully the web page helps you.
'The special value "-" indicates a disabled cron trigger, primarily meant for externally specified values resolved by a ${...} placeholder', according to the javadoc. I am using the 'cron' property of #Scheduled, so I don't know if this applies to your implementation.

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.

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.

Categories