View Javadoc

1   /*
2    * $Id: JmsClientAcknowledgeTransaction.java 8215 2007-09-05 16:56:51Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.jms;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.providers.jms.i18n.JmsMessages;
15  import org.mule.transaction.AbstractSingleResourceTransaction;
16  import org.mule.transaction.IllegalTransactionStateException;
17  import org.mule.umo.TransactionException;
18  
19  import javax.jms.Connection;
20  import javax.jms.JMSException;
21  import javax.jms.Message;
22  import javax.jms.Session;
23  
24  /**
25   * <code>JmsClientAcknowledgeTransaction</code> is a transaction implementation of
26   * performing a message acknowledgement. There is no notion of rollback with client
27   * acknowledgement, but this transaction can be useful for controlling how messages
28   * are consumed from a destination.
29   */
30  public class JmsClientAcknowledgeTransaction extends AbstractSingleResourceTransaction
31  {
32      private volatile Message message;
33  
34      public void setMessage(Message message)
35      {
36          this.message = message;
37      }
38  
39      /*
40      * (non-Javadoc)
41      *
42      * @see org.mule.transaction.AbstractSingleResourceTransaction#doBegin()
43      */
44      protected void doBegin() throws TransactionException
45      {
46          // nothing to do
47      }
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see org.mule.transaction.AbstractSingleResourceTransaction#doCommit()
53       */
54      protected void doCommit() throws TransactionException
55      {
56          try
57          {
58              if (message == null)
59              {
60                  throw new IllegalTransactionStateException(
61                      JmsMessages.noMessageBoundForAck());
62              }
63              message.acknowledge();
64          }
65          catch (JMSException e)
66          {
67              throw new IllegalTransactionStateException(CoreMessages.transactionCommitFailed(), e);
68          }
69      }
70  
71      /*
72       * (non-Javadoc)
73       * 
74       * @see org.mule.transaction.AbstractSingleResourceTransaction#doRollback()
75       */
76      protected void doRollback() throws TransactionException
77      {
78          // If a message has been bound, rollback is forbidden
79          if (message != null)
80          {
81              throw new UnsupportedOperationException("Jms Client Acknowledge doesn't support rollback");
82          }
83      }
84  
85      /*
86       * (non-Javadoc)
87       * 
88       * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
89       *      java.lang.Object)
90       */
91      public void bindResource(Object key, Object resource) throws TransactionException
92      {
93          if (key instanceof Message)
94          {
95              this.message = (Message)key;
96              return;
97          }
98          if (!(key instanceof Connection) || !(resource instanceof Session))
99          {
100             throw new IllegalTransactionStateException(
101                 CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
102         }
103 
104         Session session = (Session)resource;
105         try
106         {
107             if (session.getTransacted())
108             {
109                 throw new IllegalTransactionStateException(JmsMessages.sessionShouldNotBeTransacted());
110             }
111         }
112         catch (JMSException e)
113         {
114             throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
115         }
116 
117         super.bindResource(key, resource);
118     }
119 }