0

I need to force my java application to perform a couple of actions on a specific date and time. The scenario is as following:

1- user set a specific time and frequency (every day, every month)
2- system starts a trigger for the request

3- once that pre-defined frequency and time are reached 
 3.1 - system performs the required actions that are kept in a method

I have found this answer but could not make it work.

An example would be appreciated.

Community
  • 1
  • 1
Tim Norman
  • 421
  • 1
  • 12
  • 31

3 Answers3

3

There are multiple java scheduler frameworks available to do the tasks at your specified time, interval, periodicity. Apache quartz is one of the commonly used ones.

or simply make use of java ScheduledExecutorService

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

Quartz library is very useful for this.

How to do using Quartz: refer this http://www.mkyong.com/tutorials/quartz-scheduler-tutorial/

Jayesh
  • 6,047
  • 13
  • 49
  • 81
2

You can use Executor from jdk itself, here is kick off example:

 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 ScheduledFuture<?> handle scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() { System.out.println("your code is here :)"); }
        }, 1, 100, TimeUnit.MINUT);

so this code starts running after 1 min and runs after each 100 min.

in order to cancel later you will do handle.cancel(true)

Read from here

Elbek
  • 3,434
  • 6
  • 37
  • 49