如何在 jboss 7.1 上以编程方式绑定到 jndi 自定义对象?Context.bind 抛出异常,指示 jndi 上下文是只读的。有可能吗?
问问题
1616 次
1 回答
5
Yes, it is possible at all. The following code works in JBoss AS 7.1.1.Final:
@Stateless
public class JndiEjb {
private static final Logger LOGGER = LoggerFactory.getLogger(JndiEjb.class);
public void registerInJndi() {
try {
Context context = new InitialContext();
context.bind("java:global/JndiEjb", this);
} catch (NamingException e) {
LOGGER.error(String.format("Failed to register bean in jndi: %s", e.getMessage()));
}
}
public void retrieveFromJndi() {
try {
Context context = new InitialContext();
Object lookup = context.lookup("java:global/JndiEjb");
if(lookup != null && lookup instanceof JndiEjb) {
LOGGER.debug("Retrieval successful.");
JndiEjb jndiEjb = (JndiEjb)lookup;
jndiEjb.helloWorld();
}
} catch (NamingException e) {
LOGGER.error(String.format("Failed to register bean in jndi: %s", e.getMessage()));
}
}
public void helloWorld() {
LOGGER.info("Hello world!");
}
}
If you call first registerInJndi()
and afterwards retrieveFromJndi()
the object will be looked up and the method helloWorld()
is called.
You will find more information here.
于 2013-10-17T20:25:31.650 回答