import jakarta.ejb.Stateless;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.InvocationContext;
@Stateless
public class MyStatelessBean implements MyBusinessInterface {
@PostConstruct
public void constructed(){
}
@PreDestroy
public void destroy(){
}
@AroundInvoke
public Object invoke(InvocationContext invocationContext) throws Exception {
return invocationContext.proceed();
}
}
Callbacks
Correct usage of PostConstruct, PreDestroy, PrePassivate, PostActivate, and AroundInvoke for EJBs and Interceptors.
For Stateful, Stateless, and MessageDriven, the syntax is as follows:
-
@PostConstruct <any-scope> void <method-name>()
-
@PreDestroy <any-scope> void <method-name>()
-
@PrePassivate <any-scope> void <method-name>()
-
@PostActivate <any-scope> void <method-name>()
For an Interceptor, the syntax includes InvocationContext as follows:
-
@PostConstruct <any-scope> void <method-name>(InvocationContext)
-
@PreDestroy <any-scope> void <method-name>(InvocationContext)
-
@PrePassivate <any-scope> void <method-name>(InvocationContext)
-
@PostActivate <any-scope> void <method-name>(InvocationContext)
The AroundInvoke syntax for an EJB or Interceptor is the same:
-
@AroundInvoke <any-scope> Object <method-name>(InvocationContext) throws Exception
Stateless
Stateful
import jakarta.ejb.Stateful;
import jakarta.ejb.PostActivate;
import jakarta.ejb.PrePassivate;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.InvocationContext;
@Stateful
public class MyStatefulBean implements MyBusinessInterface {
@PostConstruct
public void constructed(){
}
@PreDestroy
public void destroy(){
}
@AroundInvoke
public Object invoke(InvocationContext invocationContext) throws Exception {
return invocationContext.proceed();
}
@PostActivate
public void activated(){
}
@PrePassivate
public void passivate(){
}
}
MessageDriven
import jakarta.ejb.MessageDriven;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.InvocationContext;
@MessageDriven
public class MyMessageDrivenBean implements MyListenerInterface {
@PostConstruct
public void constructed(){
}
@PreDestroy
public void destroy(){
}
@AroundInvoke
public Object invoke(InvocationContext invocationContext) throws Exception {
return invocationContext.proceed();
}
}
Interceptor
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.interceptor.InvocationContext;
import jakarta.interceptor.AroundInvoke;
import jakarta.ejb.PostActivate;
import jakarta.ejb.PrePassivate;
public class MyInterceptor {
@PostConstruct
public void constructed(InvocationContext invocationContext){
}
@PreDestroy
public void destroy(InvocationContext invocationContext){
}
@AroundInvoke
public Object invoke(InvocationContext invocationContext) throws Exception {
return invocationContext.proceed();
}
@PostActivate
public void activated(InvocationContext invocationContext){
}
@PrePassivate
public void passivate(InvocationContext invocationContext){
}
}