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