2011-05-31 4 views

Répondre

6

Il y a deux façons principales de le faire.

descripteur de déploiement/déclarative

Utilisez le JNDI Binding Manager en créant un descripteur de déploiement dans un fichier tel que * my-JNDI liaisons *** - service.xml ** et déposez-le dans deploy du serveur répertoire. Un descripteur exemple ressemble à ceci:

<mbean code="org.jboss.naming.JNDIBindingServiceMgr" 
     name="jboss.tests:name=example1"> 
    <attribute name="BindingsConfig" serialDataType="jbxb"> 
     <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:jndi="urn:jboss:jndi-binding-service" 
         xs:schemaLocation="urn:jboss:jndi-binding-service \ 
       resource:jndi-binding-service_1_0.xsd"> 
      <jndi:binding name="bindexample/message"> 
       <jndi:value trim="true"> 
        Hello, JNDI! 
       </jndi:value> 
      </jndi:binding> 
     </jndi:bindings> 
    </attribute> 
</mbean> 

programatic

Acquire un contexte JNDI et exécuter vous-même obligatoire. Ceci est un exemple d'un « en-jboss » appellent à faire:

import javax.naming.*; 

    public static void bind(String name, Object obj) throws NamingException { 
     Context ctx = null; 
     try { 
      ctx = new InitialContext(); 
      ctx.bind(name, obj); 
     } finally { 
      try { ctx.close(); } catch (Exception e) {} 
     } 
    } 

Si le nom est déjà lié, vous pouvez appeler REBIND:

public static void rebind(String name, Object obj) throws NamingException { 
    Context ctx = null; 
    try { 
     ctx = new InitialContext(); 
     ctx.rebind(name, obj); 
    } finally { 
     try { ctx.close(); } catch (Exception e) {} 
    } 
} 

Pour supprimer la liaison, appel unbind:

public static void unbind(String name) throws NamingException { 
    Context ctx = null; 
    try { 
     ctx = new InitialContext(); 
     ctx.unbind(name); 
    } finally { 
     try { ctx.close(); } catch (Exception e) {} 
    } 
} 

Si vous essayez de le faire à distance (c.-à-pas dans la machine virtuelle JBoss.), alors vous devrez acquérir un contexte JNDI distant:

import javax.naming.*; 
String JBOSS_JNDI_FACTORY = "org.jnp.interfaces.NamingContextFactory"; 
String JBOSS_DEFAULT_JNDI_HOST = "localhost"; 
int JBOSS_DEFAULT_JNDI_PORT = 1099; 
..... 
Properties p = new Properties(); 
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, JBOSS_JNDI_FACTORY); 
p.setProperty(Context.PROVIDER_URL, JBOSS_DEFAULT_JNDI_HOST + ":" + JBOSS_DEFAULT_JNDI_PORT); 
Context ctx = new InitialContext(p); 
Questions connexes