Coverage Report - org.apache.camel.processor.BatchProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
BatchProcessor
69% 
83% 
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.processor;
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.Iterator;
 21  
 
 22  
 import org.apache.camel.Endpoint;
 23  
 import org.apache.camel.Exchange;
 24  
 import org.apache.camel.PollingConsumer;
 25  
 import org.apache.camel.Processor;
 26  
 import org.apache.camel.impl.LoggingExceptionHandler;
 27  
 import org.apache.camel.impl.ServiceSupport;
 28  
 import org.apache.camel.spi.ExceptionHandler;
 29  
 import org.apache.camel.util.ServiceHelper;
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 
 33  
 /**
 34  
  * A base class for any kind of {@link Processor} which implements some kind of
 35  
  * batch processing.
 36  
  * 
 37  
  * @version $Revision: 1.1 $
 38  
  */
 39  
 public class BatchProcessor extends ServiceSupport implements Runnable {
 40  3
     private static final transient Log LOG = LogFactory.getLog(Resequencer.class);
 41  
     private Endpoint endpoint;
 42  
     private Processor processor;
 43  
     private Collection<Exchange> collection;
 44  6
     private long batchTimeout = 1000L;
 45  6
     private int batchSize = 100;
 46  
     private PollingConsumer consumer;
 47  
     private ExceptionHandler exceptionHandler;
 48  
 
 49  6
     public BatchProcessor(Endpoint endpoint, Processor processor, Collection<Exchange> collection) {
 50  6
         this.endpoint = endpoint;
 51  6
         this.processor = processor;
 52  6
         this.collection = collection;
 53  6
     }
 54  
 
 55  
     @Override
 56  
     public String toString() {
 57  0
         return "BatchProcessor[to: " + processor + "]";
 58  
     }
 59  
 
 60  
     public void run() {
 61  6
         LOG.debug("Starting thread for " + this);
 62  15
         while (!isStopped() && !isStopping()) {
 63  
             try {
 64  9
                 processBatch();
 65  0
             } catch (Exception e) {
 66  0
                 getExceptionHandler().handleException(e);
 67  9
             }
 68  0
         }
 69  6
         collection.clear();
 70  6
     }
 71  
 
 72  
     // Properties
 73  
     // -------------------------------------------------------------------------
 74  
     public ExceptionHandler getExceptionHandler() {
 75  0
         if (exceptionHandler == null) {
 76  0
             exceptionHandler = new LoggingExceptionHandler(getClass());
 77  
         }
 78  0
         return exceptionHandler;
 79  
     }
 80  
 
 81  
     public void setExceptionHandler(ExceptionHandler exceptionHandler) {
 82  0
         this.exceptionHandler = exceptionHandler;
 83  0
     }
 84  
 
 85  
     public int getBatchSize() {
 86  0
         return batchSize;
 87  
     }
 88  
 
 89  
     public void setBatchSize(int batchSize) {
 90  0
         this.batchSize = batchSize;
 91  0
     }
 92  
 
 93  
     public long getBatchTimeout() {
 94  0
         return batchTimeout;
 95  
     }
 96  
 
 97  
     public void setBatchTimeout(long batchTimeout) {
 98  0
         this.batchTimeout = batchTimeout;
 99  0
     }
 100  
 
 101  
     public Endpoint getEndpoint() {
 102  0
         return endpoint;
 103  
     }
 104  
 
 105  
     public Processor getProcessor() {
 106  12
         return processor;
 107  
     }
 108  
 
 109  
     /**
 110  
      * A transactional method to process a batch of messages up to a timeout
 111  
      * period or number of messages reached.
 112  
      */
 113  
     protected synchronized void processBatch() throws Exception {
 114  9
         long start = System.currentTimeMillis();
 115  9
         long end = start + batchTimeout;
 116  321
         for (int i = 0; i < batchSize; i++) {
 117  318
             long timeout = end - System.currentTimeMillis();
 118  
 
 119  318
             Exchange exchange = consumer.receive(timeout);
 120  318
             if (exchange == null) {
 121  6
                 break;
 122  
             }
 123  312
             collection.add(exchange);
 124  
         }
 125  
 
 126  9
         if (LOG.isDebugEnabled()) {
 127  0
             LOG.debug("Finsihed batch size: " + batchSize + " timeout: " + batchTimeout + " so sending set: "
 128  
                       + collection);
 129  
         }
 130  
 
 131  
         // lets send the batch
 132  9
         Iterator<Exchange> iter = collection.iterator();
 133  24
         while (iter.hasNext()) {
 134  15
             Exchange exchange = iter.next();
 135  15
             iter.remove();
 136  15
             processExchange(exchange);
 137  15
         }
 138  9
     }
 139  
 
 140  
     /**
 141  
      * Strategy Method to process an exchange in the batch. This method allows
 142  
      * derived classes to perform custom processing before or after an
 143  
      * individual exchange is processed
 144  
      */
 145  
     protected void processExchange(Exchange exchange) throws Exception {
 146  15
         processor.process(exchange);
 147  15
     }
 148  
 
 149  
     protected void doStart() throws Exception {
 150  6
         consumer = endpoint.createPollingConsumer();
 151  
 
 152  6
         ServiceHelper.startServices(processor, consumer);
 153  
 
 154  6
         Thread thread = new Thread(this, this + " Polling Thread");
 155  6
         thread.start();
 156  6
     }
 157  
 
 158  
     protected void doStop() throws Exception {
 159  6
         ServiceHelper.stopServices(consumer, processor);
 160  6
         collection.clear();
 161  6
     }
 162  
 
 163  
     protected Collection<Exchange> getCollection() {
 164  0
         return collection;
 165  
     }
 166  
 }