How to easy setup a simultaneous scheduled task? Cron Java

288 views Asked by At

I'm having troubles to run simultaneous two tasks.

I have an interface:

import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;

public interface ScheduledTask extends Runnable {
  public void schedule(final TaskScheduler taskScheduler, final Trigger triggerInterval);
}

And two similar classes, that I will call MyTaskOne and MyTaskTwo, both like:

@Component
@ApplicationScoped
public class MyTaskOne implements ScheduledTask  {
  public TaskOne(final TaskScheduler taskScheduler) {
    this.scheduler(agendadorDeTarefas, new CronTrigger("0 */31 * * * ?"));
    }
}

@Override
public void run() {
      //Bla bla bla
}

@Override
public void scheduler(final TaskScheduler taskscheduler, final Trigger triggerInterval) {
    taskscheduler.schedule(this, triggerInterval);
}

The difference between MyTaskOne and MyTaskTwo, besides the content of run(), is the cron interval, MyTaskOne is ("0 */31 * * * ?") and MyTaskTwo is ("0 */37 * * * ?").

What is happing is MyTaskOne initialize first, and MyTaskTwo only after MyTaskOne is finished, and I would like to run both simultaneous, how can I easy setup this?

The server has apache-tomcat 6.

Any help is appreciate, and sorry for any grammar mistakes!

1

There are 1 answers

0
Jiraya On

Resolved using Threads.

Implemented my Task as ScheduledTask and Runnable,

Using threads with semaphore to control worked very well.