View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging.serviceproxies;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.ksb.messaging.MessageServiceInvoker;
20  import org.kuali.rice.ksb.messaging.PersistedMessageBO;
21  import org.kuali.rice.ksb.service.KSBServiceLocator;
22  import org.springframework.core.Ordered;
23  import org.springframework.transaction.support.TransactionSynchronizationAdapter;
24  
25  import java.util.concurrent.atomic.AtomicBoolean;
26  
27  /**
28   * Sends message when current transaction commits.  
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   *
32   */
33  public class MessageSendingTransactionSynchronization extends TransactionSynchronizationAdapter {
34      
35      private static final Logger LOG = Logger.getLogger(MessageSendingTransactionSynchronization.class);
36      public static final AtomicBoolean CALLED_TRANS_COMMITTED = new AtomicBoolean(false);
37      public static final AtomicBoolean CALLED_TRANS_ROLLEDBACKED = new AtomicBoolean(false);
38  
39      private final PersistedMessageBO message;
40      
41      public MessageSendingTransactionSynchronization(PersistedMessageBO message) {
42  	    this.message = message;
43      }
44  
45      @Override
46      public void afterCompletion(int status) {
47          if (status == STATUS_COMMITTED) {
48              KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message));
49              CALLED_TRANS_COMMITTED.set(true);
50          } else {
51              LOG.info("Message " + message + " not sent because transaction not committed.");
52              CALLED_TRANS_ROLLEDBACKED.set(true);
53          }
54      }
55  
56      @Override
57  	public int getOrder() {
58  		return Ordered.HIGHEST_PRECEDENCE;
59  	}
60  }