Create local interface of the stateless session bean

Hi,

Can somebody let me know how to create a local interface of the stateless session bean. The following code is not working for me.

ProductAdminSessionLocal pas;

try{

final Context context = getInitialContext();

pas = (ProductAdminSessionLocal)context.lookup("ProductAdminSessionLocal");

}catch (NamingException ne){

thrownew WarningException(new CatalogHelper("ITOOLS_900023"));

}

private Context getInitialContext()throws UnknownException{

try{

returnnew InitialContext();

}catch (NamingException ne){

thrownew UnknownException (new CatalogHelper("ITOOLS_900024", ne.getMessage()));

}

}

Thanks in advance

-raju

[1570 byte] By [svgkrajua] at [2007-11-26 15:06:15]
# 1

EJBLocalHome home;

try {

home = (EJBLocalHome) getInitialContext().lookup(jndiHomeName);

log.info("Local jndiHomeName "+jndiHomeName+" look up successful");

} catch (Throwable e) {

throw new GeneralException(e);

}

Where "jndiHomeName" is EJB JNDI (e.g. mysite/ejb/local/MyEJB), look this up in application server JNDI tree after deployment.

Then use "home.create()" to get local interface.

sector_a at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

> Where "jndiHomeName" is EJB JNDI (e.g.

> mysite/ejb/local/MyEJB), look this up in application

> server JNDI tree after deployment.

This is not correct. Unfortunately there is a misconception that it is portable

to access ejb dependencies from the global namespace, mostly because

some application servers, e.g. JBoss, support it.

The portable way to access all ejb references, whether local or remote, is

through the private component namespace -- java:comp/env.In the

local case, you need to define an ejb-local-ref and look up its

ejb-ref-name via java:comp/env. An alternative for Java EE 5 is

to use the @EJB annotation and have the EJBLocalHome injected.

--ken

>

> Then use "home.create()" to get local interface.

ksaksa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hi,

Thank you for your replies. I am using OC4J (10.1.3.1) and EJB 3 in my application. I am able to do successful lookup for remote inteface mentioning context.lookup("ProductAdminSession")

. Apart from this it is not required to have home interace as per EJB 3.0. Is it that lookup is something different to create local interfaces?

regds

-raju

svgkrajua at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Hi Raju,

If the lookup is happening within a Java EE component, it is always preferable to use

the portable approach, which means accessing an ejb dependency via java:comp/env

or via @EJB injection.That is true regardless of whether local or remote interfaces

are being used.

--ken

ksaksa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Ken,

Thanks for you reply. I changed the code as follows:

ProductAdminSessionLocal pas;

try {

final Context context = getInitialContext();

pas = (ProductAdminSessionLocal)this.getInitialContext().lookup("java:comp/env/ProductAdminSessionLocal");

} catch (NamingException ne) {

throw new WarningException(new CatalogHelper("ITOOLS_900023"));

}

Actually I am creating the local interface in another stateless session bean that is running in the same java EE component. I am getting the same naming exception again. Is it required to maintain web.xml even if the client of the sateless session bean is another session bean within the same java EE component?

regds

-raju

svgkrajua at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Small mistake in my last postpas = (ProductAdminSessionLocal)context.lookup("java:comp/env/ProductAdminSessionLocal");regds-raju
svgkrajua at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

Hi Raju,

If the client of the stateless session bean is itself another session bean, then it is a

different component. It sounds like what you're saying is that both components

are part of the same application.In the case of EJB, the component environment

(java:comp/env) is private for each component. That means you need to define

the ejb-local-ref within the client component.

--ken

ksaksa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

Hi,

I got it. The following code worked for me.

@EJB(name="ProductAdminSessionLocal", beanInterface=ProductAdminSessionLocal.class)

@Stateless(name="AdminSession")

@Remote(AdminSession.class)

public class AdminSessionBean {

....

private Context getInitialContext() throws UnknownException {

try {

return new InitialContext();

} catch (NamingException ne) {

throw new UnknownException (new CatalogHelper("ITOOLS_900024", ne.getMessage()));

}

}

ProductAdminSessionLocal pas;

try {

final Context context = getInitialContext();

pas = (ProductAdminSessionLocal)context.lookup("java:comp/env/ProductAdminSessionLocal");

} catch (NamingException ne) {

ne.printStackTrace();

throw new WarningException(new CatalogHelper("ITOOLS_900023", ne.getMessage()));

}

....

}

Thanks for your inputs.

regds

-raju

svgkrajua at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Hi Raju,

Glad it's working for you.One other thing to note is that if you do need

to lookup the ejb reference instead of using injection, EJB 3.0 defines a

simpler way than using the JNDI API. There is now a lookup() method

on the EJBContext interface. When using it, you bypass "java:comp/env",

so it would look like this :

// inject the EJB SessionContext

@Resource SessionContext sesCtx;

...

ProductAdminSessionLocal pas = (ProductAdminSessionLocal)

sesCtx.lookup("ProductAdminSessionLocal");

There are a number of advantages to this over the old approach :

1. You don't need to know about the JNDI InitialContext API.

2. You don't need to know about java:comp/env

3. The EJBContext.lookup method does not throw a checked exception so

you don't need a try-catch block around the lookup.

--ken

ksaksa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10

Hello Ken

I have an enterprise application with a ejb module. It has two packages, one contains a SessionBean with Local and Remote Interface.

The second package is a utils package which contains a ServiceLocator class. I use this class to lookup the Remote and Local Interfaces of my session beans. The lookup code for remote interface works fine but not for local interface. I tried the approach you mentioned but still that still gives a NullPointer Exception. Here is my code:

@Resource SessionContext sesCtx;

public void getLocal() throws Exception

{

MySessionLocal lc = (MySessionLocal ) sesCtx.lookup("MySessionLocal");

System.out.println(lc.findAllPlayers.size());

}

Here is my Session Bean:

@Stateful(name="SessionBean")

public class ManagerSessionBean implements MySessionRemote , MySessionLocal {

...}

What am i doing something wrong?

Thanks

null

Message was edited by:

rabbia

rabbiaa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11
You'd simply use@EJB private MySessionLocal lc;instead of looking it up.
jwentinga at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 12

Java EE annotations are only supported on certain special container-managed classes

such as EJB bean classes, EJB interceptors, servlets, JSF managed beans, etc. If you

just have a plain utility class and define a Java EE annotation on it, the annotation will

be ignored.That's why you're getting the NPE.

Which other components are looking up the ManagerSessionBean?Are they EJBs

or web components?Only EJBs/web components in the same application as the

ManagerSessionBean can portably access its local EJB interface.

--ken

ksaksa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 13

Hello

Here is the hierarchy:

ServerApplication

|_SessionPackage

sessionpackage.ManagerSessionBean

sessionpackage.MySessionLocal

sessionpackage.MySessionRemote

|_UtilsPackage

utilspackage.ServiceLocator

A ServerApplication.jar contains all the class files of classes mentioned above and is deplyed at the jboss server.

If I use this approach within any other SessionBean within ServerApplication.jar:

@EJB private MySessionLocal lc;

It works fine. But I cannot apply this approach in ServiceLocator since it is a plain java class. Thats why i used the approach i mentioned in my previous post.

Now lets say i need another jar file which contains some other session beans say App2.jar. App2.jar will also be deployed at the same place as ServerApplication.jar. So if i need to access ManagerSessionBean in some session bean of App2.jar, isnt it a better approach to use its local interface instead of a lookup for its remote interface? Thats why i was creating a ServiceLocator so that i can lookup remote and local interfaces of session beans.

What do you suggest is a better approach?

Thanks

Message was edited by:

rabbia

rabbiaa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 14

If components in module2 need portable local ejb access to ejbs in module1, they must be

packaged in the same .ear. Some implementations support local access across application

boundaries but that it not required and is not supported in the Java EE SDK.

The package hierarchy and any existence of an extra layer of service locator code is largely

irrelevant.All the matters is what component context is active at the time the calling code

is retrieving the ejb dependency.If all the client components are themselves EJBs, it's

easiest to just use @EJB in the bean class.If you want to do lookups of the ejb references

from other non-container managed classes, you'll need to define the ejb dependency for

each component in which the lookup can take place.

Here's an entry from our EJB FAQ on this topic :

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB

ksaksa at 2007-7-8 8:56:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...