1
2
3
4
5
6
7
8
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
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
34
35
36
37
38 public Queue getQueue(String name)
39 {
40 QueueInfo queue = queueManager.getQueue(name);
41 return new QueueImpl(queue);
42 }
43
44 protected class QueueImpl implements Queue
45 {
46
47 protected QueueInfo queue;
48
49 public QueueImpl(QueueInfo queue)
50 {
51 this.queue = queue;
52 }
53
54 public void put(Object item) throws InterruptedException
55 {
56 offer(item, Long.MAX_VALUE);
57 }
58
59 public boolean offer(Object item, long timeout) throws InterruptedException
60 {
61 if (localContext != null)
62 {
63 return ((TransactionalQueueManager.QueueTransactionContext) localContext).offer(queue, item,
64 timeout);
65 }
66 else
67 {
68 try
69 {
70 Object id = queueManager.doStore(queue, item);
71 try
72 {
73 if (!queue.offer(id, 0, timeout))
74 {
75 queueManager.doRemove(queue, item);
76 return false;
77 }
78 else
79 {
80 return true;
81 }
82 }
83 catch (InterruptedException e)
84 {
85 queueManager.doRemove(queue, item);
86 throw e;
87 }
88 }
89 catch (IOException e)
90 {
91 throw new RuntimeException(e);
92 }
93 }
94 }
95
96 public Object take() throws InterruptedException
97 {
98 return poll(Long.MAX_VALUE);
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 (IOException e)
123 {
124 throw new RuntimeException(e);
125 }
126 }
127
128 public Object peek() throws InterruptedException
129 {
130 try
131 {
132 if (localContext != null)
133 {
134 return ((TransactionalQueueManager.QueueTransactionContext) localContext).peek(queue);
135 }
136 else
137 {
138 Object id = queue.peek();
139 if (id != null)
140 {
141 return queueManager.doLoad(queue, id);
142 }
143 return null;
144 }
145 }
146 catch (IOException e)
147 {
148 throw new RuntimeException(e);
149 }
150 }
151
152 public int size()
153 {
154 if (localContext != null)
155 {
156 return ((TransactionalQueueManager.QueueTransactionContext) localContext).size(queue);
157 }
158 else
159 {
160 return queue.list.size();
161 }
162 }
163
164 public String getName()
165 {
166 return queue.getName();
167 }
168
169 }
170 }