1

I am working on a JSF project using stateless ejbs. I have some functions in some of the stateless beans that I need do execute periodically (scheduling). I don't want to write an external ejb to do the scheduling. I wonder if it's good to write another ejb stateless bean as scheduler in the same web project that make call to those functions in the other stateless beans and then deploy them together on my glassfish server.

cdaiga
  • 4,318
  • 3
  • 17
  • 34

2 Answers2

2

I wonder if it's good to write another ejb stateless bean as scheduler in the same web project that make call to those functions in the other stateless beans and then deploy them together on my glassfish server.

This should be no problem.

@Schedule is the way to go. Here is an example:

@Singleton
public class Task {

    @EJB
    private SomeOtherEJB otherEJB;

    @Schedule(hour = "*/1")
    public void doSomething() {

        otherEJB.doSomething();
    }
}

This runs the method once every hour on every day of the week.

The use of @Singleton is recommended but you can also use @Stateless.

unwichtich
  • 13,143
  • 2
  • 46
  • 60
0

You could use a java.util.Timer in a @Startup @Singleton

Justin Tamblyn
  • 652
  • 8
  • 18
  • Thank you for your answer, I already know how to make a scheduler using stateless ejbs. I just want to know if my idea is good! – cdaiga Jan 15 '16 at 11:45
  • No! Terribly bad advice. http://stackoverflow.com/questions/4691132/how-to-run-a-background-task-in-a-servlet-based-web-application – BalusC Jan 15 '16 at 11:59