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