Coverage Report - net.admin4j.ui.servlets.ThreadMonitorStartupServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ThreadMonitorStartupServlet
50%
16/32
50%
4/8
5
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.ui.servlets;
 15  
 
 16  
 import javax.servlet.ServletConfig;
 17  
 import javax.servlet.ServletException;
 18  
 
 19  
 import net.admin4j.config.Admin4JConfiguration;
 20  
 import net.admin4j.monitor.BlockedThreadDetector;
 21  
 import net.admin4j.util.Daemon;
 22  
 import net.admin4j.util.ServletUtils;
 23  
 import net.admin4j.util.notify.NotifierUtils;
 24  
 
 25  
 
 26  
 /**
 27  
  * This servlet will start a thread monitor that will notify administrators of threading issues.
 28  
  * <p>Init parameters for this servlet are as follows:</p>
 29  
  * <li>sleep.interval.millis -- Optional.  Default 30000 (30 sec).  Amount of time in millis monitor will sleep between checks.</li>
 30  
  * <li>max.blocked.threads -- Optional.  Default 2. Number of blocked threads monitor will tolerate before notification.</li>
 31  
  * <li>notifier -- Required.  Handles admin notification.  See documentation for the Notifier you're using
 32  
  * for any additional configuration requirements.  For example, 'net.admin4j.util.notify.EmailNotifier'.</li>
 33  
  * @author D. Ashmore
 34  
  * @since 1.0
 35  
  */
 36  3
 public class ThreadMonitorStartupServlet extends Admin4JServlet {
 37  
 
 38  
         private static final long serialVersionUID = 591261515058479941L;
 39  
         public static final String PUBLIC_HANDLE="thread";
 40  
         
 41  
         @SuppressWarnings("unused")
 42  3
     private static Daemon threadMonitorDaemon = null;
 43  
         
 44  
         @Override
 45  
         public void init(ServletConfig config) throws ServletException {
 46  3
             super.init(config);
 47  
                 try {                        
 48  3
                         long sleepIntervalMillis = BlockedThreadDetector.DEFAULT_SLEEP_INTERVAL;
 49  3
                         int nbrBlockedThreads = BlockedThreadDetector.DEFAULT_BLOCKED_THREAD_THRESHOLD;
 50  
                         
 51  3
                         String sleepIntervalMillisStr = ServletUtils.getConfigurationSetting(
 52  
                     new String[]{PUBLIC_HANDLE + ".sleep.interval.millis", 
 53  
                     "sleep.interval.millis"}, config);
 54  3
                         if (sleepIntervalMillisStr != null) {
 55  
                                 try {
 56  0
                     sleepIntervalMillis = Long.parseLong(sleepIntervalMillisStr);
 57  0
                 } catch (Exception e) {
 58  0
                     sleepIntervalMillis = BlockedThreadDetector.DEFAULT_SLEEP_INTERVAL;
 59  0
                     this.logger.error("Invalid sleep.interval.millis parameter for ThreadMonitorStartupServlet.  Default used.  sleep.interval.millis="+ sleepIntervalMillisStr, 
 60  
                             e);
 61  0
                 }
 62  
                         }
 63  3
                         else if ( Admin4JConfiguration.getThreadSleepIntervalMillis() != null ) {
 64  0
                             sleepIntervalMillis = Admin4JConfiguration.getThreadSleepIntervalMillis();
 65  
             }
 66  
                         
 67  3
                         String blockedThreadThresholdStr = ServletUtils.getConfigurationSetting(
 68  
                     new String[]{PUBLIC_HANDLE + ".max.blocked.threads", 
 69  
                     "max.blocked.threads"}, config);
 70  3
                         if (blockedThreadThresholdStr != null) {
 71  
                                 try {
 72  0
                     nbrBlockedThreads = Integer.parseInt(blockedThreadThresholdStr);
 73  0
                 } catch (Exception e) {
 74  0
                     nbrBlockedThreads = BlockedThreadDetector.DEFAULT_BLOCKED_THREAD_THRESHOLD;
 75  0
                     this.logger.error("Invalid max.blocked.threads parameter for ThreadMonitorStartupServlet.  Default used.  max.blocked.threads="+ blockedThreadThresholdStr, 
 76  
                             e);
 77  0
                 }
 78  
                         }
 79  3
                         else if ( Admin4JConfiguration.getThreadMaxBlockedThreads() != null ) {
 80  0
                             nbrBlockedThreads = Admin4JConfiguration.getThreadMaxBlockedThreads();
 81  
             }
 82  
                         
 83  
                         
 84  
                         
 85  3
                         String notifierClassName = config.getInitParameter("notifier");
 86  3
                         BlockedThreadDetector blockedThreadDetector = new BlockedThreadDetector(
 87  
                                 NotifierUtils.configure(config, notifierClassName), 
 88  
                                 nbrBlockedThreads);
 89  3
                         threadMonitorDaemon = new Daemon( blockedThreadDetector, "Admin4j Blocked Thread Detector", sleepIntervalMillis);
 90  
                 }
 91  0
                 catch (Throwable t) {
 92  0
                         logger.error("Error in boot sequence", t);
 93  0
                         throw new ServletException(t);
 94  3
                 }
 95  3
         }
 96  
         
 97  
     /* (non-Javadoc)
 98  
      * @see net.admin4j.ui.servlets.Admin4JServlet#hasDisplay()
 99  
      */
 100  
     @Override
 101  
     public boolean hasDisplay() {
 102  0
         return false;
 103  
     }
 104  
 }