001/**
002 * Copyright 2005-2017 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.quartz;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.ksb.messaging.MessageServiceInvoker;
020import org.kuali.rice.ksb.messaging.PersistedMessageBO;
021import org.kuali.rice.ksb.messaging.threadpool.KSBThreadPool;
022import org.kuali.rice.ksb.service.KSBServiceLocator;
023import org.kuali.rice.ksb.util.KSBConstants;
024import org.quartz.Job;
025import org.quartz.JobExecutionContext;
026import org.quartz.JobExecutionException;
027
028import 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 */
038public 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        message.setLockVerNbr(null);
051            message = KSBServiceLocator.getMessageQueueService().save(message);
052            KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message));
053        } catch (Throwable t) {
054            LOG.error("Caught throwable attempting to process message in exception messaging queue.", t);
055            throw new JobExecutionException(new Exception(t));
056        }
057    }
058}