2010-09-03 62 views
2

我想使用兩個獨立的服務器,一個用於web容器,一個用於ejb容器。 這兩個容器都是Glassfish V3。從另一個Glassfish(Web容器)訪問在另一個Glassfish(EJB容器)中運行的遠程EJB

但是,如何在我的web項目中使用@EJB註釋來訪問遠程ejb-container的ejb(s)。

在Ejb 2.0中,我們必須使用ejb描述符,但在Ejb3.0和glassfish v3中發生了什麼?

感謝

+0

可能重複[我如何可以部署會話Bean與另一臺計算機與客戶端JSP/Servlet](http://stackoverflow.com/questions/3614802/how-can-i-deploy-session-bean-on -otherother-computer-with-client-jsp-servlet) – 2010-09-04 20:01:56

+0

檢查出來 - 完整的示例:http://stackoverflow.com/questions/4763960/accessing-a-無狀態的EJB-從-另一個實例-的-的GlassFish/10194057#10194057 – 2012-04-17 18:09:26

回答

2

我從來沒有親自做過,因爲我喜歡用同一個JVM中的本地接口,它可以提高性能大幅提升。

但是你可以看看這個:

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB

不過你可以試試這個:

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


    // optional. Defaults to localhost. Only needed if web server is running 
    // on a different host than the appserver 
    props.setProperty("org.omg.CORBA.ORBInitialHost", "ejb_server_ip_or_host_name"); 

    // optional. Defaults to 3700. Only needed if target orb port is not 3700. 
    props.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); 

    InitialContext ic = new InitialContext(props); 

步驟2.使用在目標遠程EJB的全局JNDI名稱擡頭。

EJB 3.x中,假設 「com.acme.FooRemoteBusiness」 的全局JNDI名稱:

FooRemoteBusiness foo = (FooRemoteBusiness) ic.lookup("com.acme.FooRemoteBusiness"); 

EJB 2.x中,假設 「com.acme.FooHome」 的全局JNDI名稱:

Object obj = ic.lookup("com.acme.FooHome"); 

    FooHome fooHome = (FooHome) PortableRemoteObject.narrow(obj, FooHome.class);