| Monitoring JBoss's EJB Container |
|
|
|
| Written by F.Marchioni | |||||
| Thursday, 11 February 2010 14:22 | |||||
|
In this short article we will show how easily you can write an Interceptor class which is added in the chain of Stateless interceptors. The purpose of this Interceptor is adding debugging information to your Stateless Bean Pool. JBoss 5 EJB container is based on the concept of interceptors. These are simple classed based on the Proxy pattern, which allow to execute all the required steps before method invocation (security checks, instance creation, transaction propagation etc.). 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:
<domain name="Stateless Bean" extends="Intercepted Bean" inheritBindings="true">
. . . .
<bind pointcut="execution(public * *->*(..))">
<interceptor-ref name="org.jboss.aspects.tx.TxPropagationInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.tx.CMTTxInterceptorFactory"/>
<interceptor-ref name="org.jboss.ejb3.stateless.StatelessInstanceInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.tx.BMTTxInterceptorFactory"/>
<interceptor-ref name="org.jboss.ejb3.AllowedOperationsInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor"/>
<!-- interceptor-ref name="org.jboss.ejb3.interceptor.EJB3InterceptorsFactory"/ -->
<stack-ref name="EJBInterceptors"/>
</bind>
<annotation expr="!class(@org.jboss.ejb3.annotation.Pool)">
@org.jboss.ejb3.annotation.Pool (value="ThreadlocalPool", maxSize=30, timeout=10000)
</annotation>
</domain>
Suppose you want to add an extra interceptor which controls the status of your Stateless Bean Session Pool. You could for example add a warning message/email if your Pool is exausted. You can perform even EJBContainer activities like programmatically increase the size of the pool, or reject the request if you don't want to create new threads in your application. Here's a simple Interceptor implementation which debugs information about the SLSB Pool size:
package com.sample;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.ejb3.EJBContainer;
import org.jboss.ejb3.aop.AbstractInterceptor;
import org.jboss.ejb3.pool.Pool;
import org.jboss.logging.Logger;
public class PoolInterceptor extends AbstractInterceptor
{
public PoolInterceptor()
{ }
public String getName()
{
return "PoolInterceptor";
}
public Object invoke(Invocation invocation)
throws Throwable
{
EJBContainer container=getEJBContainer(invocation);
Pool pool = container.getPool();
int size = pool.getCurrentSize();
int maxSize = pool.getMaxSize();
log.debug("Size is "+size);
log.debug("MaxSize is "+maxSize);
if (size == maxSize) {
// PANIC !!! WARN Administrator
}
Object obj;
try
{
obj = invocation.invokeNext();
}
catch(Exception ex)
{
throw ex;
}
return obj;
}
private static final Logger log = Logger.getLogger(com.sample.PoolInterceptor.class);
}
While this example is even too trivial, you can have an idea of the real power of Interceptors. Having access to the EJBContainer object, you can easily track the Pool Object which contains all the necessary information about the pool.So far nothing new: you can perform a similar hack connecting to the MBean containing the deployed EJB (jboss.j2ee domain). The huge power of Interceptors however relies on the ability to even modify the EJBContainer behaviour, creating new SLSB instances or even discard existing instances. For example, you can have a look at the org.jboss.ejb3.stateless.StatelessInstanceInterceptor Class, which contains the logic for creating/discarding a new SLSB instance. (The class is container into JBOSS_HOME/common/lib/jboss-ejb3-core.jar ) Now, in order to test your Interceptor you have to update the jboss-ejb3-core.jar jar with your compiled class and add a reference to the interceptor in the ejb3-nterceptors.xml. #1 Update ejb3-interceptors.xmlYou need to declare the Interceptor at the beginning of the file and then reference it in the StatelessSessionBean domain:
<!--------- OUR INTERCEPTOR ---------->
<interceptor class="com.sample.PoolInterceptor" scope="PER_VM"/>
........
<domain name="Stateless Bean" extends="Intercepted Bean" inheritBindings="true">
<interceptor-ref name="org.jboss.aspects.tx.TxPropagationInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.tx.CMTTxInterceptorFactory"/>
<interceptor-ref name="org.jboss.ejb3.stateless.StatelessInstanceInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.tx.BMTTxInterceptorFactory"/>
<interceptor-ref name="org.jboss.ejb3.AllowedOperationsInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor"/>
<!--------- OUR INTERCEPTOR ---------->
<interceptor-ref name="com.sample.PoolInterceptor"/>
<stack-ref name="EJBInterceptors"/>
</bind>
<annotation expr="!class(@org.jboss.ejb3.annotation.Pool)">
@org.jboss.ejb3.annotation.Pool (value="StrictMaxPool", maxSize=1000, timeout=100000)
</annotation>
</domain>
#2 Update jboss-ejb3-core.jarCompile 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.
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated on Friday, 12 February 2010 09:09 |






