如何在jboss 7.1上以编程方式绑定到jndi自定义对象?Context.bind抛出异常,指示jndi上下文是只读的.有可能吗?
是的,这是可能的.以下代码适用于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!");
}
}
Run Code Online (Sandbox Code Playgroud)
如果先调用registerInJndi(),之后retrieveFromJndi()将查找该对象并helloWorld()调用该方法.
您可以在此处找到更多信息.
| 归档时间: |
|
| 查看次数: |
1523 次 |
| 最近记录: |