3

I'm a rookie in programming and I have only basic concepts of Java. I need for a school project a solution for executing a Java program (class) periodically (let's say once every 6 hours) and collecting the data from it. The Java program is a client that collects messages published by a server once every 5 minutes. For resolving this my first thought was Thread() and Runnable() but like I said I have only basic concepts of Java and I don't know how to do it. The solution has to be in Java too. If someone has any ideas help is much appreciated, thanks PS sorry for my bad english...

zbr
  • 6,860
  • 4
  • 31
  • 43
Gon Freecs
  • 33
  • 4
  • You may use a schedulling library such as [quartz](http://quartz-scheduler.org/) – dotvav Aug 25 '15 at 09:50
  • 2
    [link](http://stackoverflow.com/questions/4544197/how-do-i-schedule-a-task-to-run-at-periodic-intervals) [link](http://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/) – 2787184 Aug 25 '15 at 09:50
  • @dotvav depending on the complexity of his project, quartz may be an overkill. He can first take a look at simpler solutions. – Swapnil Aug 25 '15 at 09:55
  • @Swapnil that comment went out as a pure reflex. Small projects/assignments may use simpler approaches. – dotvav Aug 25 '15 at 10:00
  • thank you for your help, i will try the solutions and than update – Gon Freecs Aug 25 '15 at 20:39

1 Answers1

5

You have two options:

QUARTZ CRONTRIGGER
Check Quartz Scheduler documentation. (Here you will find contrigger pattern doc)

executed at 00:00, 6:00, 12:00 and 18:00 hours:

0 0 0/6/12/18 * * ?

executed each 5 minutes

0 0/5 * * * ?   

TIMER
Define a thread that checks each x time to launch proces:

int SIX_HOURS = 1000 * 60 * 60 * 6;
Timer timer = new Timer(); 
timer.schedule( new TimerTask() 
{ 
    public void run() { 
    // do your work 
    } 
}, 0, SIX_HOURS);

DELAY QUEUE
I have no experience with DelayQueue, but you can find OldCurmudgeon example placed here, also Jenkov has a tutorial here, and you can find more examples here and here.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 2
    You can also use a [DelayQueue](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/DelayQueue.html). – OldCurmudgeon Aug 25 '15 at 10:03
  • @OldCurmudgeon updatted with more info, you know some other good tutorial or example of `DelayQueue`? – Jordi Castilla Aug 25 '15 at 10:09
  • 2
    Sorry for the delay - I had some trouble with it - see [here](http://stackoverflow.com/q/32206562/823393) for a sample `DelayQueue`. – OldCurmudgeon Aug 25 '15 at 14:22
  • Hi @Gon as this is your first question, if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Jordi Castilla Aug 26 '15 at 10:53