Wie binde ich programmgesteuert an ein benutzerdefiniertes JNDI-Objekt auf JBoss 7.1? Context.bind wirft eine Ausnahme, die darauf hinweist, dass der JNDI-Kontext schreibgeschützt ist. Ist das überhaupt möglich?
Antwort
Zu viele Anzeigen?Ja, es ist überhaupt möglich. Der folgende Code funktioniert 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("Fehler beim Registrieren des Beans 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("Abruf erfolgreich.");
JndiEjb jndiEjb = (JndiEjb)lookup;
jndiEjb.helloWorld();
}
} catch (NamingException e) {
LOGGER.error(String.format("Fehler beim Bean-Registrieren in JNDI: %s", e.getMessage()));
}
}
public void helloWorld() {
LOGGER.info("Hallo Welt!");
}
}
Wenn Sie zuerst registerInJndi()
aufrufen und danach retrieveFromJndi()
, wird das Objekt gesucht und die Methode helloWorld()
aufgerufen.
Weitere Informationen finden Sie hier.