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