View Javadoc

1   /*
2    * $Id: JmsClientAcknowledgeTransaction.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jms;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.transaction.TransactionException;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.transaction.AbstractSingleResourceTransaction;
17  import org.mule.transaction.IllegalTransactionStateException;
18  import org.mule.transport.jms.i18n.JmsMessages;
19  
20  import javax.jms.Connection;
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  import javax.jms.Session;
24  
25  /**
26   * <code>JmsClientAcknowledgeTransaction</code> is a transaction implementation of
27   * performing a message acknowledgement. There is no notion of rollback with client
28   * acknowledgement, but this transaction can be useful for controlling how messages
29   * are consumed from a destination.
30   */
31  public class JmsClientAcknowledgeTransaction extends AbstractSingleResourceTransaction
32  {
33      private volatile Message message;
34  
35      public JmsClientAcknowledgeTransaction(MuleContext muleContext)
36      {
37          super(muleContext);
38      }
39  
40      public void setMessage(Message message)
41      {
42          this.message = message;
43      }
44  
45      @Override
46      protected void doBegin() throws TransactionException
47      {
48          // nothing to do
49      }
50  
51      @Override
52      protected void doCommit() throws TransactionException
53      {
54          try
55          {
56              if (message == null)
57              {
58                  throw new IllegalTransactionStateException(
59                      JmsMessages.noMessageBoundForAck());
60              }
61              message.acknowledge();
62          }
63          catch (JMSException e)
64          {
65              throw new IllegalTransactionStateException(CoreMessages.transactionCommitFailed(), e);
66          }
67      }
68  
69      @Override
70      protected void doRollback() throws TransactionException
71      {
72          // If a message has been bound, rollback is forbidden
73          if (message != null)
74          {
75              throw new UnsupportedOperationException("Jms Client Acknowledge doesn't support rollback");
76          }
77      }
78  
79      @Override
80      public void bindResource(Object key, Object resource) throws TransactionException
81      {
82          if (key instanceof Message)
83          {
84              this.message = (Message)key;
85              return;
86          }
87          if (!(key instanceof Connection) || !(resource instanceof Session))
88          {
89              throw new IllegalTransactionStateException(
90                  CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
91          }
92  
93          Session session = (Session)resource;
94          try
95          {
96              if (session.getTransacted())
97              {
98                  throw new IllegalTransactionStateException(JmsMessages.sessionShouldNotBeTransacted());
99              }
100         }
101         catch (JMSException e)
102         {
103             throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
104         }
105 
106         super.bindResource(key, resource);
107     }
108 }