Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Hayrah Burki   on Jun 07 In Java Category.

  
Question Answered By: Zachary Larson   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

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

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


Tagged: