Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

RMI and JNDI ejb3 problem

  Asked By: Hayrah    Date: Jun 07    Category: Java    Views: 1611
  


import javax.annotation.*;
import javax.ejb.*;


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

public class CallBean {

@Resource
SessionContext sessionContext;
public CallBean() {
Welcome w = (Welcome) sessionContext.lookup("WelcomeBean/remote");
System.out.println(w.getUserName());

}
public static void main(String[] s) {
new CallBean();
}
}

Exception in thread "main" java.lang.NullPointerException
at CallBean.<init>(CallBean.java:12)
at CallBean.main(CallBean.java:17)


Share: 

 

3 Answers Found

 
Answer #1    Answered By: Zachary Larson     Answered On: Jun 07

I guess you've added only java  EE api to your project expecting a miracle comes and implements it and then you run client without any problem  ;)

There are different ways in different javaee application server to lookup and bind EJBs remotely from an stand-alone client application, and its better to use their own jndi  context factory, I've found most of them in this url: http://www.caucho.com/resin-2.1/ref/ejb.xtp

But there is a common way to connect all of them, (the way to connect to glassfish or Sun AS). You can find the required classes in appserv-rt.jar and javaee.jar in glassfish libraries.

Client:


package testapp;

import java.util.Properties;
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import yoohoo.YoohooSessionRemote;

public class  Main {

public static  YourFacadeRemote lookupYourFacade() {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost",
"localhost"); // server name
props.setProperty("org.omg.CORBA.ORBInitialPort",
"3700"); // ORB port for glassfish is 3700 by default and for JBoss I
// guess it's 1099

InitialContext c = new InitialContext(props);
return (YourFacadeRemote) c.lookup("YourFacade"); // Here is the most
// error prone part. Just pass the exact name of your EJB's JNDI name
// DO NOT use "java:comp/env/ejb/YourFacade",
// "java:comp/env/YourFacade" or something like that.
}

 public  static void  main(String[] args) {
YourFacadeRemote remote = lookupYourFacade();
System.out.println(remote.sayHello());
}

}

EJB Module:

ejb-jar.xml

<enterprise-beans>
<ejb>
<ejb-name>YourFacadeBean</ejb-name>
<jndi-name>YourFacade</jndi-name>
</ejb>
</enterprise-beans>


@Remote
public interface YourFacadeRemote {

String sayHello();

}

@Local
public interface YourFacadeLocal {

String sayHello();


}

@Stateless
public class YourFacadeBean implements YourFacadeRemote, YourFacadeLocal {

public String sayHello() {
return "Hello World!";
}

}

Let me know if you get in trouble about it again

 
Answer #2    Answered By: Russell Burns     Answered On: Jun 07

your instruction was great but i think some thing is misunderstood because that code i've just represented it is just part of my application.
one of the most tedious parts of EJB2.x development was writing the same few lines of boilerplate code many times to do a JNDI lookup whenever you needed to access an ejb  or a container managed resource such as a pooled database connection handle. I know in EJB3.0, JNDI lookups have been turned into simple configuration. Using metadata.
In your example if you want to access the YourFacadeBean EJB from another EJB or Servlet you can choose another approach or better say the new method
........
@EJB private YourFacadeBean yourFacadeBean;
private void  callBean() {
System.out..println(yourUserFacade.sayHello());
}
........
Did you see this is a miracle.
My problem  is here, my main  question is how can i implement this approach?
for example in JBoss when you want to call YourFfacadeBean
you have to follow this way (In ejb3  i think it's not recommended as i explained about it on above side).


import javax.naming.*;
import java.util.Properties;

public class  CallBean {
 public  static void main(String[] myString) {
Properties prop = new Properties();
prop.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
prop.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
prop.setProperty("java.naming.provider.url","localhost:1099");

Context ctx = new InitialContext(prop);
YourFfacadeBean yourFfacadeBean = (YourFfacadeBean)ctx.lookup("YourFfacadeBean/remote");
System.out.println(yourFfacadeBean.sayHello());
}
}

you see it's boring.
of course in ejb3 we have a better solution for solving this disaster;

 
Answer #3    Answered By: Louis Mason     Answered On: Jun 07

Just take a look at my reply again, you will find "stand-alone client application" phrase, I think even a beginner to EJB3 knows that you can have a reference of a local EJB in another local module using annotations;)
Noticing the exception  you've written, I imagined by myself you were going to call a Session Bean from a stand-alone client application. Not to mention anything about application-client.xml, and not to set any JNDI properties made me predict you were waiting for a miracle, ;) and I thought you were just trying to connecting to an SB in a bloody and disaster approach, that's why I suggested you the simplest and fastest way. however I'm not sure looking up an enterprise resource is a disaster but the best way for a simple stand-alone client. ;)
But to develop a Java EE Application Client, (the way you are looking for) I've found Sun Java EE Tutorial damn great, refer to CREATING THE APPLICATION CLIENT. if you are going to handle it. you must run your client with a batch file called appclient.bat (.sh) in glassfish. To know how to make your client free of app server, analyze appclient.bat.

I myself prefer to have a JAXWS webservice tier between, that makes everything simple, I guess it's better to let Java EE app servers talk to each other directly not a client to a server.

 
Didn't find what you were looking for? Find more on RMI and JNDI ejb3 problem Or get search suggestion and latest updates.




Tagged: