简体   繁体   中英

TimeDelay in EJB2

One of the EJB class is calling an external API & I would like retry if there is a failure after some time delay. As managing thread is not a good idea (Thread.sleep(1000)) is not recommended, can someone tell me how to delay/suspend the process for a certain period of time in EJB2?. Thanks.

I assume your issue with sleeping for 1 second multiple times is the unnecessary waiting time if the thread is successful.

Instead of sleep, you could try using join instead.

join(5000)

would wait up to 5 seconds for the thread to finish. If it finishes early, your execution resumes. If it doesn't finish early, your execution resumes after 5 seconds.

The sleep method uses a polling technique whereas join uses a modified interrupt scheme.

As a workaround, if there is failure, then you can create a timer with appropriate time-out duration, which in this case will be delay duration.

In time-out method, you can again call EJB which will further call external API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM