alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (ThreadPoolStatsImpl.java)

This example Glassfish source code file (ThreadPoolStatsImpl.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

boundedrangestatistic, boundedrangestatisticimpl, boundedrangestatisticimpl, countstatistic, countstatisticimpl, description, description, log, logging, managedattribute, managedattribute, rangestatistic, string, string, threadpoolstatsimpl, total

The Glassfish ThreadPoolStatsImpl.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package org.glassfish.enterprise.iiop.util;

import com.sun.corba.ee.spi.orbutil.threadpool.NoSuchWorkQueueException;
import org.glassfish.external.statistics.CountStatistic;
import org.glassfish.external.statistics.BoundedRangeStatistic;
import org.glassfish.external.statistics.RangeStatistic;

import org.glassfish.external.statistics.impl.BoundedRangeStatisticImpl;
import org.glassfish.external.statistics.impl.CountStatisticImpl;
import com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool;
import com.sun.corba.ee.spi.orbutil.threadpool.WorkQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.gmbal.ManagedAttribute;
import org.glassfish.gmbal.ManagedObject;
import org.glassfish.gmbal.Description;

/**
 * This is the implementation for the ThreadPoolStats
 * and provides the implementation required to get the statistics
 * for a threadpool
 *
 * @author Pramod Gopinath
 */
@ManagedObject
@Description("The implementation for the ThreadPoolStats")
public class ThreadPoolStatsImpl
        extends ORBCommonStatsImpl
        implements ThreadPoolStats {

    private ThreadPool threadPool;
    private WorkQueue workQueue;
    private String threadPoolName;
    private String workQueueName;
    private CountStatisticImpl numberOfBusyThreads;
    private CountStatisticImpl numberOfAvailableThreads;
    private BoundedRangeStatisticImpl currentNumberOfThreads;
    private BoundedRangeStatisticImpl averageWorkCompletionTime;
    private CountStatisticImpl totalWorkItemsAdded;
    private BoundedRangeStatisticImpl numberOfWorkItemsInQueue;
    private BoundedRangeStatisticImpl averageTimeInQueue;
    private static final String stringNumberOfBusyThreads =
            MonitoringConstants.THREADPOOL_NUMBER_OF_BUSY_THREADS;
    private static final String stringNumberOfAvailableThreads =
            MonitoringConstants.THREADPOOL_NUMBER_OF_AVAILABLE_THREADS;
    private static final String stringCurrentNumberOfThreads =
            MonitoringConstants.THREADPOOL_CURRENT_NUMBER_OF_THREADS;
    private static final String stringAverageWorkCompletionTime =
            MonitoringConstants.THREADPOOL_AVERAGE_WORK_COMPLETION_TIME;
    private static final String stringTotalWorkItemsAdded =
            MonitoringConstants.WORKQUEUE_TOTAL_WORK_ITEMS_ADDED;
    private static final String stringNumberOfWorkItemsInQueue =
            MonitoringConstants.WORKQUEUE_WORK_ITEMS_IN_QUEUE;
    private static final String stringAverageTimeInQueue =
            MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE;

    public ThreadPoolStatsImpl(ThreadPool threadPool) throws NoSuchWorkQueueException {
        this.threadPool = threadPool;
        this.threadPoolName = threadPool.getName();

        getWorkQueueForThreadPool();

        initializeStats();
    }

    private void getWorkQueueForThreadPool() {
        try {
            workQueue = threadPool.getWorkQueue(0);
            workQueueName = workQueue.getName();
        } catch (NoSuchWorkQueueException ex) {

            Logger.getLogger(workQueueName).log(Level.SEVERE, workQueueName);
            throw new RuntimeException(ex);

        }
    }

    private void initializeStats() throws NoSuchWorkQueueException {
        super.initialize("org.glassfish.enterprise.iiop.util.ThreadPoolStats");

        final long time = System.currentTimeMillis();

        numberOfBusyThreads =
                new CountStatisticImpl(threadPool.numberOfBusyThreads(), stringNumberOfBusyThreads, "COUNT",
                threadPool.getWorkQueue(0).toString(),
                time, time);

        numberOfAvailableThreads =
                new CountStatisticImpl(
                threadPool.numberOfAvailableThreads(), stringNumberOfAvailableThreads, "count",
                threadPool.getWorkQueue(0).toString(),
                time, time);

        currentNumberOfThreads =
                new BoundedRangeStatisticImpl(
                threadPool.currentNumberOfThreads(), threadPool.maximumNumberOfThreads(), threadPool.minimumNumberOfThreads(), java.lang.Long.MAX_VALUE, 0,
                stringCurrentNumberOfThreads, "count",
                threadPool.getWorkQueue(0).toString(),
                time, time);

        averageWorkCompletionTime =
                new BoundedRangeStatisticImpl(
                threadPool.averageWorkCompletionTime(), 0, 0, java.lang.Long.MAX_VALUE, 0,
                stringAverageWorkCompletionTime, "Milliseconds",
                threadPool.getWorkQueue(0).toString(),
                time, time);

        // WorkQueue workItems = threadPool.getWorkQueue(0);

        totalWorkItemsAdded =
                new CountStatisticImpl(
                workQueue.totalWorkItemsAdded(), stringTotalWorkItemsAdded, "count",
                workQueue.getName(),
                time, time);

        numberOfWorkItemsInQueue =
                new BoundedRangeStatisticImpl(
                workQueue.workItemsInQueue(), 0, 0, java.lang.Long.MAX_VALUE, 0,
                stringNumberOfWorkItemsInQueue, "count",
                workQueue.getName(),
                time, time);

        averageTimeInQueue =
                new BoundedRangeStatisticImpl(
                workQueue.averageTimeInQueue(), 0, 0, java.lang.Long.MAX_VALUE, 0,
                stringAverageTimeInQueue, "Milliseconds",
                workQueue.getName(),
                time, time);

    }

    @ManagedAttribute(id = "currentbusythreads")
    @Description("Total number of busy threads")
    public synchronized CountStatistic getNumberOfBusyThreads() {
        int numBusyThreads = threadPool.numberOfBusyThreads();
        numberOfBusyThreads.setCount(numBusyThreads);
        return (CountStatistic) numberOfBusyThreads.getStatistic();
    }

    @ManagedAttribute
    @Description("Total number of available threads")
    public synchronized CountStatistic getNumberOfAvailableThreads() {
        long numAvailableThreads = (long) threadPool.numberOfAvailableThreads();

        numberOfAvailableThreads.setCount(numAvailableThreads);

        return (CountStatistic) numberOfAvailableThreads.getStatistic();
    }

    @ManagedAttribute
    @Description("Total number of current threads")
    public synchronized BoundedRangeStatistic getCurrentNumberOfThreads() {
        int numCurrentThreads = threadPool.currentNumberOfThreads();
        currentNumberOfThreads.setCurrent(numCurrentThreads);
        return (BoundedRangeStatistic) currentNumberOfThreads.getStatistic();
    }

    @ManagedAttribute
    @Description("Average time to complete work")
    public synchronized RangeStatistic getAverageWorkCompletionTime() {
        long avgWorkCompletionTime = threadPool.averageWorkCompletionTime();
        averageWorkCompletionTime.setCurrent(avgWorkCompletionTime);
        return (RangeStatistic) averageWorkCompletionTime.getStatistic();
    }

    @ManagedAttribute
    @Description("Total number of work items added to the queue")
    public synchronized CountStatistic getTotalWorkItemsAdded() {
        long totWorkItemsAdded = workQueue.totalWorkItemsAdded();
        totalWorkItemsAdded.setCount(totWorkItemsAdded);
        return (CountStatistic) totalWorkItemsAdded.getStatistic();
    }

    @ManagedAttribute
    @Description("Total number of work items in the queue")
    public synchronized BoundedRangeStatistic getNumberOfWorkItemsInQueue() {
        int totWorkItemsInQueue = workQueue.workItemsInQueue();
        numberOfWorkItemsInQueue.setCurrent(totWorkItemsInQueue);
        return (BoundedRangeStatistic) numberOfWorkItemsInQueue.getStatistic();
    }

    @ManagedAttribute
    @Description("Average time in queue")
    public synchronized RangeStatistic getAverageTimeInQueue() {
        long avgTimeInQueue = workQueue.averageTimeInQueue();
        averageTimeInQueue.setCurrent(avgTimeInQueue);

        return (RangeStatistic) averageTimeInQueue.getStatistic();
    }
} //ThreadPoolStatsImpl{ }

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish ThreadPoolStatsImpl.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.