6/17/2006

4 Ways to Get EJBContext in EJB 3

1. Use field injection in bean class. Those fields can have any access qualifiers (e.g., private, public, protected, package default).

package com.foo.ejb;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

@Stateless
public class HelloBean implements HelloRemote {
@Resource
private SessionContext sctx;

public void hello() {
System.out.println("SessionContext from field injection: " + sctx);
}
2. Use setter method injection in bean class. You can use either method or field injection for a particular field or property, but not both. Those methods can have any access qualifiers (e.g., private, public, protected, package default).
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
private SessionContext sctx;

@Resource
private void setSessionContext(SessionContext sctx) {
this.sctx = sctx;
}
}
Or,
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
private SessionContext sctx;

@Resource
private void setSctx(SessionContext sctx) {
this.sctx = sctx;
}
}
3. Look up the injected resource based on the name() (or default name, if not specified) field of @Resource
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
@Resource(name="sessionContext")
private SessionContext sctx;

public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/env/sessionContext");
System.out.println("look up injected sctx: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
Or using default name if the name field of @Resource is not specified. Note that the default name of an injected resource is: fully-qalified-class-name/variable-name:
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
@Resource
private SessionContext sctx;

public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/env/com.foo.ejb.HelloBean/sctx");
System.out.println("look up injected sctx by default name: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
4. Look up by the standard name java:comp/EJBContext (note that there is no /env)
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/EJBContext");
System.out.println("look up EJBContext by standard name: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
The above examples use Stateless Session beans, and they also work with Stateful Session beans, Singleton Session beans (introduced in EJB 3.1), and message driven beans. EJBContext are a special kind of resource so you don't need to configure them in ejb-jar.xml, or any vendor-specific configuration files (sun-ejb-jar.xml, jboss.xml, etc). You can choose to also declare references to EJBContext/SessionContext/MessageDrivenContext in descriptors, but you are more likely to get javax.naming.NameNotFoundException.

These techniques are well defined in EJB 3 spec and should work in all JavaEE 5 compilant application servers. I tested above examples on JavaEE SDK 5/SJSAS 9/Glassfish.

10 comments:

Anonymous said...

Thanks for the post I have been looking for the solution for my problem for about 2.5 hours and this post solved my problem.

Bence

Benoit de CHATEAUVIEUX said...

Thanks a lot.
I was looking for the 4th point.
It is a standard way of obtaining the EJBContext (I found it in the EJB 3.0 specs too).

Imam said...

Thanks alot, very very helpfull

Anonymous said...

excellent demonstration. Got through idea from it.

dracof said...

Excellent !!!!!!

Anonymous said...

Very helpful post! Thanks a a lot

cialis effects said...

Thanks mate... just dropped by. Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.

Anonymous said...

very very useful! Thank you

Figurine Sportive said...

Very interesting, even though I remain with original question unanswered: should it work in any of these approaches? If not, which could be the reason?

Regards,
Pierluigi

Anonymous said...

Thanks a lot for this post.

-- Sachin Patil