FileDocCategorySizeDatePackage
CommonWorkManager.javaAPI DocGlassfish v2 API14661Fri May 04 22:34:26 BST 2007com.sun.enterprise.connectors.work

CommonWorkManager

public final class CommonWorkManager extends Object implements MonitorableWorkManager
WorkManager implementation.
author
Binod P.G

Fields Summary
private static javax.resource.spi.work.WorkManager
wm
private ThreadPoolManager
tpm
private ThreadPool
tp
private static final Logger
logger
private boolean
isMonitoringEnabled
private WorkStats
workStats
private com.sun.enterprise.util.i18n.StringManager
localStrings
Constructors Summary
public CommonWorkManager(String threadpoolId)
Private constructor.

param
threadpoolId Id of the thread pool.
throws
ConnectorRuntimeException if thread pool is not accessible

    
                          
        
                                 
        int env = ConnectorRuntime.getRuntime().getEnviron();

        if (env == ConnectorRuntime.SERVER) {
            tpm = S1ASThreadPoolManager.getThreadPoolManager();

            if (threadpoolId == null) {
                tp = tpm.getDefaultThreadPool();
            } else {
                try {
                    tp = tpm.getThreadPool(threadpoolId);
                    logger.info("Got the thread pool for :" + threadpoolId);
                } catch (NoSuchThreadPoolException e) {
                    String msg = localStrings.getString("workmanager.threadpool_not_found");
             
                    logger.log(Level.SEVERE,msg, threadpoolId);
                    throw new ConnectorRuntimeException(e.getMessage());
                }
            }
        }
    
public CommonWorkManager()
Using the default thread pool.

throws
ConnectorRuntimeException if thread pool is not accessible

        this(null);
    
Methods Summary
private java.lang.StringdebugMsg(java.lang.String message)

        String msg = "[Thread " + Thread.currentThread().getName() 
            + "] -- " + message;
        return msg;
    
public voiddoWork(javax.resource.spi.work.Work work)
Executes the work instance.

param
work work instance from resource adapter
throws
WorkException if there is an exception while executing work.

        doWork(work, -1, null, null);
    
public voiddoWork(javax.resource.spi.work.Work work, long startTimeout, javax.resource.spi.work.ExecutionContext execContext, javax.resource.spi.work.WorkListener workListener)
Executes the work instance. The calling thread will wait until the end of work execution.

param
work work instance from resource adapter
param
startTimeout Timeout for the work.
param
execContext Execution context in which the work will be executed.
param
workListener Listener from RA that will listen to work events.
throws
WorkException if there is an exception while executing work.

        
        if (logger.isLoggable(Level.FINEST)) {
            String msg = "doWork for [" + work.toString() + "] START";
            logger.log(Level.FINEST, debugMsg(msg));
        }
       
        WorkCoordinator wc = new WorkCoordinator
            (work, startTimeout, execContext, tp.getAnyWorkQueue(), workListener, 
                            this.workStats);
        wc.submitWork(WorkCoordinator.WAIT_UNTIL_FINISH);
        wc.lock();

        WorkException we = wc.getException();
        if (we != null) {
            throw we;
        }

        if (logger.isLoggable(Level.FINEST)) {
            String msg = "doWork for [" + work.toString() + "] END";
            msg = "doWork for [" + work.toString() + "] END";
            logger.log(Level.FINEST, debugMsg(msg));
        }
    
public longgetCompletedWorkCount()

        return this.workStats.completedWorkCount;
    
public longgetCurrentActiveWorkCount()

        return this.workStats.currentActiveWorkCount;
    
public longgetMaxActiveWorkCount()

        return this.workStats.maxActiveWorkCount;
    
public longgetMaxWaitQueueLength()

        return this.workStats.maxWaitQueueLength;
    
public longgetMaxWorkRequestWaitTime()

        return this.workStats.maxWorkRequestWaitTime;
       
    
public longgetMinActiveWorkCount()

        if (this.workStats.minActiveWorkCount != Long.MAX_VALUE){
            return this.workStats.minActiveWorkCount;
        } else {
            return 0;
        }
    
public longgetMinWaitQueueLength()

        if (this.workStats.minWaitQueueLength != Long.MAX_VALUE){
            return this.workStats.minWaitQueueLength;
        } else {
            return 0;
        }
    
public longgetMinWorkRequestWaitTime()

        return this.workStats.minWorkRequestWaitTime;
    
public longgetRejectedWorkCount()

        return this.workStats.rejectedWorkCount;
    
public longgetSubmittedWorkCount()

        return this.workStats.submittedWorkCount;
    
public longgetWaitQueueLength()

        return this.workStats.currWaitQueueLength;
    
public booleanisMonitoringEnabled()

        return this.isMonitoringEnabled;
    
public voidscheduleWork(javax.resource.spi.work.Work work)
Executes the work instance. Calling thread will continue after scheduling the work

param
work work instance from resource adapter
throws
WorkException if there is an exception while executing work.

        scheduleWork(work, -1, null, null);
        return;
    
public voidscheduleWork(javax.resource.spi.work.Work work, long startTimeout, javax.resource.spi.work.ExecutionContext execContext, javax.resource.spi.work.WorkListener workListener)
Executes the work instance. Calling thread will continue after scheduling the work

param
work work instance from resource adapter
param
startTimeout Timeout for the work.
param
execContext Execution context in which the work will be executed.
param
workListener Listener from RA that will listen to work events.
throws
WorkException if there is an exception while executing work.


        if (logger.isLoggable(Level.FINEST)) {
            String msg = "scheduleWork for [" + work.toString() + "] START";
            logger.log(Level.FINEST, debugMsg(msg));
        }

        WorkCoordinator wc = new WorkCoordinator
            (work, startTimeout, execContext, tp.getAnyWorkQueue(), workListener,
                            this.workStats);
        wc.submitWork(WorkCoordinator.NO_WAIT);
        wc.lock();

        WorkException we = wc.getException();
        if (we != null) {
            throw we;
        }

        if (logger.isLoggable(Level.FINEST)) {
            String msg = "scheduleWork for [" + work.toString() + "] END";
            logger.log(Level.FINEST, debugMsg(msg));
        }
        return;
    
public voidsetMonitoringEnabled(boolean isEnabled)

        this.isMonitoringEnabled = isEnabled;
        if ( this.workStats == null ) {
            this.workStats = new WorkStats();
        }
        //reset WorkStats when monitoring disabled
        if (!isEnabled){
            this.workStats.reset();
        } 
    
public longstartWork(javax.resource.spi.work.Work work)
Executes the work instance. The calling thread will wait until the start of work execution.

param
work work instance from resource adapter
throws
WorkException if there is an exception while executing work.

        //block the current application thread
        //find a thread to run work
        //notify the application thread when done

        return startWork(work, -1, null, null);
    
public longstartWork(javax.resource.spi.work.Work work, long startTimeout, javax.resource.spi.work.ExecutionContext execContext, javax.resource.spi.work.WorkListener workListener)
Executes the work instance. The calling thread will wait until the start of work execution.

param
work work instance from resource adapter
param
startTimeout Timeout for the work.
param
execContext Execution context in which the work will be executed.
param
workListener Listener from RA that will listen to work events.
throws
WorkException if there is an exception while executing work.


        if (logger.isLoggable(Level.FINEST)) {
            String msg = "startWork for [" + work.toString() + "] START";
            logger.log(Level.FINEST, debugMsg(msg));
        }

        long acceptanceTime = System.currentTimeMillis();

        WorkCoordinator wc = new WorkCoordinator
            (work, startTimeout, execContext, tp.getAnyWorkQueue(), workListener,
                            this.workStats);
        wc.submitWork(WorkCoordinator.WAIT_UNTIL_START);
        wc.lock();

        WorkException we = wc.getException();
        if (we != null) {
            throw we;
        }

        if (logger.isLoggable(Level.FINEST)) {
            String msg = "startWork for [" + work.toString() + "] END";
            logger.log(Level.FINEST, debugMsg(msg));
        }
        long startTime = System.currentTimeMillis();

        return (startTime - acceptanceTime);