眾所周知Jboss依賴于JMX來裝載MBean服務(wù),而這些MBean服務(wù)組成了具體服務(wù)器實(shí)例的差異性。標(biāo)準(zhǔn)JBoss發(fā)布版本提供的所有功能都是基于MBean的。所以,如果要為JBoss服務(wù)器添加新的服務(wù),最好的方法是開發(fā)自己的JMX MBean服務(wù)。
MBean服務(wù)的生命周期是由如下三個(gè)JBoss MBean負(fù)責(zé)的:SARDeployer、ServiceConfigurator、ServiceController。
如自定義MBean服務(wù)依賴于其他MBean服務(wù),可以通過如下方法實(shí)現(xiàn):
1、在自定義MBean接口中添加Service中任何方法。
???這種方式避免了對(duì)JBoss具體接口的依賴。
2、為自定義MBean接口擴(kuò)展org.jboss.system.Service接口。
3、為自定義MBean接口擴(kuò)展org.jboss.system.ServiceMBean接口。
???最簡單的辦法是將自定義MBean接口繼承于ServiceMBean接口,將MBean實(shí)現(xiàn)類繼承ServiceMBeanSupport類。ServiceMBeanSupport已經(jīng)實(shí)現(xiàn)了ServiceMBean接口,ServiceMBeanSupport還集成了日志、JBoss服務(wù)狀態(tài)管理跟蹤功能,這些方法需要我們具體實(shí)現(xiàn)createService、startService、stopService和destroyService中的部分方法。
下面介紹基于ServiceMBean接口和ServiceMBeanSupport類的JNDIMapMBean接口及其JNDIMap實(shí)現(xiàn)類。
package org.joss.chap2.ex2;
import javax.naming.NamingException;
import org.jboss.system.ServiceMBean;
public interface JNDIMapMBean extends ServiceMBean
{
?public String getJndiName();
?public void setJndiName(String jndiName) throws NamingException;
}
package org.joss.chap2.ex2;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import org.jboss.naming.NonSerializableFactory;
import org.jboss.system.ServiceMBeanSupport;
public class JNDIMap extends ServiceMBeanSupport implements JNDIMapMBean
{
?private String jndiName;
?private HashMap contextMap = new HashMap();
?public JNDIMap()
?{
??super();
??// TODO Auto-generated constructor stub
?}
?public String getJndiName()
?{
??
??return jndiName;
?}
?public void setJndiName(String jndiName) throws NamingException
?{
??String oldName = this.jndiName;
??this.jndiName = jndiName;
??if(super.getState()==STARTED)
??{
???unbind(oldName);
???try
???{
????rebind();
???}
???catch(Exception e)
???{
????NamingException ne = new NamingException("Failed to update jndiName");
????ne.setRootCause(e);
????throw ne;
???}
??}
?}
?
?public void startService()throws Exception
?{
??rebind();
?}
?
?
?public void stopService()
?{
??unbind(jndiName);
?}
?
?private void rebind() throws NamingException
?{
??InitialContext rootCtx = new InitialContext();
??Name fullName = rootCtx.getNameParser("").parse(jndiName);
??NonSerializableFactory.rebind(fullName,contextMap,true);
?}
?
?private void unbind(String jndiName)
?{
??try
??{
???InitialContext rootCtx = new InitialContext();
???rootCtx.unbind(jndiName);
???NonSerializableFactory.unbind(jndiName);
??}
??catch(NamingException e)
??{
???System.out.println(e);
??}
?}
?public String getName()
?{
??// TODO Auto-generated method stub
??return null;
?}
?public int getState()
?{
??// TODO Auto-generated method stub
??return 0;
?}
?public String getStateString()
?{
??// TODO Auto-generated method stub
??return null;
?}
?public void jbossInternalLifecycle(String arg0) throws Exception
?{
??// TODO Auto-generated method stub
?}
?public void create() throws Exception
?{
??// TODO Auto-generated method stub
?}
?public void start() throws Exception
?{
??// TODO Auto-generated method stub
?}
?public void stop()
?{
??// TODO Auto-generated method stub
?}
?public void destroy()
?{
??// TODO Auto-generated method stub
?}
?public ObjectName preRegister(MBeanServer arg0, ObjectName arg1)
???throws Exception
?{
??// TODO Auto-generated method stub
??return null;
?}
?public void postRegister(Boolean arg0)
?{
??// TODO Auto-generated method stub
?}
?public void preDeregister() throws Exception
?{
??// TODO Auto-generated method stub
?}
?public void postDeregister()
?{
??// TODO Auto-generated method stub
?}
}
<?xml version="1.0" encoding="UTF-8"?>
<server>
?<mbean code="org.joss.chap2.ex2.JNDIMap"
??name="chap2.ex2:service=JNDIMap">
??<attribute name="JndiName">inmemory/map/MapTest</attribute>
??<depends>jboss:service=Naming</depends>
?</mbean>
</server>
?