Search code examples
ejbscheduledexecutorservice

Should I use ejb @Schedule annotation over schedule executor


I'm writing a service that will call REST service every x seconds. I could do that with EJB @Schedule annotation as well as with ScheduledExecutorService.

which one is better to use? and why?


Solution

  • If you're writing an EJB application, you should use Schedule - the EJB spec demands that EJBs should not use threads directly, and says that doing so may cause errors in the container, as well as preventing the container from managing resources efficiently. In exchange, with Schedule you get the benefits of container management and EJB services.