All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.addc.commons.queue.DefaultPersistingQueueStatistics Maven / Gradle / Ivy

Go to download

The addc-queues library supplies support for internal persistent queues using an optional DERBY database for storage.

The newest version!
package com.addc.commons.queue;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.ObjectName;
import javax.management.StandardMBean;

import com.addc.commons.Constants;
import com.addc.commons.i18n.I18nTextFactory;

/**
 * The DefaultPersistingQueueStatistics tracks elements being added, retrieved
 * or dropped from a {@link PersistingQueue}.
 * 
 */
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
public class DefaultPersistingQueueStatistics extends StandardMBean implements PersistingQueueStatistics, IMbQueueStats {

    protected final String LINE_SEPARATOR= System.getProperty("line.separator");
    private long elementsCreated;
    private long elementsDroppped;
    private long elementsEnqueued;
    private long elementsDequeued;
    private final ObjectName objectName;

    /**
     * Create a DefaultPersistingQueueStatistics object
     * 
     * @param objectName
     *            The MBean Object name
     */
    public DefaultPersistingQueueStatistics(ObjectName objectName) {
        super(IMbQueueStats.class, false);
        this.objectName= objectName;
    }

    @Override
    public synchronized void itemCreated(T element) {
        elementsCreated++;
    }

    @Override
    public synchronized void itemDropped(T element) {
        elementsDroppped++;
    }

    @Override
    public synchronized void itemReadFromPersistence(T element) {
        elementsDequeued++;
    }

    @Override
    public synchronized void itemWrittenToPersistence(T element) {
        elementsEnqueued++;
    }

    @Override
    public synchronized long getItemsCreated() {
        return elementsCreated;
    }

    @Override
    public synchronized long getItemsDropped() {
        return elementsDroppped;
    }

    @Override
    public synchronized long getItemsReadFromPersistence() {
        return elementsDequeued;
    }

    @Override
    public synchronized long getItemsWrittenToPersistence() {
        return elementsEnqueued;
    }

    @Override
    public ObjectName getObjectName() {
        return objectName;
    }

    
    @Override
    public synchronized String getQueueStatistics() {
        StringBuffer sb= new StringBuffer(
                I18nTextFactory.getTranslator(Constants.BASENAME).translate(Constants.PQ_STATS_TITLE))
                        .append(LINE_SEPARATOR);
        sb.append(I18nTextFactory.getTranslator(Constants.BASENAME).translate(Constants.PQ_STATS_RECEIVED))
                .append(this.getItemsCreated()).append(LINE_SEPARATOR);
        sb.append(I18nTextFactory.getTranslator(Constants.BASENAME).translate(Constants.PQ_STATS_DROPPED))
                .append(this.getItemsDropped()).append(LINE_SEPARATOR);
        sb.append(I18nTextFactory.getTranslator(Constants.BASENAME).translate(Constants.PQ_STATS_ENQUEUED))
                .append(this.getItemsWrittenToPersistence()).append(LINE_SEPARATOR);
        sb.append(I18nTextFactory.getTranslator(Constants.BASENAME).translate(Constants.PQ_STATS_DEQUEUED))
                .append(this.getItemsReadFromPersistence()).append(LINE_SEPARATOR);
        return sb.toString();
    }

    @Override
    protected String getDescription(MBeanInfo info) {
        return "Persistent queue statistics";
    }

    @Override
    protected String getDescription(MBeanAttributeInfo info) {
        if("ItemsCreated".equals(info.getName())) {
            return "The number of items put to the queue";
        }
        if ("ItemsDropped".equals(info.getName())) {
            return "The number of elements dropped from the queue";
        }
        if ("ItemsReadFromPersistence".equals(info.getName())) {
            return "The number of items read from persistence";
        }
        if ("ItemsWrittenToPersistence".equals(info.getName())) {
            return "The number of items written to persistence";
        }
        return super.getDescription(info);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy