Monitor your JBoss resources using the DMR API

This is a short do-it-yourself guide that will teach you how to monitor your JBoss 7 server resources using EJB 3 Timers and the JBoss DMR API.

 Supposing that you need to monitor a critical attribute of your server, such the Connection pool statistics and need to issue a warning if you are running out of JDBC Connections, for example.

The CLI provides the relevant pool statistics in the datasources subsystem, which can be obtained using the following command: (substitute the ExampleDS with the actual datasource you are using)

which returns:

    “outcome” => “success”,
    “result” => {
        “ActiveCount” => “1”,
        “AvailableCount” => “20”,
        “AverageBlockingTime” => “2”,
        “AverageCreationTime” => “791”,
        “CreatedCount” => “1”,
        “DestroyedCount” => “0”,
        “MaxCreationTime” => “791”,
        “MaxUsedCount” => “1”,
        “MaxWaitTime” => “1”,
        “TimedOut” => “0”,
        “TotalBlockingTime” => “2”,
        “TotalCreationTime” => “791”
On key runtime attribute of the Connection pool is the ActiveCount which returns the number of JDBC Connections which are currently “busy”. If you want to be notified when this number grows too much without the need to install any agent on your server, all you need is an EJB timer and the JBoss DMR API:

package com.sample;

import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.*;
import javax.mail.Session;

import org.jboss.dmr.ModelNode;

import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class ResourceMonitor

    private final static Logger logger = Logger.getLogger(ResourceMonitor.class.getName());

    private TimerService timerService;

    private Session mailSession;


    @Schedule(dayOfWeek = "*", hour = "*", minute = "*", second = "*/30",year="*", persistent = false)
    public void backgroundProcessing()
    {"Timer is checking your resources........");
        ModelControllerClient client=null;
        try {
            client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999);
        } catch (UnknownHostException e) {

        ModelNode op = new ModelNode();         


        ModelNode address = op.get("address");         

        address.add("subsystem", "datasources");           
        address.add("data-source", "ExampleDS");       
        address.add("statistics", "pool");           

        ModelNode returnVal=null;
        try {
            returnVal = client.execute(op);
        } catch (IOException e) {
            // TODO Auto-generated catch block

        ModelNode node2 = returnVal.get("result");
        String _activeCount = node2.get("ActiveCount").asString();
        int activeCount = Integer.parseInt(_activeCount);
        if (activeCount > 20) {
            sendMessage("You are using too many Connections!");

    public void sendMessage(String txt) {
        try    {
            MimeMessage m = new MimeMessage(mailSession);
            Address from = new InternetAddress("");
            Address[] to = new InternetAddress[] {new InternetAddress("") };

            m.setRecipients(Message.RecipientType.TO, to);
            m.setSubject("JBoss AS 7 Warning");
            m.setSentDate(new java.util.Date());
  "Mail sent!");
        catch (javax.mail.MessagingException e)
            logger.severe("Error in Sending Mail: "+e);

This simple EJB timer fires an event every 30 seconds and checks for the value of the ActiveCount ModelNode. If the value exceeds 20 a mail is sent, using the default Mail Session.
In order to compile these application, if enough that you include JBoss Runtime Environment in your Eclipse project. If you are using Maven, add the following dependencies to your project:



Last but not least, in order to be able to deploy this EJB, you need to add in your MANIFEST.MF file the following dependencies:

Manifest-Version: 1.0