Coverage Report - org.kuali.rice.ksb.messaging.MessageFetcher
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageFetcher
0%
0/42
0%
0/6
1.889
MessageFetcher$1
0%
0/4
N/A
1.889
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.ksb.messaging;
 17  
 
 18  
 import org.apache.log4j.Logger;
 19  
 import org.kuali.rice.ksb.messaging.service.MessageQueueService;
 20  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 21  
 import org.kuali.rice.ksb.util.KSBConstants;
 22  
 import org.springframework.transaction.TransactionStatus;
 23  
 import org.springframework.transaction.support.TransactionCallback;
 24  
 
 25  
 /**
 26  
  * Fetches messages from the db. Marks as 'R'. Gives messages to ThreadPool for execution
 27  
  * 
 28  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 29  
  * 
 30  
  */
 31  0
 public class MessageFetcher implements Runnable {
 32  
 
 33  0
     private static final Logger LOG = Logger.getLogger(MessageFetcher.class);
 34  
 
 35  
     private Integer maxMessages;
 36  
     private Long routeQueueId;
 37  
 
 38  0
     public MessageFetcher(Integer maxMessages) {
 39  0
         this.maxMessages = maxMessages;
 40  0
     }
 41  
 
 42  0
     public MessageFetcher(Long routeQueueId) {
 43  0
         this.routeQueueId = routeQueueId;
 44  0
     }
 45  
 
 46  
     public void run() {
 47  
         try {
 48  0
             requeueDocument();
 49  0
             requeueMessages();
 50  0
         } catch (Throwable t) {
 51  0
             LOG.error("Failed to fetch messages.", t);
 52  0
         }
 53  0
     }
 54  
 
 55  
     private void requeueMessages() {
 56  0
         if (this.routeQueueId == null) {
 57  
             try {
 58  0
                 for (final PersistedMessage message : getRouteQueueService().getNextDocuments(maxMessages)) {
 59  0
                     markEnrouteAndSaveMessage(message);
 60  0
                     executeMessage(message);
 61  
                 }
 62  0
             } catch (Throwable t) {
 63  0
                 LOG.error("Failed to fetch or process some messages during requeueMessages", t);
 64  0
             }
 65  
         }
 66  0
     }
 67  
 
 68  
     private void requeueDocument() {
 69  
         try {
 70  0
             if (this.routeQueueId != null) {
 71  0
                 PersistedMessage message = getRouteQueueService().findByRouteQueueId(this.routeQueueId);
 72  0
                 message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING);
 73  0
                 getRouteQueueService().save(message);
 74  0
                 executeMessage(message);
 75  
             }
 76  0
         } catch (Throwable t) {
 77  0
             LOG.error("Failed to fetch or process some messages during requeueDocument", t);
 78  0
         }
 79  0
     }
 80  
 
 81  
     private void executeMessage(PersistedMessage message) {
 82  
         try {
 83  0
             KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message));
 84  0
         } catch (Throwable t) {
 85  0
             LOG.error("Failed to place message " + message + " in thread pool for execution", t);
 86  0
         }
 87  0
     }
 88  
 
 89  
     private void markEnrouteAndSaveMessage(final PersistedMessage message) {
 90  
         try {
 91  0
             KSBServiceLocator.getTransactionTemplate().execute(new TransactionCallback() {
 92  
                 public Object doInTransaction(TransactionStatus status) {
 93  0
                     message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING);
 94  0
                     getRouteQueueService().save(message);
 95  0
                     return null;
 96  
                 }
 97  
             });
 98  0
         } catch (Throwable t) {
 99  0
             LOG.error("Caught error attempting to mark message " + message + " as R", t);
 100  0
         }
 101  0
     }
 102  
 
 103  
     private MessageQueueService getRouteQueueService() {
 104  0
         return KSBServiceLocator.getRouteQueueService();
 105  
     }
 106  
 
 107  
 }