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