main website home
  • About this blog

    This blog features updates, opinions, and technical notes from Caucho engineers about Caucho products, the enterprise Java industry, and PHP. Caucho Technology is the creator of the Resin Application Server and the Quercus PHP in Java engine. A leader in Java performance since 1998, Caucho is a Sun JavaEE licensee with over 9000 customers worldwide.
  • Tags

    ajaxworld bam candi cdi cloud cluster comet configuration deploy devoxx eclipse ejb embedded flash flex google app engine hessian hmtp ioc java ee 6 javaone javazone jms messaging newsletter nyjug osgi php pomegranate quercus resin resin 4.0 REST servlet sfjug silicon valley code camp spring testing training tssjs watchdog webbeans web profile websockets wordpress
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
« TDD and the release cycle
App Server Cloud Architectures »

Logging custom statistics in Resin

Last week I was out of town doing some on-site training for a potential customer and they asked if it’s possible to log the number of threads active at any given time. We don’t do that as part of Resin normally, but it got me thinking that this should actually be very easy using our scheduled tasks and a PHP script to access our MBeans.

All we need to do is create a web app that contains a quick logging script and a scheduled task to access it. Here’s the script first, which we’ll call index.php:

<?php

// get a handle to the JMX server
$mbean_server = new MBeanServer();

// grab Resin-specific and general Java threading beans
$server = $mbean_server->lookup("resin:type=Server");
$jvm_thread = $mbean_server->lookup("java.lang:type=Threading");
$thread_pool = $server->ThreadPool;

// log the latest stats
$logger = java_class("java.util.logging.Logger")->getLogger("example");
$logger->info("thread stats: " .
              "(resin - " .
              "active: " . $thread_pool->ThreadActiveCount . " " .
              "idle: " . $thread_pool->ThreadIdleCount . " " .
              "total: " . $thread_pool->ThreadIdleCount . ") " .
              "(jvm - " .
              "total: " . $jvm_thread->ThreadCount . " " .
              "peak: " . $jvm_thread->PeakThreadCount . ")");
?>

Accessing this script will log various thread statistics, so now all we need to do to log on a regular basis is to access the script regularly. Scheduled tasks do this for us. Just drop in the following resin-web.xml:

<web-app xmlns="http://caucho.com/ns/resin"
         xmlns:resin="urn:java:com.caucho.resources">

  <resin:ScheduledTask url="/index.php">
    <resin:period>1m</resin:period>
  </resin:ScheduledTask>

</web-app>

Now Resin will request the page from itself every minute, logging something like the following:

[09-07-13 10:26:00.445] {resin-42-TaskFuture} thread stats: (resin - active: 14 idle: 33 total: 33) (jvm - total: 59 peak: 59)

Of course you can do anything you want in the script or log anything you want, so the possibilities are endless. You might want to lock off a script like this using security constraints if it might introduce undue load or cause adverse affects, but this is straightforward as well. The point is that by creating two small files in a webapp, I got some custom, periodic logging.

The above scheduled task syntax is new in Resin 4.0, but the following will work in both 3.1 and 4.0:

<web-app xmlns="http://caucho.com/ns/resin">
  <scheduled-task url="/index.php">
    <period>1m</period>
  </scheduled-task>
</web-app>

The new syntax for 4.0 is more consistent with our CanDI configuration, but the 3.1 syntax will still be recognized.

If you’re looking for more stats to log, you can either use the /resin-admin application or jconsole to browse our MBeans. For more examples on how to use JMX from PHP, just check out the /resin-admin source.

Tags: jmx, php, resin, scheduled tasks

This entry was posted on Monday, July 13th, 2009 at 12:38 pm and is filed under Engineering. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Logging custom statistics in Resin”

  1. alfreema Says:
    October 2nd, 2009 at 8:40 pm

    I’d love to have that setup. Any chance you could translate that into JSTL for us JSP types?

Leave a Reply

You must be logged in to post a comment.


Caucho Technology is proudly powered by WordPress and Quercus®
Entries (RSS) and Comments (RSS).

  • HOME |
  • CONTACT US |
  • DOCUMENTATION |
  • BLOG |
  • WIKI 4 |
  • WIKI 3 |
  • Resin: Java Application Server
Copyright (c) 1998-2012 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.
resin® is a cloud optimized, java® application server that supports the java ee webprofile ®