java 自动启动 JBoss 服务 (MBean)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1010278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:47:47  来源:igfitidea点击:

Automatically starting a JBoss service (MBean)

javajbossservicejakarta-eembeans

提问by nozebacle

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

我正在尝试构建一个应该在每次启动服务器时自动启动的 JBoss 服务。

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 :

我还有以下配置文件——jboss-service.xml——用于我的服务:

<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.

我希望 JBoss 会在加载过程中调用方法 start(),但事实并非如此:我的 start() 方法中有很多登录代码,但我没有看到任何输出。

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

但是,当我使用 JMXConsole 查看 MBean 状态时,其状态 (StateString) 是“已启动”。

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.

我找到了解决我的问题的方法。我覆盖了 start()、stop()、destroy() 和 create() 方法;尽管如此,因为我要扩展抽象类 ServiceMBeanSupport,所以我应该覆盖方法 startService()、stopService() 等。

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.

我刚刚将代码从方法 start( ) 移到方法 startService( ) ,现在一切都按我的需要运行:只要满足其依赖关系,我的服务就会启动并执行方法 startService( )。

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.

我认为结论是:虽然 MBean 的生命周期涉及调用 create()、start()、stop() 和 destroy(),但抽象类 ServiceMBeanSupport 的实现使用这些方法来处理生命周期。尽管如此,它提供了受保护的方法 *Service() 以允许程序员参与生命周期。

采纳答案by nozebacle

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.

我找到了解决我的问题的方法。我覆盖了 start()、stop()、destroy() 和 create() 方法;尽管如此,因为我要扩展抽象类 ServiceMBeanSupport,所以我应该覆盖方法 startService()、stopService() 等。

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.

我刚刚将代码从方法 start( ) 移到方法 startService( ) ,现在一切都按我的需要运行:只要满足其依赖关系,我的服务就会启动并执行方法 startService( )。

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.

我认为结论是:虽然 MBean 的生命周期涉及调用 create()、start()、stop() 和 destroy(),但抽象类 ServiceMBeanSupport 的实现使用这些方法来处理生命周期。尽管如此,它提供了受保护的方法 *Service() 以允许程序员参与生命周期。

回答by Roland Schneider

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

对我来说,它帮助定义了 MBean 接口中的停止和启动方法:

public interface MyServiceMBean {
    ...

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

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

优点是您不必扩展 ServiceMBean 或 ServiceMBeanSupport。