Coverage Report - org.apache.camel.bam.processor.ActivityMonitorEngine
 
Classes in this File Line Coverage Branch Coverage Complexity
ActivityMonitorEngine
68% 
86% 
0
 
 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.camel.bam.processor;
 18  
 
 19  
 import java.util.Date;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.persistence.EntityManager;
 23  
 import javax.persistence.LockModeType;
 24  
 import javax.persistence.PersistenceException;
 25  
 
 26  
 import org.apache.camel.bam.model.ActivityState;
 27  
 import org.apache.camel.bam.rules.ProcessRules;
 28  
 import org.apache.camel.impl.ServiceSupport;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 import org.springframework.orm.jpa.JpaCallback;
 33  
 import org.springframework.orm.jpa.JpaTemplate;
 34  
 import org.springframework.transaction.TransactionStatus;
 35  
 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
 36  
 import org.springframework.transaction.support.TransactionTemplate;
 37  
 
 38  
 /**
 39  
  * A timer engine to monitor for expired activities and perform whatever actions
 40  
  * are required.
 41  
  * 
 42  
  * @version $Revision: $
 43  
  */
 44  22
 public class ActivityMonitorEngine extends ServiceSupport implements Runnable {
 45  2
     private static final Log LOG = LogFactory.getLog(ActivityMonitorEngine.class);
 46  
     private JpaTemplate template;
 47  
     private TransactionTemplate transactionTemplate;
 48  
     private ProcessRules rules;
 49  
     private int escalateLevel;
 50  2
     private long windowMillis = 1000L;
 51  
     private Thread thread;
 52  
     private boolean useLocking;
 53  
 
 54  2
     public ActivityMonitorEngine(JpaTemplate template, TransactionTemplate transactionTemplate, ProcessRules rules) {
 55  2
         this.template = template;
 56  2
         this.transactionTemplate = transactionTemplate;
 57  2
         this.rules = rules;
 58  2
     }
 59  
 
 60  
     public boolean isUseLocking() {
 61  2
         return useLocking;
 62  
     }
 63  
 
 64  
     public void setUseLocking(boolean useLocking) {
 65  0
         this.useLocking = useLocking;
 66  0
     }
 67  
 
 68  
     public void run() {
 69  2
         LOG.debug("Starting to poll for timeout events");
 70  
 
 71  9
         while (!isStopped()) {
 72  
             try {
 73  9
                 long now = System.currentTimeMillis();
 74  9
                 long nextPoll = now + windowMillis;
 75  9
                 final Date timeNow = new Date(now);
 76  
 
 77  9
                 transactionTemplate.execute(new TransactionCallbackWithoutResult() {
 78  9
                     protected void doInTransactionWithoutResult(TransactionStatus status) {
 79  9
                         List<ActivityState> list = template.find("select x from " + ActivityState.class.getName() + " x where x.escalationLevel = ?1 and x.timeOverdue < ?2", escalateLevel, timeNow);
 80  9
                         for (ActivityState activityState : list) {
 81  2
                             fireExpiredEvent(activityState);
 82  2
                         }
 83  9
                     }
 84  
                 });
 85  
 
 86  9
                 long timeToSleep = nextPoll - System.currentTimeMillis();
 87  9
                 if (timeToSleep > 0) {
 88  9
                     if (LOG.isDebugEnabled()) {
 89  0
                         LOG.debug("Sleeping for " + timeToSleep + " millis");
 90  
                     }
 91  
                     try {
 92  9
                         Thread.sleep(timeToSleep);
 93  0
                     } catch (InterruptedException e) {
 94  0
                         LOG.debug("Caught: " + e, e);
 95  7
                     }
 96  
                 }
 97  0
             } catch (Exception e) {
 98  0
                 LOG.error("Caught: " + e, e);
 99  7
             }
 100  0
         }
 101  0
     }
 102  
 
 103  
     protected void fireExpiredEvent(final ActivityState activityState) {
 104  2
         if (LOG.isDebugEnabled()) {
 105  0
             LOG.debug("Trying to fire expiration of: " + activityState);
 106  
         }
 107  
 
 108  2
         template.execute(new JpaCallback() {
 109  2
             public Object doInJpa(EntityManager entityManager) throws PersistenceException {
 110  
                 // lets try lock the object first
 111  2
                 if (isUseLocking()) {
 112  0
                     LOG.info("Attempting to lock: " + activityState);
 113  0
                     entityManager.lock(activityState, LockModeType.WRITE);
 114  0
                     LOG.info("Grabbed lock: " + activityState);
 115  
                 }
 116  
 
 117  
                 try {
 118  2
                     rules.processExpired(activityState);
 119  0
                 } catch (Exception e) {
 120  0
                     LOG.error("Failed to process expiration of: " + activityState + ". Reason: " + e, e);
 121  2
                 }
 122  2
                 activityState.setEscalationLevel(escalateLevel + 1);
 123  2
                 return null;
 124  
             }
 125  
         });
 126  2
     }
 127  
 
 128  
     protected void doStart() throws Exception {
 129  2
         rules.start();
 130  2
         thread = new Thread(this, "ActivityMonitorEngine");
 131  2
         thread.start();
 132  2
     }
 133  
 
 134  
     protected void doStop() throws Exception {
 135  0
         if (thread != null) {
 136  0
             thread = null;
 137  
         }
 138  0
         rules.stop();
 139  0
     }
 140  
 }