001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.ksb.messaging.quartz;
017    
018    import org.apache.log4j.Logger;
019    import org.kuali.rice.ksb.messaging.MessageServiceInvoker;
020    import org.kuali.rice.ksb.messaging.PersistedMessageBO;
021    import org.kuali.rice.ksb.messaging.threadpool.KSBThreadPool;
022    import org.kuali.rice.ksb.service.KSBServiceLocator;
023    import org.kuali.rice.ksb.util.KSBConstants;
024    import org.quartz.Job;
025    import org.quartz.JobExecutionContext;
026    import org.quartz.JobExecutionException;
027    
028    import java.io.Serializable;
029    
030    
031    /**
032     * Job saves a {@link org.kuali.rice.ksb.messaging.PersistedMessageBO} to the message queue in the state of 'R' and then puts into a
033     * {@link MessageServiceInvoker} for execution in {@link KSBThreadPool}.
034     * 
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     * 
037     */
038    public class MessageServiceExecutorJob implements Job, Serializable {
039    
040        private static final Logger LOG = Logger.getLogger(MessageServiceExecutorJob.class);
041    
042        private static final long serialVersionUID = 6702139047380618522L;
043    
044        public static final String MESSAGE_KEY = "message";
045    
046        public void execute(JobExecutionContext jec) throws JobExecutionException {
047            try {
048                PersistedMessageBO message = (PersistedMessageBO) jec.getJobDetail().getJobDataMap().get(MESSAGE_KEY);
049                message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING);
050                KSBServiceLocator.getMessageQueueService().save(message);
051                KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message));
052            } catch (Throwable t) {
053                LOG.error("Caught throwable attempting to process message in exception messaging queue.", t);
054                throw new JobExecutionException(new Exception(t));
055            }
056        }
057    }