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.Properties;
   20   
   21   import org.apache.commons.logging.Log;
   22   import org.apache.commons.logging.LogFactory;
   23   import org.apache.geronimo.concurrent.impl.GBeanBuilder;
   24   import org.apache.geronimo.concurrent.impl.NotificationHelper;
   25   import org.apache.geronimo.concurrent.thread.ManagedThread;
   26   import org.apache.geronimo.gbean.AbstractName;
   27   import org.apache.geronimo.gbean.GBeanInfo;
   28   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   29   import org.apache.geronimo.gbean.GBeanLifecycle;
   30   import org.apache.geronimo.kernel.Kernel;
   31   import org.apache.geronimo.management.EventProvider;
   32   import org.apache.geronimo.management.ManagedConstants;
   33   
   34   public class ManagedThreadGBean implements GBeanLifecycle, EventProvider, org.apache.geronimo.management.ManagedThread {
   35   
   36       private final static Log LOG = LogFactory.getLog(ManagedThreadGBean.class);
   37       
   38       public static final GBeanInfo GBEAN_INFO;
   39   
   40       private AbstractName name;
   41       private ManagedThread thread;
   42   
   43       private NotificationHelper notificationHelper;
   44       
   45       public ManagedThreadGBean(Kernel kernel, 
   46                                 ClassLoader classLoader, 
   47                                 AbstractName name) {
   48           this.name = name;
   49           
   50           this.notificationHelper = new NotificationHelper(kernel, name);
   51       }
   52               
   53       protected void sendNotification(ManagedThread.TaskState state) {
   54           if (this.notificationHelper.isNotificationSupported()) {
   55               String type = null;
   56               if (state == ManagedThread.TaskState.CANCELLED) {
   57                   type = ManagedConstants.TASK_CANCELLED_STATE;
   58               } else if (state == ManagedThread.TaskState.RELEASED) {
   59                   type = ManagedConstants.TASK_RELEASED_STATE;                
   60               } else if (state == ManagedThread.TaskState.HUNG) {
   61                   type = ManagedConstants.TASK_HUNG_STATE;
   62               } else {
   63                   // no notifications on other states
   64                   return;
   65               }
   66               
   67               // TODO: 1) might need to properly synchronize to get accurate info
   68               //       2) some info only set on certain events?
   69               
   70               Properties userData = new Properties();
   71               userData.setProperty("managedthread", 
   72                                    getObjectName());            
   73               userData.setProperty("managedthread.threadID", 
   74                                    String.valueOf(getThreadID()));
   75               userData.setProperty("managedthread.threadName", 
   76                                    getThreadName());
   77               userData.setProperty("managedthread.taskRunTime", 
   78                                    String.valueOf(getTaskRunTime()));
   79               String taskDescription = getTaskIdentityDescription();
   80               if (taskDescription != null) {
   81                   userData.setProperty("managedthread.taskIdentityDescription",
   82                                        taskDescription);
   83               }
   84               String taskName = getTaskIdentityName();
   85               if (taskName != null) {
   86                   userData.setProperty("managedthread.taskIdentityName", 
   87                                        taskName);
   88               }
   89                          
   90               this.notificationHelper.sendNotification(type, userData);
   91           }
   92       }
   93       
   94       protected void setManagedThread(ManagedThread thread) {
   95           this.thread = thread;
   96       }
   97       
   98       public void doFail() {
   99           this.thread = null;
  100       }
  101   
  102       public void doStart() throws Exception {     
  103           LOG.debug("Thread gbean started: " + this.thread);
  104       }
  105   
  106       public void doStop() throws Exception {
  107           LOG.debug("Thread gbean stopped: " + this.thread);
  108           this.thread = null;
  109       }
  110       
  111       public boolean cancel() {
  112           return (this.thread == null) ? false : this.thread.cancelTask();
  113       }
  114   
  115       public long getHungTaskThreshold() {
  116           return (this.thread == null) ? -1 : this.thread.getHungTaskThreshold();
  117       }
  118       
  119       public void setHungTaskThreshold(long threshold) {
  120           if (this.thread != null) {
  121               this.thread.setHungTaskThreshold(threshold);
  122           }
  123       }
  124   
  125       public String getTaskIdentityDescription() {
  126           return (this.thread == null) ? null : this.thread.getTaskIdentityDescription();
  127       }
  128   
  129       public String getTaskIdentityDescription(String locale) {
  130           return (this.thread == null) ? null : this.thread.getTaskIdentityDescription(locale);
  131       }
  132   
  133       public String getTaskIdentityName() {
  134           return (this.thread == null) ? null : this.thread.getTaskIdentityName();
  135       }
  136   
  137       public long getTaskRunTime() {
  138           return (this.thread == null) ? 0 : this.thread.getTaskRunTime();
  139       }
  140   
  141       public long getThreadID() {
  142           return (this.thread == null) ? -1 : this.thread.getThreadID();
  143       }
  144   
  145       public String getThreadName() {
  146           return (this.thread == null) ? null : this.thread.getThreadName();
  147       }
  148   
  149       public boolean isTaskCancelled() {
  150           return (this.thread == null) ? false : this.thread.isTaskCancelled();
  151       }
  152   
  153       public boolean isTaskHung() {
  154           return (this.thread == null) ? false : this.thread.isTaskHung();
  155       }
  156   
  157       public AbstractName getName() {
  158           return this.name;
  159       }
  160       
  161       public String getObjectName() {
  162           return this.name.getObjectName().getCanonicalName();
  163       }
  164   
  165       public String[] getEventTypes() {
  166           return new String[] { ManagedConstants.TASK_HUNG_STATE,
  167                                 ManagedConstants.TASK_CANCELLED_STATE,
  168                                 ManagedConstants.TASK_RELEASED_STATE };        
  169       }
  170       
  171       public boolean isEventProvider() {
  172           return true;
  173       }
  174   
  175       public boolean isStateManageable() {
  176           return true;
  177       }
  178   
  179       public boolean isStatisticsProvider() {
  180           return false;
  181       }
  182       
  183       protected void verifyObjectName() {
  184           GBeanBuilder.verifyObjectName(getObjectName(),  
  185                                         ManagedConstants.MANAGED_THREAD, 
  186                                         ManagedConstants.MANAGED_THREAD);
  187       }
  188       
  189       static {
  190           GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ManagedThreadGBean.class, ManagedConstants.MANAGED_THREAD);
  191   
  192           infoFactory.addAttribute("classLoader", ClassLoader.class, false, false);
  193           infoFactory.addAttribute("abstractName", AbstractName.class, false, false);
  194           infoFactory.addAttribute("kernel", Kernel.class, false, false);
  195   
  196           infoFactory.addInterface(org.apache.geronimo.management.ManagedThread.class);
  197   
  198           infoFactory.setConstructor(new String[]{"kernel", 
  199                                                   "classLoader", 
  200                                                   "abstractName"});
  201   
  202           GBEAN_INFO = infoFactory.getBeanInfo();
  203       }
  204   
  205       public static GBeanInfo getGBeanInfo() {
  206           return GBEAN_INFO;
  207       }
  208          
  209   }

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