View Javadoc

1   /*
2    * $Id: TransactionalQueueSession.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.util.queue;
12  
13  import org.mule.util.xa.AbstractXAResourceManager;
14  import org.mule.util.xa.DefaultXASession;
15  
16  import java.io.IOException;
17  
18  /**
19   * A Queue session that is used to manage the transaction context of a Queue
20   */
21  class TransactionalQueueSession extends DefaultXASession implements QueueSession
22  {
23  
24      protected TransactionalQueueManager queueManager;
25  
26      public TransactionalQueueSession(AbstractXAResourceManager resourceManager,
27                                       TransactionalQueueManager queueManager)
28      {
29          super(resourceManager);
30          this.queueManager = queueManager;
31      }
32  
33      public Queue getQueue(String name)
34      {
35          QueueInfo queue = queueManager.getQueue(name);
36          return new QueueImpl(queue);
37      }
38  
39      protected class QueueImpl implements Queue
40      {
41  
42          protected QueueInfo queue;
43  
44          public QueueImpl(QueueInfo queue)
45          {
46              this.queue = queue;
47          }
48  
49          public void put(Object item) throws InterruptedException
50          {
51              offer(item, Long.MAX_VALUE);
52          }
53  
54          public boolean offer(Object item, long timeout) throws InterruptedException
55          {
56              if (localContext != null)
57              {
58                  return ((TransactionalQueueManager.QueueTransactionContext) localContext).offer(queue, item,
59                      timeout);
60              }
61              else
62              {
63                  try
64                  {
65                      Object id = queueManager.doStore(queue, item);
66                      try
67                      {
68                          if (!queue.offer(id, 0, timeout))
69                          {
70                              queueManager.doRemove(queue, id);
71                              return false;
72                          }
73                          else
74                          {
75                              return true;
76                          }
77                      }
78                      catch (InterruptedException e)
79                      {
80                          queueManager.doRemove(queue, id);
81                          throw e;
82                      }
83                  }
84                  catch (IOException e)
85                  {
86                      throw new RuntimeException(e);
87                  }
88              }
89          }
90  
91          public Object take() throws InterruptedException
92          {
93              return poll(Long.MAX_VALUE);
94          }
95  
96          public void untake(Object item) throws InterruptedException
97          {
98              queue.untake(item);
99          }
100 
101         public Object poll(long timeout) throws InterruptedException
102         {
103             try
104             {
105                 if (localContext != null)
106                 {
107                     return ((TransactionalQueueManager.QueueTransactionContext) localContext).poll(queue,
108                                                                                                    timeout);
109                 }
110                 else
111                 {
112                     Object id = queue.poll(timeout);
113                     if (id != null)
114                     {
115                         Object item = queueManager.doLoad(queue, id);
116                         queueManager.doRemove(queue, id);
117                         return item;
118                     }
119                     return null;
120                 }
121             }
122             catch (InterruptedException iex)
123             {
124                 if (queueManager.getMuleContext().isStopping())
125                 {
126                     throw iex;
127                 }
128                 // if stopping, ignore
129                 return null;
130             }
131             catch (IOException e)
132             {
133                 throw new RuntimeException(e);
134             }
135 
136         }
137 
138         public Object peek() throws InterruptedException
139         {
140             try
141             {
142                 if (localContext != null)
143                 {
144                     return ((TransactionalQueueManager.QueueTransactionContext) localContext).peek(queue);
145                 }
146                 else
147                 {
148                     Object id = queue.peek();
149                     if (id != null)
150                     {
151                         return queueManager.doLoad(queue, id);
152                     }
153                     return null;
154                 }
155             }
156             catch (IOException e)
157             {
158                 throw new RuntimeException(e);
159             }
160         }
161 
162         public int size()
163         {
164             if (localContext != null)
165             {
166                 return ((TransactionalQueueManager.QueueTransactionContext) localContext).size(queue);
167             }
168             else
169             {
170                 return queue.list.size();
171             }
172         }
173 
174         public String getName()
175         {
176             return queue.getName();
177         }
178 
179     }
180 }