hengheng123456789

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            297 Posts :: 68 Stories :: 144 Comments :: 0 Trackbacks

          眾所周知Jboss依賴于JMX來裝載MBean服務,而這些MBean服務組成了具體服務器實例的差異性。標準JBoss發布版本提供的所有功能都是基于MBean的。所以,如果要為JBoss服務器添加新的服務,最好的方法是開發自己的JMX MBean服務。
          MBean服務的生命周期是由如下三個JBoss MBean負責的:SARDeployer、ServiceConfigurator、ServiceController。

          如自定義MBean服務依賴于其他MBean服務,可以通過如下方法實現:
          1、在自定義MBean接口中添加Service中任何方法。
          ???這種方式避免了對JBoss具體接口的依賴。
          2、為自定義MBean接口擴展org.jboss.system.Service接口。

          3、為自定義MBean接口擴展org.jboss.system.ServiceMBean接口。
          ???最簡單的辦法是將自定義MBean接口繼承于ServiceMBean接口,將MBean實現類繼承ServiceMBeanSupport類。ServiceMBeanSupport已經實現了ServiceMBean接口,ServiceMBeanSupport還集成了日志、JBoss服務狀態管理跟蹤功能,這些方法需要我們具體實現createService、startService、stopService和destroyService中的部分方法。

          下面介紹基于ServiceMBean接口和ServiceMBeanSupport類的JNDIMapMBean接口及其JNDIMap實現類。

          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>



          ?

          posted on 2006-12-14 18:03 哼哼 閱讀(3937) 評論(0)  編輯  收藏 所屬分類: JMXJBOSS
          主站蜘蛛池模板: 西乌珠穆沁旗| 启东市| 昭苏县| 聊城市| 西乌珠穆沁旗| 三台县| 大荔县| 元江| 普陀区| 安丘市| 古蔺县| 全州县| 涞水县| 芜湖市| 潢川县| 都昌县| 建瓯市| 永吉县| 兴安盟| 孝义市| 博湖县| 汉寿县| 奉化市| 库车县| 独山县| 开化县| 互助| 清镇市| 漾濞| 平度市| 东乡| 郸城县| 宜君县| 凤阳县| 毕节市| 乌鲁木齐市| 方城县| 墨竹工卡县| 中牟县| 正安县| 武定县|