001    /**
002     * Copyright 2005-2013 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.threadpool;
017    
018    import org.kuali.rice.ksb.messaging.MessageServiceInvoker;
019    import org.kuali.rice.ksb.messaging.PersistedMessageBO;
020    
021    import java.util.Comparator;
022    import java.util.concurrent.PriorityBlockingQueue;
023    
024    /**
025     * A comparator to put into the {@link PriorityBlockingQueue} used in the {@link KSBThreadPoolImpl}.
026     * 
027     *  Determines execution order by priority and create date.
028     * 
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     *
031     */
032    public class PriorityBlockingQueuePersistedMessageComparator implements Comparator {
033    
034        
035        public int compare(Object arg0, Object arg1) {
036            if (! (arg0 instanceof MessageServiceInvoker) || ! (arg1 instanceof MessageServiceInvoker) ) {
037                return 0;
038            }
039            PersistedMessageBO message0 = ((MessageServiceInvoker)arg0).getMessage();
040            PersistedMessageBO message1 = ((MessageServiceInvoker)arg1).getMessage();
041            
042            if (message0.getQueuePriority() < message1.getQueuePriority()) {
043                return -1;
044            } else if (message0.getQueuePriority() > message1.getQueuePriority()) {
045                return 1;
046            }
047            
048            if (message0.getQueueDate().before(message1.getQueueDate())) {
049                return -1;
050            } else if (message0.getQueueDate().after(message1.getQueueDate())) {
051                return 1;
052            }
053            
054            return 0;
055        }
056    
057    }