3 Replies Latest reply on Jul 28, 2003 8:14 AM by Juha Lindfors

    Is it necessary to spawn new thread for each mbean?

    jikramer Newbie

      Hi -

      I just created an mbean that works fine. what we noticed though was it was running in the jboss main thread - it blocked the monitor & we could'nt shut it down. we got around this by explicitly creating a new thread in the start method of our entry class ( below ). just wondering if this is the recommended way to work, or if there is some kind of jboss mechanism which would spawn the necessary thread?

      thanks


      package com.marketaxess.datalink.MBean;

      import java.io.*;
      import java.util.*;
      import java.text.*;

      import javax.naming.*;

      import com.marketaxess.framework.jms.*;
      import com.marketaxess.datalink.core.*;

      import org.jboss.naming.NonSerializableFactory;


      /**
      * The JMSService class provides support for:
      *
      *
      */

      public class JMSService implements JMSServiceMBean, Runnable
      {

      private String jndiName = null;
      private HashMap contextMap = new HashMap();
      private boolean started;
      public String jName = "spaceHolder";

      static ProjectProperties projectProperties = null;

      JMSPublisher thisPublisher = null;
      JMSPublisherService thisPublisherService = null;

      boolean status = true;
      Thread thread = null;



      public JMSService(){

      System.out.println("in JMSService ctor()..");

      try{
      projectProperties = new ProjectProperties();
      jndiName = projectProperties.getProperty("jndiName");

      }catch(Exception e){ System.out.println("failure in JMSService ctor.. " + e); }

      }

      public void setJName(){
      jName="spaceHolder";
      }

      public String getJName(){
      return jName;
      }

      public String getJndiName(){
      return jndiName;
      }




      public void setJndiName (String jndiName) throws NamingException {
      String oldName = this.jndiName;
      this.jndiName = jndiName;
      if( started )
      {
      unbind(oldName);
      try{
      rebind();
      }catch(Exception e){ NamingException ne = new NamingException("Failed to update jndiName");
      ne.setRootCause(e);
      throw ne;
      }
      }
      }

      public void start() throws Exception{

      thread = new Thread(this);
      thread.setDaemon(true);
      thread.setName("authServer");
      thread.start();

      }

      public void run(){

      try{
      System.out.println("Starting the datalink process..");
      setJName();
      started = true;
      rebind();
      } catch(Exception ee){
      System.out.println("Exception thrown in JMSService.run()" + ee);
      }

      startPublisherService();
      startPublisher(thisPublisherService);


      }


      public void stop(){
      try{
      stopPublisherService();
      started = false;
      unbind(jndiName);
      }catch(Exception e){ System.out.println("Full Stop Unsuccessfull " + e); }
      }

      public void stopPublisherService(){

      setStatusToOff();
      thisPublisherService.setStatus(status);

      try{
      thisPublisherService.stopService();
      }catch(Exception e){ System.out.println("Publisher service stop Unsuccessfull " + e); }

      }

      public void startPublisherService(){
      setStatusToOn();

      thisPublisherService = new JMSPublisherService();
      thisPublisherService.setStatus(status);
      try{
      thisPublisherService.startService();
      }catch(Exception e){ System.out.println("Publisher service start Unsuccessfull: " + e); }

      }

      public void startPublisher(JMSPublisherService jmsPublisherService){
      setStatusToOn();
      thisPublisherService.setStatus(status);
      thisPublisher = new JMSPublisher(jmsPublisherService);
      }


      private static Context createContext(Context rootCtx, Name name) throws NamingException {
      Context subctx = rootCtx;
      for(int n = 0; n < name.size(); n ++){
      String atom = name.get(n);
      try{
      Object obj = subctx.lookup(atom);
      subctx = (Context)obj;
      } catch(NamingException e) { // No binding exists, create a subcontext
      subctx = subctx.createSubcontext(atom); }
      }
      return subctx;
      }


      /*


      */

      private void rebind() throws NamingException {
      InitialContext rootCtx = new InitialContext();
      // Get the parent context into which we are to bind
      Name fullName = rootCtx.getNameParser("").parse(jndiName);
      Name parentName = fullName;

      if( fullName.size() > 1 )
      parentName = fullName.getPrefix(fullName.size()-1);
      else
      parentName = new CompositeName();

      Context parentCtx = createContext(rootCtx, parentName);
      Name atomName = fullName.getSuffix(fullName.size()-1);
      String atom = atomName.get(0);

      try{
      NonSerializableFactory.rebind(parentCtx, atom, contextMap);
      } catch(NamingException e){ e.printStackTrace(); }
      }



      /*


      */

      private void unbind(String jndiName){
      try{
      Context rootCtx = (Context) new InitialContext();
      rootCtx.unbind(jndiName);
      Name fullName = rootCtx.getNameParser("").parse(jndiName);
      Name atomName = fullName.getSuffix(fullName.size()-1);
      String atom = atomName.get(0);
      NonSerializableFactory.unbind(atom);

      } catch(NamingException e){ e.printStackTrace();}
      }


      public void setStatusToOn(){
      status = true;
      if (thisPublisherService != null)
      thisPublisherService.setStatus(status);
      }

      public void setStatusToOff (){
      status = false;
      if (thisPublisherService != null)
      thisPublisherService.setStatus(status);
      }
      }