2017-02-25 96 views
0

我正在學習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(); 
    } 

} 
+0

你得到什麼錯誤? – Leonardo

+0

@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

+0

[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

回答

0

我終於得到了事情的工作!儘管閱讀了許多與這個問題相關的帖子,但這仍然是一個過程。以下是我的配置的詳細信息。希望這對某些人有用。

OS:贏得10 IDE:Eclipse的霓虹燈 應用服務器:Glassfish的4.1.1 JDK:1.8.0_111

我需要提到這篇文章最終導致我答案:

http://mavicode.com/2014/08/a-standalone-client-for-ejbs-running-on-glassfish-4/

小號o,感謝和讚揚。

首先,創建EJB演示項目:

package com.ejb.test.pckg; 

import java.io.Serializable; 

public interface ICalculator extends Serializable { 

    public int add(int a, int b); 

} 


package com.ejb.test.pckg; 

import javax.ejb.Remote; 

@Remote 
public interface ICalcRemote extends ICalculator { 

} 

package com.ejb.test.pckg; 

import javax.ejb.Stateless; 

// @Stateless(mappedName = "chester") 
@Stateless 
public class CalcBean implements ICalcRemote { 

    private static final long serialVersionUID = 5571798968598315142L; 

    @Override 
    public int add(int a, int b) { 
     return a + b; 
    } 

} 

部署在服務器上(運行方式>在服務器上運行)>檢查日誌看JDNI信息。它看起來應該是這樣的:

2017-02-25T20:41:47.100-0400 |信息:對於EJB CalcBean移植的JNDI名稱:【JAVA:全球/ EJBDemo/CalcBean, 的java:全球/ EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote] 2017-02-25T20:41:47.100-0400 |信息:特定於Glassfish的(不可移植的) EJB CalcBean的JNDI名稱: [com.ejb。 test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]

之後,創建應用程序客戶端項目。 Main.java將自動創建。這是我的主:

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"); 

      Context ctx = new InitialContext(); 
      ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote"); 

      System.out.println(calc.add(5, 43)); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    /* 
    * (non-Java-doc) 
    * 
    * @see java.lang.Object#Object() 
    */ 
    public Main() { 
     super(); 
    } 

} 

在這一點上,我的項目是這樣的:

enter image description here

這裏來的重要組成部分。從Glassfish lib目錄添加一個新的外部庫。 !重要提示:不要複製粘貼到路徑! .jar顯然使用/引用其他GF罐中的其他類。所以只需將它作爲外部jar添加到構建路徑而不是複製/粘貼。

您應該立即設置。作爲Java應用程序運行Main。

這就是我得到的。

enter image description here

enter image description here