001    /**
002     * Copyright 2005-2012 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.serviceproxies;
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.service.KSBServiceLocator;
022    import org.springframework.core.Ordered;
023    import org.springframework.transaction.support.TransactionSynchronizationAdapter;
024    
025    import java.util.concurrent.atomic.AtomicBoolean;
026    
027    /**
028     * Sends message when current transaction commits.  
029     * 
030     * @author Kuali Rice Team (rice.collab@kuali.org)
031     *
032     */
033    public class MessageSendingTransactionSynchronization extends TransactionSynchronizationAdapter {
034        
035        private static final Logger LOG = Logger.getLogger(MessageSendingTransactionSynchronization.class);
036        public static final AtomicBoolean CALLED_TRANS_COMMITTED = new AtomicBoolean(false);
037        public static final AtomicBoolean CALLED_TRANS_ROLLEDBACKED = new AtomicBoolean(false);
038    
039        private final PersistedMessageBO message;
040        
041        public MessageSendingTransactionSynchronization(PersistedMessageBO message) {
042                this.message = message;
043        }
044    
045        @Override
046        public void afterCompletion(int status) {
047            if (status == STATUS_COMMITTED) {
048                KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message));
049                CALLED_TRANS_COMMITTED.set(true);
050            } else {
051                LOG.info("Message " + message + " not sent because transaction not committed.");
052                CALLED_TRANS_ROLLEDBACKED.set(true);
053            }
054        }
055    
056        @Override
057            public int getOrder() {
058                    return Ordered.HIGHEST_PRECEDENCE;
059            }
060    }