Coverage Report - net.admin4j.timer.QuartzPerformanceMonitoringJobListener
 
Classes in this File Line Coverage Branch Coverage Complexity
QuartzPerformanceMonitoringJobListener
86%
25/29
50%
1/2
1.375
 
 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.timer;
 15  
 
 16  
 import net.admin4j.util.Admin4jRuntimeException;
 17  
 import net.admin4j.util.ExpiringCache;
 18  
 
 19  
 import org.quartz.JobExecutionContext;
 20  
 import org.quartz.JobExecutionException;
 21  
 import org.quartz.JobListener;
 22  
 import org.slf4j.Logger;
 23  
 import org.slf4j.LoggerFactory;
 24  
 
 25  
 /**
 26  
  * Monitors performance for Quartz batch jobs.
 27  
  * 
 28  
  * <p>To install, place the following line in your quartz.properties file.</p>
 29  
  * <code>org.quartz.jobListener.Admin4JPerformanceMonitor.class = net.admin4j.timer.QuartzPerformanceMonitoringJobListener</code>
 30  
  * @author D. Ashmore
 31  
  * @since 1.0
 32  
  */
 33  3
 public class QuartzPerformanceMonitoringJobListener implements JobListener {
 34  
     
 35  3
     private static final ExpiringCache TIMER_CACHE = new ExpiringCache(60000 * 60 * 24, 60000 * 60 * 24);
 36  3
     private static final Logger logger = LoggerFactory.getLogger(QuartzPerformanceMonitoringJobListener.class);
 37  3
     private String name = "Admin4JBatchJobPerformanceMonitoring";
 38  
 
 39  
     public String getName() {
 40  3
         return name;
 41  
     }
 42  
     
 43  
     public void setName(String name) {
 44  3
         this.name = name;
 45  3
     }
 46  
 
 47  
     public void jobExecutionVetoed(JobExecutionContext jobContext) {
 48  3
         this.timerStop(jobContext);
 49  
 
 50  3
     }
 51  
 
 52  
     public void jobToBeExecuted(JobExecutionContext jobContext) {
 53  
         try {
 54  6
             String contextKey = this.createContextKey(jobContext);
 55  6
             String timerLabel = this.createTimerLabel(jobContext);
 56  6
             TaskTimer perfMonitor = TaskTimerFactory.start(timerLabel);
 57  6
             TIMER_CACHE.put(contextKey, perfMonitor);
 58  
         }
 59  0
         catch (Throwable t) {
 60  0
             logger.error("Error starting performance time on Quartz Batch job", 
 61  
                     new Admin4jRuntimeException(t).addContextValue("jobContext", jobContext));
 62  6
         }
 63  6
     }
 64  
 
 65  
     public void jobWasExecuted(JobExecutionContext jobContext,
 66  
             JobExecutionException jobException) {
 67  3
         this.timerStop(jobContext);
 68  
 
 69  3
     }
 70  
     
 71  
     private void timerStop(JobExecutionContext jobContext) {
 72  
         try {
 73  6
             String contextKey = this.createContextKey(jobContext);
 74  6
             TaskTimer perfMonitor = (TaskTimer)TIMER_CACHE.get(contextKey);;
 75  6
             if (perfMonitor != null) {
 76  6
                 perfMonitor.stop();
 77  
             }
 78  
         }
 79  0
         catch (Throwable t) {
 80  0
             logger.error("Error starting performance time on Quartz Batch job", 
 81  
                     new Admin4jRuntimeException(t).addContextValue("jobContext", jobContext));
 82  6
         }
 83  6
     }
 84  
     
 85  
     private String createContextKey(JobExecutionContext jobContext) {
 86  12
         return createTimerLabel(jobContext) + System.identityHashCode(jobContext);
 87  
     }
 88  
     
 89  
     private String createTimerLabel(JobExecutionContext jobContext) {
 90  18
         return "batch-" + jobContext.getJobDetail().getFullName();
 91  
     }
 92  
 
 93  
 }