Schedule job to run on the Wildfly server

Asked

Viewed 166 times

2

I need to create a task runner where it will call a method from time to time.

I saw an example and it was like this my implementation:

public class Agendador {
  // Scheduler
  private final ScheduledExecutorService s = Executors.newScheduledThreadPool(1);

  final Runnable beeper = new Runnable() {
    @Override
    public void run() {
      EncomendaController controller = new EncomendaController();
      controller.pesquisar();
    }
  };
  // Beeper Handler
  final ScheduledFuture<?> b = s.scheduleAtFixedRate(beeper, 0, 1, TimeUnit.MINUTES);
}

How can I make Wildfly execute the scheduling and execution of this task?

1 answer

3


Example for the method funcaoAgendada() be invoked every second:

@Singleton
public class Agendador {
    @Schedule(second = "*/1", minute = "*", hour = "*", persistent = false)
    public void funcaoAgendada() {
        System.out.println("Função Agendada em execução!");
    }
}

Your scheduler may also have scope @Stateless or @Stateful depends on the need.

  • How different from them?

  • Stateless does not maintain the state of the object, Singleton as stateless with only 1 instance, Stateful retains the whole state of the object. I recommend reading about Design Patterns (Design Patterns) and EJB Session Status as the question is complex.

  • I get it, he’s calling the method but he’s giving nullpointer on the method I don’t know pq :(

  • Just by mistake it becomes difficult to know, anything opens a new question...

  • 1

    I’ve seen what it was, thank you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.