2

In EJB3 Timer Service, I can create the timer with the timerService.createTimer(...)function, but the question is where I can use it? As I know, I can't do it in Session Bean's lifecycle function, like @PostConstruct function.

I also read it can't be called in Stateful session bean? Is there anything else I should pay attention about createing timer?

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
cn1h
  • 1,188
  • 4
  • 16
  • 24

1 Answers1

2

I guess the most important thing to be aware of is that timers are transactional objects; it means, if you create a timer in a transaction that will roll back, the timer creation will be rolled back as well. This also implies that they must be called in a transaction context, and there is none in @PostConstruct.

Oh, and as you know, you need a TimerService to create a timer, so you usually will be using it in managed classes, so you can inject it.

MaDa
  • 10,511
  • 9
  • 46
  • 84
  • It's possible to call `timerService.createTimer(...)` in a `@PostConstruct` method of a `@Singleton` Session Bean if it implements `javax.ejb.TimedObject`. Is it bad to do so? – ltlBeBoy Apr 27 '23 at 20:39
  • Sadly, I lost touch with Java EE years ago and don't know how to answer this. – MaDa May 03 '23 at 21:00