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:
// 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:
Now Resin will request the page from itself every minute, logging something like the following:
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:
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.