What is the lifecycle of EJB interceptors and is there a need to make it thread safe in EAP

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x
    • 7.x

Issue

  • What is the behaviour of EJB interceptors in EAP6 if the invocations are concurrent?
  • Do I need to make the EJB interceptors thread safe?
  • Can I use client side EJB interceptors in a multi-thread client and does they have to be thread safe?

Resolution

Server side interceptors

Server side interceptors are defined by the ejb3 specification. These interceptors are bound to the related instance and will have the same life cycle. (see EJB 3.2 core specification JSR345 - chapter 7. Interceptors)

This will have the following effects

SLSB Stateless SessionBeans

no problem as parallel invocations are done by other instances which have other interceptor instances.

SFSB Statefull Session Beans

Conncurent invocations are not allowed by spec, the interceptor is not touched as the invocation is rejected.
In case of passivation the intceptor is passivated also (see EJB 3.2 core spec JSR345 - chapter 7.2 Interceptor life cycle)

Singleton Bean

Such bean is controlled by Locking,
If Locked the interceptor is not called, this is the default behaviour.
If not Locked the concurency is the same as for the Singleton and the interceptor need to be thread safe as well

Client side interceptors

The client side interceptor is a JBoss specific implementation and not covered by the JavaEE specificatiton.
Such interceptor is bound to the EJBClientContext. Each context will declare the interceptor and instantiate it.
The instance is used for all EJB invocations.
This means for Multithreaded clients which use the same context, this is the case if you use jboss-ejb-client.properties or the API and set a EJBClientContext.setSelector(),
you will hit the same instance and the interceptor need to be thread safe!

EJB client interceptors in JBoss EAP 6
How to pass extra parameters when EJB client invokes EJB in JBoss EAP 6

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.