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