EJB Interceptors can be added much the same way at two different levels:
- At EJB Container level
- At Application level
If you want to learn more Application level EJB interceptors you can have a look at this tutorial.
Here we will show how to apply EJB Interceptors at EJB Container level, which could be used for monitoring some aspects which are not peculiar with your applications, but can be reused in different contexts. You can configure EJB Container interceptors in different ways depending on the release of JBoss AS you are using
JBoss AS 7.2
EJB Container interceptors are available since the release 7.2.0 of the application server. You can configure them, by adding the Interceptor (or the chain of interceptors) in your jboss-ejb3.xml file which is JBoss AS 7 custom deployment descriptor for your EJB applications. Let's see one example:
This is a default Interceptor, which will be used for all your EJBs. And here's the Interceptor class, which simply includes the javax.interceptor.AroundInvoke annotation in one of its methods:
When executed, this Interceptor will display the EJB being called and its method:
15:02:13,198 INFO [com.mastertheboss.interceptor.GenericContainerInterceptor] (http-/127.0.0.1:8080-1) Going to call public void com.mastertheboss.ejb.ServiceBean.put(com.mastertheboss.model.SimpleProperty)
If you want to intercept just one EJB, you can specify the EJB name in the file jboss-ejb3.xml
Finally, you can intercept just a single method of the EJB, like in the following example: <
JBoss AS 5 - 6
The great benefit of interceptors is that they are a seamless way to add Aspect Oriented Programming to your business methods.
EJB 3 Interceptors are defined in the file server/<your server>/deploy/ejb3-interceptors.xml.
Here's the section we want to operate on:
#2 Update jboss-ejb3-core.jar
Compile the interceptor and add the class to the jboss-ejb3-core.jar
That's all! restart the server and verify that your SLSB now debugs information about the current/max size of instances.