问题
I am using Glassfish-bundled Eclipse IDE. I wrote a simple EJB application. but it doesn't work.
@Stateless
@Remote(CalculatorRemote.class)
@Local(CalculatorLocal.class)
public class Calculator implements CalculatorRemote, CalculatorLocal {
@Override
public String sayHello(String name) {
System.out.println("ejb:"+name);
return null;
}
}
----
@Remote
public interface CalculatorRemote {
public String sayHello(String name);
}
-------
@Local
public interface CalculatorLocal {
public String sayHello(String name);
}
I wrote a standalone client to test, but failed. can't find JNDI.
public class Main {
public static void main(String[] args) throws Exception {
InitialContext ctx = new InitialContext();
CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote");
bean.sayHello("Billy Bob");
}
}
In the server log, it said
INFO: Portable JNDI names for EJB Calculator : [java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote, java:global/TestEAR/TEjb/Calculator!com.CalculatorLocal]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB Calculator : [com.CalculatorRemote, com.CalculatorRemote#com.CalculatorRemote]
Also, I have tried
ctx.lookup("com.CalculatorRemote")
still doesn't work.
error message is
java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.Main.main(Main.java:9)
Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote' in SerialContext [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.Main.main(Main.java:9)
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 2 more
Caused by: java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
... 3 more
Please help me.
回答1:
Can you just add the above lines:
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// glassfish default port value will be 3700,
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);
CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator! com.CalculatorRemote");
I have created a blog post here using EJB3x with Glassfish v3. http://anirbanchowdhury.wordpress.com/2012/06/07/ejb-3-application-in-glassfish-3x/
回答2:
I think that exception is thrown because you didn't configure the initial context properly. Either create a jndi.properties file, or create a hash table with the properties and it send it to the constructor of IntialContext.
Creating EJB3 using Netbeans and Glassfish
回答3:
Just today I had an issue with this. Your standalone client failed but it will work inside GF EJB container.
For client testing you need 2 things to make it work:
- from GlassFish_install_folder\glassfish\lib get javaee, gf-client and appserv-rt jars. the last one contains jndi.prop so you could use default c-tor InitialContext();
- from GlassFish_install_folder\glassfish\modules get all jars.
These jars need to be in your classpath. It is silly but I don`t know yet the minimum jars from 2) in order to make it work.
回答4:
Solution is as below.
In the code below, You have to call a bean in another JVM Right ? For ex, your main class is in JRE and BEAN is in Glassfish JVM.
The code is partially right. But more coded is needed to your class..
Go through this link, complete example you can follow.
- Accessing a stateless EJB from another instance of Glassfish
public class Main {
public static void main(String[] args) throws Exception {
InitialContext ctx = new InitialContext();
CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote");
bean.sayHello("Billy Bob");
}
}
来源:https://stackoverflow.com/questions/5705922/ejb3-glassfish-jndi-lookup