tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Einar M. RÃ¥berg Rosenvinge <eina...@tihlde.org>
Subject Using MBeans with Tomcat
Date Mon, 14 Apr 2003 09:09:48 GMT
Hi!

I have a webapp running on a Tomcat web server that I would like to
monitor. I have only just recently looked at JMX, and this seems to make
the job a bit simpler (hopefully).

At first I would like to monitor the logged in users. This is done in the
following way:

I have a session bean that implements HttpSessionBindingListener. This
means that when a new session is created, valueBound() is called on the
session bean, and valueUnbound() is called when the session is invalidated
or times out.

So far so good.

In valueBound() I wish to gain access to an MBeanServer object, and
through this gain access to a CurrentUsers object where I will register
the current session.
In valueUnbound() I wish to unregister the session from CurrentUsers.

As of now I have a class called Initiator, which implements
ServletContextListener.  contextInitialized() is called on this when the
web application starts up.
Here I instantiate an MBeanServer and add this to the ServletContext
(application) object.

Is this a good place to create an MBeanServer? Is there a way I can gain
access to Tomcat's MBeanServer? Would that be a good idea?

As of now, SessionBean.valueBound() looks like this:

  public void valueBound(javax.servlet.http.HttpSessionBindingEvent httpSessionBindingEvent)
{
    //called whenever this object is bound to a http session
    MBeanServer server = (MBeanServer) httpSessionBindingEvent.getSession().getServletContext().getAttribute("MBeanServer");
    try {
      ObjectName objName = new ObjectName("com.test:type=CurrentUsers,name=currUsers"); Object[]
      params = {this};
      String[] signature = {"com.test.SessionBean"};
      server.invoke(objName, "addSession", params, signature);
    } catch (Exception e) {
      System.err.println("valueBound():"  + e);
    }
    }
  }

I have some more questions, too:

I also (of course) want to get information _out_ of the MBeanServer. What
is the most convenient way of doing that?

E.g. using HtmlAdaptorServer or some SNMP stuff...

HtmlAdaptorServer could be started like this:

      HtmlAdaptorServer adaptorServer = new HtmlAdaptorServer();
      ObjectName adaptorObjName = new ObjectName("com.test:type=htmladaptor,port=8902");
      adaptorServer.setPort(8902);
      server.registerMBean(adaptorServer, adaptorObjName);
      adaptorServer.start();

The start() method (of course) does not return. Should I create the
HtmlAdaptorServer from a separate thread started from Initiator?

Or should I create MBeanServer (and perhaps HtmlAdaptorServer) in some
other magical place?


Hope someone can clarify things a bit.


-Einar

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


Mime
View raw message