0 Replies Latest reply on Mar 13, 2008 7:59 PM by Bryan Moore

    ServiceMBean or Not?

    Bryan Moore Newbie

      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());
      }
      }