How to create a MDB 3.0 singleton ?

 

EJB3 uses the resource adapter.  When using the resource adapter, you need to constrain the number of Sessions:  so you set the minimum for the sessions.  This will essentially give you a singleton. 

You can set the maxSession in your bean with an annotation by adding :

@ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")

If you are running JBoss 4/5/6 you can configure this at EJB container level by changing the server/$/deploy/ejb3-interceptors-aop.xml file 

 

 

      <annotation expr="!class(@org.jboss.annotation.ejb.DefaultActivationSpecs)">
         @org.jboss.annotation.ejb.DefaultActivationSpecs (value={@~ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")})
      </annotation>

This will set the default for all message driven beans.

Will it work in a cluster too ?

No , if you want a clustered singleton MDB you need to deploy your mdb in

deploy-hasingleton

or create your own clustered singleton deployment.

Related articles available on mastertheboss.com

EJB 3.0 tutorial : Session Beans

Enterprise JavaBeans (EJB) technology is a J2EE technology for de

JBoss MDB 3.0

Message-driven beans (MDBs) are stateless, server-side, transacti

JBoss EJB 3 BMT

In a Bean Managed Transaction, the code in the session or message

How do you keep your EJB callbacks separated from biz. methods?

JBoss daily recipe

How to set EJB timeout period ?

JBoss recipe of the day

How to add a Selector with MDB 3.0 ?

Message selectors allow an MDB to be more selective about the mes

Follow us on Twitter