Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Scheduling a run for method in java code

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All;

I have method in Java code file and I need this method to executed periodically (once every day at 9 PM for example), is it possible to do this in Spring? How?

Regards
Bilal
 
Saloon Keeper
Posts: 21227
137
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a Spring scheduler and one of the scheduling systems it supports is the cron-like Quartz Scheduler.

However, if you don't need the app running the other 23 hours of the day, instead of making the application sleep, consider making the entire application go away and only run at the scheduled time. In Unix/Linux/MacOS, you can do this through the cron subsystem. Or in recent (systemd) systems, the systemd scheduler.

For Microsoft Windows, there's the equivalent Windows Scheduler.
 
Bilal Ghayad
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you a lot Tim.

I will look for Quartz Scheduler, but do you have an idea what is the advantage of using Quartz Scheduler than using cron subsystem in Unix/Linux?

From the other side: Is I required to have main method in the class to be able to run it?
I am thinking if possible to run the code by calling the class name which contains constructor?

Appreciate Tim if you can help me on the best way to run java code from crontab. Is it by calling the class name or the method name and if main method is needed or no need for this and if there are main things I have to take care for it to be able to run the code successfully from command line. With fully thanks in advance.
Regards
Bilal
 
Tim Holloway
Saloon Keeper
Posts: 21227
137
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quartz and the Spring Scheduling services are Java code. For them to work, you have to already be running a Java application. So if you've got something like a web application that accepts transactions during the day but doesn't process them until 3AM, you could create a scheduled thread to run every day at 3AM.

Cron, on the other hand, is a general OS service and it can launch any sort of program, whether it's a Java program or not. Cron is much, much older than Java and doesn't care specifically about Java, so when it runs programs on schedule, it expects you to do so the same way you would run them from a command prompt. That is, something like "/usr/bin/java -jar /opt/com/coderanch/myscheduledapp.jar".
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are doing this in an existing Spring Boot application, this is easy enough with just two annotations.

1) Annotate one of your configuration classes with @EnableScheduling; your main class that's already annotated with @SpringBootApplication will be good enough.
2) Annotate a method of a bean with @Scheduled(cron = "...").
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!