How to lookup a Stateless Session bean in a Managed Bean

Since I always forgot how to do and there are several ways to lookup a Stateless Session bean, I'm writing a blog entry here:

Fortunately, the newest containers available today such as Glassfish 3.1.x and JBoss 7.x, list all the available services on Startup, just look at the log.

Here are the different ways:
1.) JBoss
public static Object getLocalInterface(String nameEJB) {
 InitialContext ctx;
 try {
  ctx = (InitialContext) getInitialContext();
  return ctx.lookup(nameEJB);
 } catch (NamingException ex) {
  ex.printStackTrace();
 }
 return null;
}

public MyBeanLocal getService() {
 return ((MyBeanLocal)Utils.getLocalInterface("workflow/MyBeanBean/local"));
}

2.) Glassfish
a.) Long name:
InitialContext ic = new InitialContext();
IMyService myService = (IMyService) ic
      .lookup("java:global/myProject-ear-1.0.0/myModule/MyService!com.czetsuya.service.MyService");
b.) App access
InitialContext ic = new InitialContext();
IMyService promoService = (IMyService) ic
      .lookup("java:app/myProject-ear/myModule/MyService");
How to lookup a Stateless Session bean in a Managed Bean How to lookup a Stateless Session bean in a Managed Bean Reviewed by Edward Legaspi on Monday, December 24, 2012 Rating: 5

No comments:

Powered by Blogger.