I have job that will run every 10 mins. I don't want to use Spring Scheduler based on last job run next job will schedule to run. Suppose First job ran at 10:15 AM, Subsequent job needs to run at 10:25 AM. When i googled i saw posts with nextExecutionTime. When i use nextExecutionTime my subsequent job is running at 10:20 instead of 10:25. Below is my code, Can any one give an idea how i can run my job at exact 10 mins from last run.
CronTrigger trigger = new CronTrigger("0 0/10 * * * ?");
SimpleTriggerContext triggerContext = new SimpleTriggerContext();
triggerContext.update(Date.from(ZonedDateTime.now().toInstant()), Date.from(ZonedDateTime.now().toInstant()), Date.from(ZonedDateTime.now().toInstant()));
Date nextFireAt = trigger.nextExecutionTime(triggerContext);
System.out.println(nextFireAt);
Can you try the below. I provide below the sequence.
Get the date now.
Add 10 mins to the date and get a new updated date
Update the like triggerContext.update(null, null, date in which you have added 10 mins);
Related
I am running a job scheduler in spring boot application.
To execute job every 5 min, I am using below cron expression
#Scheduled(cron = "0 0/5 * * * *", zone ="EST").
This is working fine
But when I try to execute the same job at a specific time, for instance at 11:30 AM, I am using below cron expression
#Scheduled(cron = "0 30 11 * * *", zone ="EST")
This is not working
I couldn't find any mistake w.r.t syntax. Please help if I am doing anything wrong
I have a JUnit test that looks for a user that hasn't changed their password in the past 355 days. In the test, I save a user with their last password update date to the system's date/time - 355 days. Then I run the function that should pick up that user for additional processing.
The test passes locally, both when the test is run on its own AND when the entire test suite is run. However, for some reason, the test fails between 12 - 1pm when deploying our code in Jenkins. It's not a consistent failure, either. Sometimes, simply running the deployment again with no changes allows the test to succeed. What could be causing this issue?
For reference, the test looks like:
#Test
public void passwordExpiryTest() {
Timestamp currentTimeMinus355Days = new Timestamp(System.currentTimeMillis() - 30672000000L);
createUser("expiringUser", currentTimeMinus355Days);
createUser("recentUser", new Timestamp(System.currentTimeMillis() - 864000000L));
List<User> passwordExpiringUsers = userDao.getUsersWithPasswordExpiringInTenDays();
Assert.assertEquals(1, passwordExpiringUsers.size());
}
And userDao.getUsersWithPasswordExpiringInTenDays is a SQL query (into a Postgres table):
SELECT * FROM users WHERE last_password_update IS NOT NULL AND EXTRACT(epoch FROM (NOW() - last_password_update))/86400 >= 355 AND EXTRACT(epoch FROM (NOW() - last_password_update))/86400 <= 356")
I would investigate the logic in userDao.getUsersWithPasswordExpiringInTenDays(). The concept of a "day" depends on an interpretation - is it 24 hours, or the next day number (e.g. 11 vs 12 of April?)? And is there an impact of time zones? e.g. are you testing in India, but the server lives in the US (12 hours time difference?)
I want to schedule two task which will run everyday at 1.01 AM CST/CEST (as server is on BST --> 00:01) and & 17.30 CST/CEST (So BST --> 16.30).
Because of day light saving time changes I have to adjust is manually. I want it to adjust automatically.
So how can I handle it in cron job
First task
#Scheduled(cron = "${job.schedule}") // 00:01
public void startSchedulePullData() throws Exception {
LOGGER.info("Fetching all schedules");
List<FileForTransfer> dataPullSchedulesList = dbUtils.findPullDataScheduled();
Second task
#Scheduled(cron = "${job.schedule.fileransfer}") // evening 5:30 PM CET
public void startScheduleFileTransfer() throws Exception {
LOGGER.info("Fetching all schedules");
List<FileForTransfer> fileTransferScheduleList = dbUtils.findFileTransferScheduled();
In properties file
job.schedule=0 01 00 * * *
job.schedule.fileransfer = 0 30 16 * * *
During daylight saving changes, time should be updated at the server/machine where your code is running and it will be automatically reflected in your code without any manual adjustment.
If you feel this doesn't answer your question, please elaborate a bit more on the specific scenario and the adjustment you have to manually make.
I have a spring batch job with a #Scheduled(fixedDelay=5000) annotation. But it starts 5 seconds after the end of the previous execution. How can I start it 5 seconds after the start of the previous execution?
Use #Scheduled(fixedRate = 5000). You can also use a CRON expression #Scheduled(cron = "*/5 * * * * ?") but it seems like an overkill.
I want my java program to be run at a specific date and time requested by the user which will be in the form of Timestamp the requested timestamp will be stored in the database and the code should start running at that point of time.
should I use Timer class for this or Quartz scheduler. please advice me a better solution. I am new to java so I'm not that familiar with these scheduler. if anyone can help me by giving a simple example it'll be a great help for me how can I give the timestamp as parameter in timer .
for (int i = 0; i < 4; i++)
{
if (bur[i] > 0) {
if (bur[i] > qtm) {
execOrder.add(i + 1);
bur[i] = bur[i] -qtm;
flagClounter++;
} else {
execOrder.add(i + 1);
bur[i] = 0;
flagClounter++;
}
}
}
if the above is the code part ..how can I use it using timer and how to give the Timestamp there or in Quartz. please help me.
Quartz scheduler is a very good option for achieving these kind of functionalities in java..Go with it.. http://www.tutorialsavvy.com/2012/12/quartz-scheduler-scheduling-job-in-java.html
You can use Quartz triggers..
Basically quartz has two type of triggers
1. Simple Trigger
2. Cron Trigger
Suppose if you want to run you Job on a particular Date and Time then use Cron Trigger. Cron trigger accepts Cron Expression which looks like below
expression=59 59 23 FRI * ?
Expression is says job should execute every Friday night at 11:59:59 PM
More expression can be obtained here.
On the other hand Simple trigger accepts milliseconds and executes once your application starts. i.e if Milliseconds specified is 10000, then job executes after 10000 milliseconds once application starts.