I have created a remote EJB bean, deployed it into ejb-container and I'm able to call it's method remotely. Now I want to get the url on which the remote EJB bean is located from within remote EJB bean.
Here some code. Remote interface:
@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
}
}
For example : if remote bean is located on remote.bean.com:8081, then inside ExampleServiceImpl.example() I want to get this url (remote.bean.com:8081) without passing it directly as input param.
I've been trying to get some information from SessionContext:
InitialContext ic = new InitialContext();
SessionContext sctxLookup = (SessionContext)ic.lookup("java:comp/EJBContext");
, but got nothing useful there. Is it even possible to do this?
Thanks!
Example of calling remote EJB deployed on weblogic
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);
}
}
Where Calculator is
@Remote
public interface Calculator {
public int add(int a, int b);
}
And implementation is
@Stateless(mappedName = "myCalculator")
public class CalculatorImpl implements Calculator {
@Override
public int add(int a, int b) {
return a + b;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.