2016-09-27 77 views
0

我創建了一個遠程EJB bean,將它部署到ejb-container中,並且可以遠程調用它的方法。現在我想從遠程EJB bean中獲取遠程EJB bean所在的url。從遠程EJB bean中獲取URL

這裏有一些代碼。遠程接口:

@Remote 
public interface ExampleService { 

void example(); 

} 

EJB:

@Stateful 
public class ExampleServiceImpl implements ExampleService { 

    @Override 
    public void example() { 
     //get Remote Url When This Method Is Called 
    } 

} 

例如:如果遠程豆位於remote.bean.com:8081,那裏面ExampleServiceImpl.example() 我想這個網址(remote.bean.com:8081)而不直接作爲輸入參數傳遞它。

我一直在試圖從SessionContext得到一些信息:

InitialContext ic = new InitialContext(); 
SessionContext sctxLookup = (SessionContext)ic.lookup("java:comp/EJBContext"); 

,但什麼也沒得到有用的存在。 它甚至可以做到這一點?

謝謝!

回答

0

調用部署在WebLogic遠程EJB

private static Calculator getRemoteCalculator() { 
    Hashtable<String, String> props = new Hashtable<String, String>(); 

    props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); 
    props.put(Context.PROVIDER_URL, "t3://localhost:7001");  


    try { 
     InitialContext ctx = new InitialContext(props); 
     return (Calculator) ctx.lookup("myCalculator#com.javaee.Calculator"); 
    } catch (NamingException e) { 
     throw new RuntimeException(e); 
    } 
} 

哪裏計算器是

@Remote 
public interface Calculator { 

    public int add(int a, int b); 

} 

和實施例是

@Stateless(mappedName = "myCalculator") 
public class CalculatorImpl implements Calculator { 
    @Override 
    public int add(int a, int b) { 
     return a + b; 
    } 
} 
+0

你甚至看我的問題?我沒有任何問題遠程調用ejb(我在我的問題的第一句中寫過) –