001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.messaging;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.core.api.config.property.ConfigContext;
020import org.kuali.rice.ksb.messaging.service.MessageQueueService;
021import org.kuali.rice.ksb.service.KSBServiceLocator;
022import org.kuali.rice.ksb.util.KSBConstants;
023import org.springframework.transaction.TransactionStatus;
024import org.springframework.transaction.support.TransactionCallback;
025
026/**
027 * Fetches messages from the db. Marks as 'R'. Gives messages to ThreadPool for execution
028 * 
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 * 
031 */
032public class MessageFetcher implements Runnable {
033
034    private static final Logger LOG = Logger.getLogger(MessageFetcher.class);
035
036    private Integer maxMessages;
037    private Long routeQueueId;
038
039    public MessageFetcher(Integer maxMessages) {
040        this.maxMessages = maxMessages;
041    }
042
043    public MessageFetcher(Long routeQueueId) {
044        this.routeQueueId = routeQueueId;
045    }
046
047    public void run() {
048        if (ConfigContext.getCurrentContextConfig().getBooleanProperty(KSBConstants.Config.MESSAGE_PERSISTENCE, false)) {
049                try {
050                        requeueDocument();
051                        requeueMessages();
052                } catch (Throwable t) {
053                        LOG.error("Failed to fetch messages.", t);
054                }
055        }
056    }
057
058    private void requeueMessages() {
059        if (this.routeQueueId == null) {
060            try {
061                for (final PersistedMessageBO message : getRouteQueueService().getNextDocuments(maxMessages)) {
062                    markEnrouteAndSaveMessage(message);
063                    executeMessage(message);
064                }
065            } catch (Throwable t) {
066                LOG.error("Failed to fetch or process some messages during requeueMessages", t);
067            }
068        }
069    }
070
071    private void requeueDocument() {
072        try {
073            if (this.routeQueueId != null) {
074                PersistedMessageBO message = getRouteQueueService().findByRouteQueueId(this.routeQueueId);
075                message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING);
076                getRouteQueueService().save(message);
077                executeMessage(message);
078            }
079        } catch (Throwable t) {
080            LOG.error("Failed to fetch or process some messages during requeueDocument", t);
081        }
082    }
083
084    private void executeMessage(PersistedMessageBO message) {
085        try {
086            KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message));
087        } catch (Throwable t) {
088            LOG.error("Failed to place message " + message + " in thread pool for execution", t);
089        }
090    }
091
092    private void markEnrouteAndSaveMessage(final PersistedMessageBO message) {
093        try {
094            KSBServiceLocator.getTransactionTemplate().execute(new TransactionCallback() {
095                public Object doInTransaction(TransactionStatus status) {
096                    message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING);
097                    getRouteQueueService().save(message);
098                    return null;
099                }
100            });
101        } catch (Throwable t) {
102            LOG.error("Caught error attempting to mark message " + message + " as R", t);
103        }
104    }
105
106    private MessageQueueService getRouteQueueService() {
107        return KSBServiceLocator.getMessageQueueService();
108    }
109
110}