You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Kris Balle Kristensen <kr...@aktant.com> on 2005/03/20 16:37:29 UTC

Howto MBean

Hi there,
 
I have written a bunch of MBeans for JBoss, but I can't figure out how
to do it for Tomcat5 :(. I have google'd a lot of pages containing the
MBean keyword, but have yet to find a tutorial for MBeans deployed on
Tomcat5.
 
My scenario:
I need a persistent object in Tomcat (trigger mechanism) that will be
triggered when a certain time of day (like midnight) is up. Furthermore
I need some kind of cache for some of my beans. This cache should be
callable from any jsp page if so desired. I'm not sure if MBean would be
the right approach, but anyway this is what I normally use with JBoss. 
What I need is an MBean example written for Tomcat5 including example
descriptors for same. I have tried to do this myself, but it looks like
my Mbean doesn't get deployed on startup of Tomcat. I can't see it in
the Tomcat log.

In the test example below, the Mbean is just suppose to show the current
datetime when the getShowTime (attribute showTime) gets activated. Also
the System.outs should be printed out during init/start/stop/destroy of
this MBean, but nothing happens.

I have tried the following:

In server.xml I added the following:
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"
            debug="0"
	
descriptors="/mbean/test/mbean-descriptors.xml"/>
  <Listener
className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
            debug="0"/>

In mbean/test/mbean-descriptors.xml:
<mbean         name="mbean.test.ShowTimeMBean"
            className="org.apache.catalina.mbeans.ClassNameMBean"
          description="Shows the current time"
               domain="Catalina"
                group="ShowTime"
                 type="mbean.test.ShowTime">
    <attribute   name="timeNow"
          description="Shows the current time"
                 type="java.lang.String"
            writeable="false"/>
    <operation name="start" description="Start" impact="ACTION"
returnType="void" />
    <operation name="stop" description="Stop" impact="ACTION"
returnType="void" />
    <operation name="init" description="Init" impact="ACTION"
returnType="void" />
    <operation name="destroy" description="Destroy" impact="ACTION"
returnType="void" />
    <operation name="showDateTimeNow" description="showDateTimeNow"
impact="ACTION" returnType="java.lang.String" />
  </mbean>

In package test.mbean:
public class ShowTime {
    private String showTime;
    public String getShowTime()  {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
tt:mm:ss");
       showTime =  sdf.format(new Date());
       return showTime;
    }
    public void start() {
        System.out.println("start called..");
    }

    public void stop() {
        System.out.println("stop called..");
    }

    public void init() {
        System.out.println("init called..");
    }
    public void destroy() {
        System.out.println("destroy called..");
    }

}

public class ShowTimeMBean extends BaseModelMBean{
    String timeNow = null;
    protected MBeanServer mserver;
    protected ManagedBean managed;
    public ShowTimeMBean() throws MBeanException,
RuntimeOperationsException {
        initialize();
    }

    public ShowTimeMBean(ModelMBeanInfo modelMBeanInfo) throws
MBeanException, RuntimeOperationsException {
        super(modelMBeanInfo);
        initialize();
    }

    public ShowTimeMBean(String s) throws MBeanException,
RuntimeOperationsException {
        super(s);
        initialize();
    }

    public ShowTimeMBean(String s, ModelerSource modelerSource) throws
MBeanException, RuntimeOperationsException {
        super(s, modelerSource);
        initialize();
    }

    private void initialize() {
        registry = MBeanUtils.createRegistry();
        mserver = MBeanUtils.createServer();
        managed = registry.findManagedBean("ShowTime");

    }

    public void start() {
        System.out.println("ShowTimeMBean::start called..");
    }

    public void stop() {
        System.out.println("ShowTimeMBean::stop called..");
    }

    public void init() {
        System.out.println("ShowTimeMBean::init called..");
    }
    public void destroy() {
        System.out.println("ShowTimeMBean::destroy called..");
    }

    public String showDateTimeNow() {
        ShowTime st = (ShowTime)resource;
        timeNow = st.getShowTime();
        return timeNow;
    }

    public String getTimeNow() {
        return timeNow;
    }

}

Everything gets deployed using a .war file.
 
Can any of you point me in the right direction? 

Regards.
Kris
 

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Howto MBean

Posted by Bill Barker <wb...@wilshire.com>.
Tomcat doesn't have an automatic MBean deployment option for a Context. 
You'll need a ServletContextListener (or otherwise) to register your 
application MBeans.

Note that with commons-modeler 1.1 (which ships with Tomcat 5), it is no 
longer necessary to include your mbeans-descriptor.xml in the 
ServerLifecycleListener.  commons-modeler will automagically load it when 
your MBeans are registered.

"Kris Balle Kristensen" <kr...@aktant.com> wrote in message 
news:EB67410D50E72442A3B6FDC023E1DC5A1C0ECF@communicator.aktant.com...
Hi there,

I have written a bunch of MBeans for JBoss, but I can't figure out how
to do it for Tomcat5 :(. I have google'd a lot of pages containing the
MBean keyword, but have yet to find a tutorial for MBeans deployed on
Tomcat5.

My scenario:
I need a persistent object in Tomcat (trigger mechanism) that will be
triggered when a certain time of day (like midnight) is up. Furthermore
I need some kind of cache for some of my beans. This cache should be
callable from any jsp page if so desired. I'm not sure if MBean would be
the right approach, but anyway this is what I normally use with JBoss.
What I need is an MBean example written for Tomcat5 including example
descriptors for same. I have tried to do this myself, but it looks like
my Mbean doesn't get deployed on startup of Tomcat. I can't see it in
the Tomcat log.

In the test example below, the Mbean is just suppose to show the current
datetime when the getShowTime (attribute showTime) gets activated. Also
the System.outs should be printed out during init/start/stop/destroy of
this MBean, but nothing happens.

I have tried the following:

In server.xml I added the following:
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"
            debug="0"

descriptors="/mbean/test/mbean-descriptors.xml"/>
  <Listener
className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
            debug="0"/>

In mbean/test/mbean-descriptors.xml:
<mbean         name="mbean.test.ShowTimeMBean"
            className="org.apache.catalina.mbeans.ClassNameMBean"
          description="Shows the current time"
               domain="Catalina"
                group="ShowTime"
                 type="mbean.test.ShowTime">
    <attribute   name="timeNow"
          description="Shows the current time"
                 type="java.lang.String"
            writeable="false"/>
    <operation name="start" description="Start" impact="ACTION"
returnType="void" />
    <operation name="stop" description="Stop" impact="ACTION"
returnType="void" />
    <operation name="init" description="Init" impact="ACTION"
returnType="void" />
    <operation name="destroy" description="Destroy" impact="ACTION"
returnType="void" />
    <operation name="showDateTimeNow" description="showDateTimeNow"
impact="ACTION" returnType="java.lang.String" />
  </mbean>

In package test.mbean:
public class ShowTime {
    private String showTime;
    public String getShowTime()  {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
tt:mm:ss");
       showTime =  sdf.format(new Date());
       return showTime;
    }
    public void start() {
        System.out.println("start called..");
    }

    public void stop() {
        System.out.println("stop called..");
    }

    public void init() {
        System.out.println("init called..");
    }
    public void destroy() {
        System.out.println("destroy called..");
    }

}

public class ShowTimeMBean extends BaseModelMBean{
    String timeNow = null;
    protected MBeanServer mserver;
    protected ManagedBean managed;
    public ShowTimeMBean() throws MBeanException,
RuntimeOperationsException {
        initialize();
    }

    public ShowTimeMBean(ModelMBeanInfo modelMBeanInfo) throws
MBeanException, RuntimeOperationsException {
        super(modelMBeanInfo);
        initialize();
    }

    public ShowTimeMBean(String s) throws MBeanException,
RuntimeOperationsException {
        super(s);
        initialize();
    }

    public ShowTimeMBean(String s, ModelerSource modelerSource) throws
MBeanException, RuntimeOperationsException {
        super(s, modelerSource);
        initialize();
    }

    private void initialize() {
        registry = MBeanUtils.createRegistry();
        mserver = MBeanUtils.createServer();
        managed = registry.findManagedBean("ShowTime");

    }

    public void start() {
        System.out.println("ShowTimeMBean::start called..");
    }

    public void stop() {
        System.out.println("ShowTimeMBean::stop called..");
    }

    public void init() {
        System.out.println("ShowTimeMBean::init called..");
    }
    public void destroy() {
        System.out.println("ShowTimeMBean::destroy called..");
    }

    public String showDateTimeNow() {
        ShowTime st = (ShowTime)resource;
        timeNow = st.getShowTime();
        return timeNow;
    }

    public String getTimeNow() {
        return timeNow;
    }

}

Everything gets deployed using a .war file.

Can any of you point me in the right direction?

Regards.
Kris




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org