-1

I have a method that I want to be invoked periodically: every day on 11 am. It is a simple method in Main:

public void loadProduct() {
PropertyConfigurator.configure("log4j.properties");
try {
     service.create(product);
     logger.info("Creation started");
} catch (Exception e) {
  // Log Exception
     logger.error(e);
}
}

I have almost figured out how to achieve this with the help of Spring context:

<task:scheduler id="scheduler" pool-size="1"/>

<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="productTask" method="loadProduct" cron="0/30 * * * * *"/>
</task:scheduled-tasks>

But how to schedule the task to start every 24 hours on 11 am every day?

Or is there a way to achieve this in Java code?

samba
  • 2,821
  • 6
  • 30
  • 85
  • you could create a timer or some daemon thread that checks the system time every x seconds (or a time on a server) – Stultuske Nov 17 '17 at 13:15
  • 1
    You appear to have a utility that lets you run cron jobs. The crux of your question is about learning how to write cron jobs, which would be better learned from a man page about them. – Makoto Nov 17 '17 at 13:17
  • you can use http://corntab.com/; consider that spring scheduler has seconds while crontab does not. So there are 5 (*) in crontab and 6 in spring scheduler – Nonika Nov 17 '17 at 13:23
  • 2
    "0 0 11 * * *" = 11 o'clock of every day. – Nonika Nov 17 '17 at 13:31
  • as @Nonika says just have a runnable jar and execute it on the Cron – Essex Boy Nov 17 '17 at 13:35
  • Related question: [java - Spring cron expression for every day 1:01:am](https://stackoverflow.com/questions/26147044/). – Sergey Vyacheslavovich Brunov Nov 17 '17 at 15:15

1 Answers1

2

But how to schedule the task to start every 24 hours on 11 am every day?

This can be achieved by using the cron expression: 0 0 11 * * *.

Or is there a way to achieve this in Java code?

Yes, by using the Scheduled (Spring Framework 5.0.1.RELEASE API) annotation, for example:

@Scheduled(cron = "0 0 11 * * *", zone = "Europe/Moscow")
public void run() {
    // ...
}

Additional references:

  1. Integration: 7. Task Execution and Scheduling: 7.4. Annotation Support for Scheduling and Asynchronous Execution, Spring Framework Documentation.
  2. Getting Started · Scheduling Tasks.