[jboss-user] [Beginners Corner] - ServiceMBean or Not?

Nimtu do-not-reply at jboss.com
Thu Mar 13 19:59:30 EDT 2008


Greetings from a JBoss newbie.  I'm trying to write my first JBoss program, which I have working, I'm just not sure I've done it the right way.  And in the process of writing it I've come up with something I don't really understand that is probably a key bit of required knowledge.

I've seen example code in JBoss books I have that looks like so...

package org.jboss.system;
public interface Service {
  public void create() throws Exception;
  public void start() throws Exception;
  public void  stop(); 
  public void destroy();
}

Sometimes that first method is shown as init();
Then other places, I see the following methods shown.

  public void createService() throws Exception;
  public void startService() throws Exception;
  public void stopService() throws Exception; 
  public void destroyService() throws Exception;


Whats the difference in the two sets of methods?


The code below represents the basic layout of my program (too long to show here) which works just fine, but I wanted to be able to run it outside of JBoss as well if possible.  The way I have it works outside but I have to carry around some JBoss jar files to do it.  If i remove the extends ServiceMBean clause, the program will still deploy and can be run inside JBoss using the JMX console, but it won't automatically start when JBoss starts or if its deployed while JBoss is running.

Thought? / Pointers?

   
public interface TrialMBean extends org.jboss.system.ServiceMBean {
  public void createService() throws Exception;
  public void startService() throws Exception;
  public void stopService() throws Exception;
  public void destroyService() throws Exception;
}

public class Trial extends org.jboss.system.ServiceMBeanSupport implements TrialMBean, Runnable {

  boolean keepRunning = true;

  public void createService() throws Exception {
    System.out.println("createService called");
  }

  public void startService() throws Exception {
    System.out.println("startService called");      
    Thread t = new Thread(this);
    t.start();
  }

  public void stopService() throws Exception {
    System.out.println("stopService called");
    keepRunning=false;
  }

  public void destroyService() throws Exception {
  }

  public void run() {
    keepRunning=true;
    while (keepRunning) {
      try {
        System.out.println("Still Running");
        Thread.sleep(1000);
      } catch (InterruptedException ex) {
        logger.severe(ex.toString());
      }
    }
  }

  public static void main(String[] args) {
    try {
      Thread t = new Thread(new Trial());
      t.start();
      t.join();
    } catch (Exception ex) {
      System.out.println(ex.toString());
    }
  }  




View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4136492#4136492

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4136492



More information about the jboss-user mailing list