繁体   English   中英

从远程EJB bean中获取URL

[英]Get URL from within remote EJB bean

我创建了一个远程EJB bean,并将其部署到ejb-container中,并且可以远程调用它的方法。 现在,我想从远程EJB Bean中获取远程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
   }

}

例如:如果远程bean位于remote.bean.com:8081上,则在ExampleServiceImpl.example()内部,我想获取此URL(remote.bean.com:8081),而不必将其直接作为输入参数传递。

我一直在尝试从SessionContext获取一些信息:

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

,但那里没有任何用处。 甚至有可能这样做吗?

谢谢!

调用部署在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;
    }   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM