Coverage Report - org.kuali.rice.ksb.messaging.MessageFetcher
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageFetcher
0%
0/43
0%
0/8
2
MessageFetcher$1
0%
0/4
N/A
2
 
 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.core.api.config.property.ConfigContext;
 20  
 import org.kuali.rice.ksb.messaging.service.MessageQueueService;
 21  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 22  
 import org.kuali.rice.ksb.util.KSBConstants;
 23  
 import org.kuali.rice.ksb.util.KSBConstants.Config;
 24  
 import org.springframework.transaction.TransactionStatus;
 25  
 import org.springframework.transaction.support.TransactionCallback;
 26  
 
 27  
 /**
 28  
  * Fetches messages from the db. Marks as 'R'. Gives messages to ThreadPool for execution
 29  
  * 
 30  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 31  
  * 
 32  
  */
 33  0
 public class MessageFetcher implements Runnable {
 34  
 
 35  0
     private static final Logger LOG = Logger.getLogger(MessageFetcher.class);
 36  
 
 37  
     private Integer maxMessages;
 38  
     private Long routeQueueId;
 39  
 
 40  0
     public MessageFetcher(Integer maxMessages) {
 41  0
         this.maxMessages = maxMessages;
 42  0
     }
 43  
 
 44  0
     public MessageFetcher(Long routeQueueId) {
 45  0
         this.routeQueueId = routeQueueId;
 46  0
     }
 47  
 
 48  
     public void run() {
 49  0
             if (ConfigContext.getCurrentContextConfig().getBooleanProperty(KSBConstants.Config.MESSAGE_PERSISTENCE, false)) {
 50  
                     try {
 51  0
                             requeueDocument();
 52  0
                             requeueMessages();
 53  0
                     } catch (Throwable t) {
 54  0
                             LOG.error("Failed to fetch messages.", t);
 55  0
                     }
 56  
             }
 57  0
     }
 58  
 
 59  
     private void requeueMessages() {
 60  0
         if (this.routeQueueId == null) {
 61  
             try {
 62  0
                 for (final PersistedMessageBO message : getRouteQueueService().getNextDocuments(maxMessages)) {
 63  0
                     markEnrouteAndSaveMessage(message);
 64  0
                     executeMessage(message);
 65  
                 }
 66  0
             } catch (Throwable t) {
 67  0
                 LOG.error("Failed to fetch or process some messages during requeueMessages", t);
 68  0
             }
 69  
         }
 70  0
     }
 71  
 
 72  
     private void requeueDocument() {
 73  
         try {
 74  0
             if (this.routeQueueId != null) {
 75  0
                 PersistedMessageBO message = getRouteQueueService().findByRouteQueueId(this.routeQueueId);
 76  0
                 message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING);
 77  0
                 getRouteQueueService().save(message);
 78  0
                 executeMessage(message);
 79  
             }
 80  0
         } catch (Throwable t) {
 81  0
             LOG.error("Failed to fetch or process some messages during requeueDocument", t);
 82  0
         }
 83  0
     }
 84  
 
 85  
     private void executeMessage(PersistedMessageBO message) {
 86  
         try {
 87  0
             KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message));
 88  0
         } catch (Throwable t) {
 89  0
             LOG.error("Failed to place message " + message + " in thread pool for execution", t);
 90  0
         }
 91  0
     }
 92  
 
 93  
     private void markEnrouteAndSaveMessage(final PersistedMessageBO message) {
 94  
         try {
 95  0
             KSBServiceLocator.getTransactionTemplate().execute(new TransactionCallback() {
 96  
                 public Object doInTransaction(TransactionStatus status) {
 97  0
                     message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING);
 98  0
                     getRouteQueueService().save(message);
 99  0
                     return null;
 100  
                 }
 101  
             });
 102  0
         } catch (Throwable t) {
 103  0
             LOG.error("Caught error attempting to mark message " + message + " as R", t);
 104  0
         }
 105  0
     }
 106  
 
 107  
     private MessageQueueService getRouteQueueService() {
 108  0
         return KSBServiceLocator.getMessageQueueService();
 109  
     }
 110  
 
 111  
 }