How to configure EJB Client side Interceptors in EAP 7.1+
Environment
Red Hat Enterprise Application Platform (EAP) 7.1+
Issue
- How to configure EJB Client side Interceptors in EAP 7.1 and later?
Resolution
EJB Client Side Interceptor
Create the Client Side Interceptor as shown below:
package examples;
import org.jboss.ejb.client.EJBClientInterceptor;
import org.jboss.ejb.client.EJBClientInvocationContext;
public class HelloClientInterceptor implements EJBClientInterceptor {
@Override
public void handleInvocation(EJBClientInvocationContext context) throws Exception {
// Must make this call
context.sendRequest();
}
@Override
public Object handleInvocationResult(EJBClientInvocationContext context) throws Exception {
try {
return context.getResult();
} finally {
// at this point all client interceptors have been invoked
}
}
}
Enabling an EJB Client Side Interceptor
Using annotations on the EJB's Remote Interface
Specify the Client Interceptor by adding the annotation shown below on the EJB's Interface:
import org.jboss.ejb.client.annotation.ClientInterceptors;
@ClientInterceptors({HelloClientInterceptor.class})
public interface HelloBeanRemote {
public String hello();
}
Inserting a Client Interceptor Programmatically 1
EJBClientContext ctxWithInterceptors = EJBClientContext.getCurrent().withAddedInterceptors(clientInterceptor);
-
You can run the following code with
EJBClientContextapplied using a Callable operation. EJB calls performed within the Callable operation will apply the client-side interceptors:ctxWithInterceptors.runCallable(() -> { // perform the calls which should use the interceptor }) -
Alternatively you can mark the newly created
EJBClientContextas the new default:EJBClientContext.getContextManager().setThreadDefault(ctxWithInterceptors);
Using the service loader mechanism2
Packing a org.jboss.ejb.client.EJBClientInterceptor file in the META-INF/services/ directory. In the scope of the example of this article, the content of the file should be:
examples.HelloClientInterceptor
Using wildfly-config.xml (If Client is a Standalone Java App)
Packing a wildfly-config.xml in the META-INF directory
<?xml version="1.0" ?>
<configuration>
<jboss-ejb-client xmlns="urn:jboss:wildfly-client-ejb:3.0">
<global-interceptors>
<interceptor class="examples.HelloClientInterceptor"/>
</global-interceptors>
<connections/>
</jboss-ejb-client>
</configuration>
Related Solutions
What is the lifecycle of EJB interceptors and is there a need to make it thread safe in EAP
===
7.1.8.1. Inserting a Client Interceptor Programmatically
2: 7.1.8.2. Inserting a Client Interceptor Using the Service Loader Mechanism
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.