BindMBeanToJNDI

This service lets you bind your MBean or any object you wish to JNDI.

 

Note:  You can change objectToBind to whatever you want.  By default, it

binds the MBean itself to JNDI.

 

import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;

import org.jboss.system.ServiceMBeanSupport;
import org.jboss.util.naming.NonSerializableFactory;

public abstract class JndiService extends ServiceMBeanSupport implements
        JndiServiceMBean {

    private String jndiName;

    public void setJndiName(String jndiName) {
        if (getState() == STARTED)
             throw new IllegalStateException();
        this.jndiName = jndiName;
    }

    public String getJndiName() {
        return jndiName;
    }

    public void startService() throws Exception {
        rebind();
    }

    public void stopService() throws Exception {
        unbind(jndiName);
    }

    private void rebind() throws NamingException {
        InitialContext rootCtx = new InitialContext();
        try {
            Name fullName = rootCtx.getNameParser("").parse(jndiName);
            NonSerializableFactory.rebind(fullName, getObjectToBind(), true);
        } finally {
            rootCtx.close(); 
        }
    }
    
    private void unbind(String jndiName) throws NamingException {
        InitialContext rootCtx = new InitialContext();
        try {
            rootCtx.unbind(jndiName);
            NonSerializableFactory.unbind(jndiName);
        } finally {
            rootCtx.close(); 
        }
    }

     /**
      * @return the "this".objectToBind
      */
     abstract protected Object getObjectToBind();
}
import org.jboss.system.ServiceMBean;

public interface JndiServiceMBean extends ServiceMBean {
    String getJndiName();
    void setJndiName(String jndiName);
    
    void startService() throws Exception;
    void stopService() throws Exception;
}

 

This is a simpler version of the example in the JBoss 3.0 documentation.

http://docs.huihoo.com/jboss/online_manual/3.0/ch13s26.html