|
37 | 37 | import java.io.DataOutputStream; |
38 | 38 | import java.io.IOException; |
39 | 39 | import java.util.Arrays; |
| 40 | +import java.util.Collections; |
40 | 41 | import java.util.concurrent.Semaphore; |
41 | 42 | import java.util.List; |
| 43 | +import java.util.SortedSet; |
| 44 | +import java.util.TreeSet; |
42 | 45 | import java.util.UUID; |
43 | 46 |
|
44 | 47 | import org.apache.commons.cli.CommandLine; |
@@ -248,6 +251,8 @@ public static class Producer implements Runnable, ReturnListener, AckListener { |
248 | 251 | private boolean confirm; |
249 | 252 | private long confirmCount; |
250 | 253 | private Semaphore confirmPool; |
| 254 | + private volatile SortedSet<Long> ackSet = |
| 255 | + Collections.synchronizedSortedSet(new TreeSet<Long>()); |
251 | 256 |
|
252 | 257 | public Producer(Channel channel, String exchangeName, String id, |
253 | 258 | List flags, int txSize, |
@@ -289,21 +294,34 @@ public synchronized void resetBasicReturns() { |
289 | 294 | basicReturnCount = 0; |
290 | 295 | } |
291 | 296 |
|
292 | | - public void handleAck(long sequenceNumber, boolean multiple) { |
293 | | - logAck(sequenceNumber); |
| 297 | + public void handleAck(long seqNo, boolean multiple) { |
| 298 | + int numConfirms = 0; |
| 299 | + if (multiple) { |
| 300 | + for (long i = ackSet.first(); i <= seqNo; ++i) { |
| 301 | + if (!ackSet.contains(i)) |
| 302 | + continue; |
| 303 | + ackSet.remove(i); |
| 304 | + numConfirms++; |
| 305 | + } |
| 306 | + } else { |
| 307 | + ackSet.remove(seqNo); |
| 308 | + numConfirms = 1; |
| 309 | + } |
| 310 | + addConfirms(numConfirms); |
| 311 | + |
| 312 | + if (confirmPool != null) { |
| 313 | + for (int i = 0; i < numConfirms; ++i) { |
| 314 | + confirmPool.release(); |
| 315 | + } |
| 316 | + } |
294 | 317 | } |
295 | 318 |
|
296 | 319 | private synchronized void resetConfirms() { |
297 | 320 | confirmCount = 0; |
298 | 321 | } |
299 | 322 |
|
300 | | - private void logAck(long seqNum) { |
301 | | - if (confirmPool != null) { |
302 | | - confirmPool.release(); |
303 | | - } |
304 | | - synchronized (this) { |
305 | | - confirmCount++; |
306 | | - } |
| 323 | + private synchronized void addConfirms(int numConfirms) { |
| 324 | + confirmCount += numConfirms; |
307 | 325 | } |
308 | 326 |
|
309 | 327 | public void run() { |
@@ -345,6 +363,7 @@ public void run() { |
345 | 363 | private void publish(byte[] msg) |
346 | 364 | throws IOException { |
347 | 365 |
|
| 366 | + ackSet.add(channel.getNextPublishSeqNo()); |
348 | 367 | channel.basicPublish(exchangeName, id, |
349 | 368 | mandatory, immediate, |
350 | 369 | persistent ? MessageProperties.MINIMAL_PERSISTENT_BASIC : MessageProperties.MINIMAL_BASIC, |
|
0 commit comments