Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.thread » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.concurrent.impl.thread;
   18   
   19   import java.util.ArrayList;
   20   import java.util.List;
   21   
   22   import org.apache.geronimo.concurrent.thread.BasicManagedThreadFactory;
   23   import org.apache.geronimo.concurrent.thread.ManagedThread;
   24   
   25   public class GeronimoManagedThreadFactory extends BasicManagedThreadFactory {
   26       
   27       // gbean that manages this object
   28       private ManagedThreadFactoryGBean factoryGBean;
   29           
   30       private HungTaskMonitor hungTaskMonitor;
   31       private long hungTaskMonitorFrequency = 1000 * 60; // 1 minute
   32       
   33       public GeronimoManagedThreadFactory(ManagedThreadFactoryGBean gbean) {                                       
   34           this.factoryGBean = gbean;
   35       }
   36       
   37       public void setHungTaskMonitorFrequency(long newFrequency) {
   38           if (newFrequency <= 0) {
   39               throw new IllegalArgumentException("Frequency must be greater than 0");
   40           }
   41           this.hungTaskMonitorFrequency = newFrequency;
   42       }
   43       
   44       public long getHungTaskMonitorFrequency() {
   45           return this.hungTaskMonitorFrequency;
   46       }
   47       
   48       @Override
   49       protected ManagedThread createThread(ThreadGroup group, Runnable runnable, String name) {               
   50           GeronimoManagedThread managedThread =  
   51               new GeronimoManagedThread(group, runnable, name, this);
   52           
   53           // add gbean for the thread
   54           this.factoryGBean.addThreadGBean(managedThread);
   55   
   56           startHungTaskMonitor();
   57           
   58           return managedThread;
   59       }
   60       
   61       @Override
   62       public void threadStopped(Thread thread) {
   63           super.threadStopped(thread);
   64           
   65           // remove gbean fro the thread
   66           GeronimoManagedThread managedThread = (GeronimoManagedThread)thread;
   67           this.factoryGBean.removeThreadGBean(managedThread);
   68       }
   69       
   70       public String[] getThreads() {
   71           return getThreads(getThreadList());
   72       }
   73       
   74       public String[] getHungTaskThreads() {
   75           return getHungTaskThreads(getThreadList());
   76       }
   77       
   78       public static String[] getThreads(List<ManagedThread> threadList) {
   79           int size = threadList.size();
   80           String [] threadNames = new String[size];
   81           for (int i = 0; i < size; i++) {
   82               GeronimoManagedThread thread = (GeronimoManagedThread)threadList.get(i);
   83               threadNames[i] = thread.getGbean().getObjectName();
   84           }
   85           return threadNames;        
   86       }
   87       
   88       public static String[] getHungTaskThreads(List<ManagedThread> threadList) {
   89           int size = threadList.size();
   90           List<String> hungTaskThreads = new ArrayList<String>();
   91           for (int i = 0; i < size; i++) {
   92               GeronimoManagedThread thread = (GeronimoManagedThread)threadList.get(i);
   93               if (thread.isTaskHung()) {
   94                   hungTaskThreads.add(thread.getGbean().getObjectName());
   95               }
   96           }
   97           return hungTaskThreads.toArray(new String[hungTaskThreads.size()]);
   98       }
   99       
  100       private synchronized void startHungTaskMonitor() {
  101           if (this.hungTaskMonitor == null) {
  102               this.hungTaskMonitor = new HungTaskMonitor();
  103               this.hungTaskMonitor.setFrequency(this.hungTaskMonitorFrequency);
  104               Thread thread = new Thread(this.threadGroup, 
  105                                          this.hungTaskMonitor, 
  106                                          "UpdateTaskStateThread");
  107               thread.start();
  108           }
  109       }
  110       
  111       private class HungTaskMonitor implements Runnable {
  112           private long frequency = 1000 * 60; // 1 minute default
  113           private boolean done = false;
  114           
  115           public void setFrequency(long newFrequency) {
  116               frequency = newFrequency;
  117           }        
  118           
  119           public long getFrequency() {
  120               return frequency;
  121           }
  122           
  123           public void stop() {
  124               done = true;
  125           }
  126   
  127           public void run() {
  128               while (!done) {
  129                   try {
  130                       Thread.sleep(frequency);
  131                   } catch (InterruptedException e) {
  132                       break;
  133                   }
  134                   updateStatus();
  135               }
  136           }
  137       }
  138       
  139   }

Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.thread » [javadoc | source]