我正在學習EJB,我希望得到以下代碼,但目前爲止沒有成功。 這裏是我的EJB項目代碼:在Glassfish 4上運行的EJB Stateless bean的JNDI查找
@Stateless
public class CalcBean implements ICalcRemote {
private static final long serialVersionUID = 5571798968598315142L;
@Override
public int add(int a, int b) {
return a + b;
}
}
package com.ejb.test.pckg;
import javax.ejb.Remote;
@Remote
public interface ICalcRemote extends ICalculator {
}
package com.ejb.test.pckg;
import java.io.Serializable;
public interface ICalculator extends Serializable {
public int add(int a, int b);
}
我在Eclipse霓虹燈運行與GlassFish 4.1.1。
當我把EJB部署項目,我可以在日誌中看到以下內容:
2017-02-24T21:18:09.036-0400|Info: Portable JNDI names for EJB CalcBean: [java:global/EJBDemo/CalcBean, java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote]
2017-02-24T21:18:09.036-0400|Info: Glassfish-specific (Non-portable) JNDI names for EJB CalcBean: [com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]
這是我的客戶端代碼:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb.test.pckg.ICalcRemote;
public class Main {
public static void main(String[] args) {
try {
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");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
Context ctx = new InitialContext();
ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");
System.out.println(calc.add(5, 7));
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* (non-Java-doc)
*
* @see java.lang.Object#Object()
*/
public Main() {
super();
}
}
,但我有沒有運氣。任何建議如何得到這個工作?
謝謝!
編輯:
這是我的主要(客戶端),其中包括從EJBDemo部署日誌信息:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb.test.pckg.ICalcRemote;
public class Main {
public static void main(String[] args) {
try {
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");
// props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
/*
* THIS IS from Glassfish log of EJBDemo deployment
*
* 2017-02-25T20:41:47.100-0400|Info: Portable JNDI names for EJB CalcBean: [java:global/EJBDemo/CalcBean,
* java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote] 2017-02-25T20:41:47.100-0400|Info: Glassfish-specific (Non-portable) JNDI names for EJB CalcBean:
* [com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]
*
*
*/
Context ctx = new InitialContext();
ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");
System.out.println(calc.add(5, 7));
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* (non-Java-doc)
*
* @see java.lang.Object#Object()
*/
public Main() {
super();
}
}
你得到什麼錯誤? – Leonardo
@Leondardo - 嗨,我明白了; javax.naming.NamingException:在SerialContext中'java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote'的查找失敗[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming。 impl.SerialInitContextFactory,java.naming.factory.url.pkgs = com.sun.enterprise.naming,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} – BustedSanta
[Root exception是javax.naming.NamingException:無法獲取SerialContext的SerialContextProvider [myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.url.pkgs = com.sun .enterprise.naming,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} – BustedSanta