How to create an EJB Remote Client with Eclipse OEPE on Weblogic ?
To understand how an EJB is called through lookup, you must first read this article: Java - Java Naming and Directory Interface (JNDI).
In this example, I create a simple EJB with a method which returns and Hello World string.
Create wlfullclient.jar by running in WLS_HOME/server/lib, the following command
java -jar wljarbuilder.jar
and add it to the classpath.
wlfullclient.jar is not dependent on external jars, and can be copied to client machines.
Add the EJB project to the client classpath:
package ejbclient;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import nl.hotitem.ejb.HotStarsEjbRemote;
public class EjbClient {
public static void main(String[] args) {
Context ctx = null;
Hashtable<String, String> ht = new Hashtable<String, String>();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://192.168.2.40:7001");
ht.put(Context.SECURITY_PRINCIPAL, "weblogic");
ht.put(Context.SECURITY_CREDENTIALS, "welcome1");
try {
ctx = new InitialContext(ht);
System.out.println("Initial Context created");
// We are requesting the remote bean from the JNDI context
HotStarsEjbRemote bean = (HotStarsEjbRemote) ctx
.lookup("ejb.DataInsight-HotStarsEjb#nl.hotitem.ejb.HotStarsEjbRemote");
System.out.println("lookup successful");
String MyHello = bean.sayHello("Nicolas");
System.out.println("EJB method called and say: " + MyHello);
} catch (NamingException e) {
// a failure occurred
System.out.println("A Naming failure occurred" + e);
} finally {
try {
ctx.close();
} catch (Exception e) {
System.out.println("A Context Close exception occurred" + e);
}
}
}
}
Initial Context created
lookup successful
EJB method called and say: Hello Nicolas It’s Working!
Exception in thread "main" javax.ejb.EJBException: Could not unmarshal method ID;
nested exception is: java.rmi.UnmarshalException: Method not found: 'sayHello(Ljava.lang.String;)';
nested exception is: java.rmi.UnmarshalException: Method not found: 'sayHello(Ljava.lang.String;)'
java.rmi.UnmarshalException: Method not found: 'sayHello(Ljava.lang.String;)'
at weblogic.rmi.internal.MethodDescriptor.getCanonical(MethodDescriptor.java:268)
Possible problems: