Dynamically change Session/Message Driven Bean parameters

When an EJB3 Session Bean or a Message Driven Bean is created, it is assigned some basic parameters such as Max Pool Size, Timeout, Max Session Size etc. Such parameters are pre-configured with default values in ‘ejb3-interceptors-aop.xml‘ file available in $JBOSS_HOME/server/$Profile/deploy folder.

As you will open the file, you will find that the parameters are available for Session Bean are as following,

StatelessBean

and for Message Driven Bean as following,

MDB

as you see, both the beans have Pool annotation configuration is given. If you want to change the maxSize, timeout or pool type to ThreadLocalPool or StrictMaxPool, you can change the configuration easily in this file. This will change the parameters of all the beans.

******** But there’s a problem!!! *******

This configuration is applied to every single bean deployed in your EJB container. Consider, you have 10000 session beans deployed in the container and  one of your session beans requires to have a very big pool maxSize as 1000 due to heavy request to it. The immediate thing you would do is to change the maxSize to 1000 in above file. Wait… wait… what are you doing??? There are 10000 beans and 1000 maxSize per bean… so it totals to 10000000 threads!!! I doubt that your server will survive.

Well, to a solution to this problem, add the @Pool annotation in the required session bean itself only and set the maxSize to 1000 and keep the default configuration unchanged in ‘ejb3-interceptors-aop.xml‘.

simple_sb

By setting the maxSize in this Session Bean, it will override the default configuration of ‘ejb3-interceptors-aop.xml‘ and assign the new maxSize to this session bean only.

******** But wait…. again there’s a problem!!! *******

Consider you have released this well set session bean on production environment and you found out that you are required to change the maxSize to 5000 instead of 1000 due to heavy load on this session bean. Oh man.. what I’ve done… you can’t do that without changing the code and redeploying this session bean on the production environment. So, what to do???

Well, don’t be alarmed. There’s a solution to this problem. The solution is, you neither set the required maxSize in Session Bean nor change it in the ‘ejb3-interceptors-aop.xml‘. Ok, but then where and how?

Here’s how we do it:

Step-1: Create a file named ‘my-aop.xml‘ file in $JBOSS_HOME/server/$Profile/deploy folder.

Step-2: Add following domain in this file,

sb_aop

with setting maxSize to 5000 in this file.

Step-3: Add annotation @AspectDomain with the same name you have provided in above xml file. And remove the @Pool annotation from the bean.

sb_aop1

That’s it!!! Now, you can change any number in the maxSize in ‘my-aop.xml‘ placed in deploy folder based on your requirement . You won’t have to give a new release of code every time you want to change the pool size. Thanks God (of JBoss)!!!

******** Aahhaan… a big relief !!! *******

Some after-notes on this,

  1. You can have any prefix of name for the aop.xml file, ie. ***-aop.xml.
  2. One limitation to this, is we will have to restart the JBoss Server to take the changes in effect. However, we will have to restart the JBoss Server anyway even if we use first two methods. So, we’re even!!!
  3. Not only the @Pool annotation, but also we can configure any annotation provided by EJB specification with this aop.xml file aspect domain approach.

Further, to demonstrate other annotations, lets have example of Message Driven Bean Configurations.

A simple MDB may look like this,

mdb_aop

To enable this bean with aspect domain, remove the @Pool and @ActivationConfigProperty annotations from the MDB and add the @AspectDomain annotation as following,

mdb_aop_class

And add the domain entry with the annotations for this MDB as following in ‘my-aop.xml‘ which is already there in your deploy folder.

aop_xml_mdb_sb

You can put as many <domain> entries as you can in one file, or you can create separate ***-aop.xml for different beans. Choice is yours.

Life was never been so easy!!!!