• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Java program to calculate next execution time if one cron expression is given

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my project requirement i wanted to have one function which will calculate next execution time using cron expression.

Here i have one cron expression which says every 25 minutes, i want to have one function which will be passed with cron expression and time and function should return next execution time.

i was wondering if any ready function available for the same.
 
Ranch Hand
Posts: 1902
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you could easily enough write something that would tell you the approximate intervals of execution - by adding 25 minutes at a time to a given time - but I'm not sure you can query cron through Java to ask it when the next execution time is. Cron doesn't queue tasks, so you can't inquire from it when the next execution time is.
 
Ashok Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Theodore,
Actually i had given you very simple example but i have one complex cron expression stored in database, based on that i have to calculate the next execution time and store in database so one scheduler process will check if the current time is less than the calculated time then process the transaction.
And there will be different cron expressions for different client subscriptions.
So I was looking at any generic function that can calculate next execution time from the given cron expression.
 
Theodore Casser
Ranch Hand
Posts: 1902
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See, given that, the problem's a different one than the original example. ;)

If you're storing the times of execution in a database, it shouldn't be too hard to retrieve the time-of-last-execution from that table, calculate the next execution time (as you said) and proceed from there. You just need something to parse the expression - try using String.split() or the like to split out the fields and then you can probably very easily write a parser to get the values for each field.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic