Automatically starting a JBoss service (MBean)

10,576

Solution 1

Problem Solved

I found the solution to my problem. I was overriding the methods start( ), stop( ), destroy( ) and create( ); nevertheless, since I'm extending the abstract class ServiceMBeanSupport, I should be overriding the methods startService( ), stopService( ), etc.

I just moved my code from the method start( ) to the method startService( ) and now everything is behaving as I needed: as soon as its dependencies are fulfilled, my service is started and the method startService( ) is executed.

I think the conclusion is: although the life-cycle of an MBean involves calling create( ), start( ), stop( ) and destroy( ), the implementation of the abstract class ServiceMBeanSupport uses those methods to handle the life cycle. Nevertheless, it provides the protected methods *Service( ) in order to allow the programmer to participate in the life cycle.

Solution 2

For me it helped defining the stop and start methods in the MBean Interface:

public interface MyServiceMBean {
    ...

    // Lifecycle callbacks
    void start() throws Exception;
    void stop();
}

The advantage is that you don't have to extend ServiceMBean oder ServiceMBeanSupport.

Share:
10,576
KolemanPa
Author by

KolemanPa

I'm currently a PhD student in Software Engineering. I program most of the time using Java / JEE, but I also like PHP. I always use a Mac, and thus I frequently find compatibility problems in Java applications that are not as 'platform independent' as they should. I'm currently also interested in learning scripting languages such as AppleScript, awk, and bash.

Updated on June 12, 2022

Comments

  • KolemanPa
    KolemanPa about 2 years

    I'm trying to build a JBoss service that should be started automatically, each time the server is initiated.

    I've got the following class structure for my service:

    public interface CumbiaXPMServiceMBean extends org.jboss.system.ServiceMBean
    public class CumbiaXPMService extends org.jboss.system.ServiceMBeanSupport implements CumbiaXPMServiceMBean
    

    I've also got the following configuration file -- jboss-service.xml -- for my service :

    <server>
        <mbean code="uniandes.cumbia.xpm.jboss.CumbiaXPMService"
           name="jcumbia:service=JCumbiaEngine">
          <depends>jcumbia:service=cumbiaConsole</depends>
          <attribute name="LocationInCumbia" attributeClass="java.lang.String">XPMEngine</attribute>    
        </mbean>
    </server>
    

    My question is: how do I automatically start this service?

    I expected that JBoss will call the method start( ) as part as the loading process, but it is not: I've got a lot of loggin code in my start( ) method, but I haven't seen any output.

    However, when I look at the MBean status using the JMXConsole, its state (StateString) is 'Started'.

    Problem Solved

    I found the solution to my problem. I was overriding the methods start( ), stop( ), destroy( ) and create( ); nevertheless, since I'm extending the abstract class ServiceMBeanSupport, I should be overriding the methods startService( ), stopService( ), etc.

    I just moved my code from the method start( ) to the method startService( ) and now everything is behaving as I needed: as soon as its dependencies are fulfilled, my service is started and the method startService( ) is executed.

    I think the conclusion is: although the life-cycle of an MBean involves calling create( ), start( ), stop( ) and destroy( ), the implementation of the abstract class ServiceMBeanSupport uses those methods to handle the life cycle. Nevertheless, it provides the protected methods *Service( ) in order to allow the programmer to participate in the life cycle.