st.
st.

Reputation: 188

EJB Timer usage recommendations

I want to schedule a job, which read some entities, call some ws and write/update some entities. This job should be processed after the moon, for example at 01am. I have 2 problems about this issue. First is that Scheduler takes constant parameters, my requeirement is that Scheduler should be adjustable via some ui. Is there a suitable way to do this? Otherwise I have to adjust the scheduler for example every 30 min, and inside the method I have to look some variable time values whether they are met or not. The second problem is inside the Scheduler method usertransaction is started, and when I call the method, which calls the utx.start, I get "thread is already associated with a transaction!" exp. What shall I do? Recommendation pls. Thanks in advance.

Upvotes: 1

Views: 2345

Answers (2)

Brett Kail
Brett Kail

Reputation: 33936

If you want to create variable schedule expressions, then you'll need to obtain TimerService (e.g., @Resource Timerservice _timerService), and then use the createCalendarTimer method, which will invoke your @Timeout method. A timer's schedule is immutable, so the UI will need to use getTimers() to find/cancel the existing timer (perhaps by matching getInfo()) before creating a new one.

@Timeout methods inherit the transaction capabilities of the containing bean. If the bean is using container-managed transactions, then UserTransaction will not work.

Upvotes: 1

Mr.Eddart
Mr.Eddart

Reputation: 10273

You could expose a Managed Bean to manage this timer through JMX console.

For your second problem, it seems that you configured the transactions to be managed by the container. Then, when the method launches, a transaction is automatically started. You can disable this automatic management, or yet better, avoid programmatic management and let the container manage transactions.

Upvotes: 0

Related Questions