2013-10-17 3 views

Répondre

5

Oui, c'est possible du tout. Le code suivant fonctionne dans 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!"); 
    } 
} 

Si vous appelez d'abord registerInJndi() et ensuite retrieveFromJndi() l'objet sera levé les yeux et la méthode helloWorld() est appelée. Vous trouverez plus d'informations here.

+0

Et registerInJndi() doit être appelé dans servlet avec load-on-startup = 0 (Et peut-être à partir du service ejb de démarrage). Ici, nous pouvons éditer jndi sans exception. – user2889981

Questions connexes