forked from spring-projects/spring-amqp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PublisherCallbackChannelImpl.java
598 lines (496 loc) · 18.7 KB
/
PublisherCallbackChannelImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.amqp.rabbit.support;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.AMQP.Basic.RecoverOk;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.Channel.FlowOk;
import com.rabbitmq.client.AMQP.Exchange.BindOk;
import com.rabbitmq.client.AMQP.Exchange.DeclareOk;
import com.rabbitmq.client.AMQP.Exchange.DeleteOk;
import com.rabbitmq.client.AMQP.Exchange.UnbindOk;
import com.rabbitmq.client.AMQP.Queue.PurgeOk;
import com.rabbitmq.client.AMQP.Tx.CommitOk;
import com.rabbitmq.client.AMQP.Tx.RollbackOk;
import com.rabbitmq.client.AMQP.Tx.SelectOk;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Command;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.FlowListener;
import com.rabbitmq.client.GetResponse;
import com.rabbitmq.client.Method;
import com.rabbitmq.client.ReturnListener;
import com.rabbitmq.client.ShutdownListener;
import com.rabbitmq.client.ShutdownSignalException;
/**
* Channel wrapper to allow a single listener able to handle
* confirms from multiple channels.
*
* @author Gary Russell
* @since 1.0.1
*
*/
public class PublisherCallbackChannelImpl implements PublisherCallbackChannel, ConfirmListener, ReturnListener {
private final Log logger = LogFactory.getLog(this.getClass());
private final Channel delegate;
private final Map<String, Listener> listeners = new ConcurrentHashMap<String, Listener>();
private final Map<Listener, SortedMap<Long, PendingConfirm>> pendingConfirms
= new ConcurrentHashMap<PublisherCallbackChannel.Listener, SortedMap<Long,PendingConfirm>>();
private final SortedMap<Long, Listener> listenerForSeq = new ConcurrentSkipListMap<Long, Listener>();
public PublisherCallbackChannelImpl(Channel delegate) {
this.delegate = delegate;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BEGIN PURE DELEGATE METHODS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void addShutdownListener(ShutdownListener listener) {
this.delegate.addShutdownListener(listener);
}
public void removeShutdownListener(ShutdownListener listener) {
this.delegate.removeShutdownListener(listener);
}
public ShutdownSignalException getCloseReason() {
return this.delegate.getCloseReason();
}
public void notifyListeners() {
this.delegate.notifyListeners();
}
public boolean isOpen() {
return this.delegate.isOpen();
}
public int getChannelNumber() {
return this.delegate.getChannelNumber();
}
public Connection getConnection() {
return this.delegate.getConnection();
}
public void close(int closeCode, String closeMessage) throws IOException {
this.delegate.close(closeCode, closeMessage);
}
public FlowOk flow(boolean active) throws IOException {
return this.delegate.flow(active);
}
public FlowOk getFlow() {
return this.delegate.getFlow();
}
public void abort() throws IOException {
this.delegate.abort();
}
public void abort(int closeCode, String closeMessage) throws IOException {
this.delegate.abort(closeCode, closeMessage);
}
public void addFlowListener(FlowListener listener) {
this.delegate.addFlowListener(listener);
}
public boolean removeFlowListener(FlowListener listener) {
return this.delegate.removeFlowListener(listener);
}
public void clearFlowListeners() {
this.delegate.clearFlowListeners();
}
public Consumer getDefaultConsumer() {
return this.delegate.getDefaultConsumer();
}
public void setDefaultConsumer(Consumer consumer) {
this.delegate.setDefaultConsumer(consumer);
}
public void basicQos(int prefetchSize, int prefetchCount, boolean global)
throws IOException {
this.delegate.basicQos(prefetchSize, prefetchCount, global);
}
public void basicQos(int prefetchCount) throws IOException {
this.delegate.basicQos(prefetchCount);
}
public void basicPublish(String exchange, String routingKey,
BasicProperties props, byte[] body) throws IOException {
this.delegate.basicPublish(exchange, routingKey, props, body);
}
public void basicPublish(String exchange, String routingKey,
boolean mandatory, boolean immediate, BasicProperties props,
byte[] body) throws IOException {
this.delegate.basicPublish(exchange, routingKey, mandatory, immediate,
props, body);
}
public void basicPublish(String exchange, String routingKey,
boolean mandatory, BasicProperties props, byte[] body)
throws IOException {
this.delegate.basicPublish(exchange, routingKey, mandatory, props, body);
}
public DeclareOk exchangeDeclare(String exchange, String type)
throws IOException {
return this.delegate.exchangeDeclare(exchange, type);
}
public DeclareOk exchangeDeclare(String exchange, String type,
boolean durable) throws IOException {
return this.delegate.exchangeDeclare(exchange, type, durable);
}
public DeclareOk exchangeDeclare(String exchange, String type,
boolean durable, boolean autoDelete, Map<String, Object> arguments)
throws IOException {
return this.delegate.exchangeDeclare(exchange, type, durable, autoDelete,
arguments);
}
public DeclareOk exchangeDeclare(String exchange, String type,
boolean durable, boolean autoDelete, boolean internal,
Map<String, Object> arguments) throws IOException {
return this.delegate.exchangeDeclare(exchange, type, durable, autoDelete,
internal, arguments);
}
public DeclareOk exchangeDeclarePassive(String name) throws IOException {
return this.delegate.exchangeDeclarePassive(name);
}
public DeleteOk exchangeDelete(String exchange, boolean ifUnused)
throws IOException {
return this.delegate.exchangeDelete(exchange, ifUnused);
}
public DeleteOk exchangeDelete(String exchange) throws IOException {
return this.delegate.exchangeDelete(exchange);
}
public BindOk exchangeBind(String destination, String source,
String routingKey) throws IOException {
return this.delegate.exchangeBind(destination, source, routingKey);
}
public BindOk exchangeBind(String destination, String source,
String routingKey, Map<String, Object> arguments)
throws IOException {
return this.delegate
.exchangeBind(destination, source, routingKey, arguments);
}
public UnbindOk exchangeUnbind(String destination, String source,
String routingKey) throws IOException {
return this.delegate.exchangeUnbind(destination, source, routingKey);
}
public UnbindOk exchangeUnbind(String destination, String source,
String routingKey, Map<String, Object> arguments)
throws IOException {
return this.delegate.exchangeUnbind(destination, source, routingKey,
arguments);
}
public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclare()
throws IOException {
return this.delegate.queueDeclare();
}
public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclare(String queue,
boolean durable, boolean exclusive, boolean autoDelete,
Map<String, Object> arguments) throws IOException {
return this.delegate.queueDeclare(queue, durable, exclusive, autoDelete,
arguments);
}
public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclarePassive(
String queue) throws IOException {
return this.delegate.queueDeclarePassive(queue);
}
public com.rabbitmq.client.AMQP.Queue.DeleteOk queueDelete(String queue)
throws IOException {
return this.delegate.queueDelete(queue);
}
public com.rabbitmq.client.AMQP.Queue.DeleteOk queueDelete(String queue,
boolean ifUnused, boolean ifEmpty) throws IOException {
return this.delegate.queueDelete(queue, ifUnused, ifEmpty);
}
public com.rabbitmq.client.AMQP.Queue.BindOk queueBind(String queue,
String exchange, String routingKey) throws IOException {
return this.delegate.queueBind(queue, exchange, routingKey);
}
public com.rabbitmq.client.AMQP.Queue.BindOk queueBind(String queue,
String exchange, String routingKey, Map<String, Object> arguments)
throws IOException {
return this.delegate.queueBind(queue, exchange, routingKey, arguments);
}
public com.rabbitmq.client.AMQP.Queue.UnbindOk queueUnbind(String queue,
String exchange, String routingKey) throws IOException {
return this.delegate.queueUnbind(queue, exchange, routingKey);
}
public com.rabbitmq.client.AMQP.Queue.UnbindOk queueUnbind(String queue,
String exchange, String routingKey, Map<String, Object> arguments)
throws IOException {
return this.delegate.queueUnbind(queue, exchange, routingKey, arguments);
}
public PurgeOk queuePurge(String queue) throws IOException {
return this.delegate.queuePurge(queue);
}
public GetResponse basicGet(String queue, boolean autoAck)
throws IOException {
return this.delegate.basicGet(queue, autoAck);
}
public void basicAck(long deliveryTag, boolean multiple) throws IOException {
this.delegate.basicAck(deliveryTag, multiple);
}
public void basicNack(long deliveryTag, boolean multiple, boolean requeue)
throws IOException {
this.delegate.basicNack(deliveryTag, multiple, requeue);
}
public void basicReject(long deliveryTag, boolean requeue)
throws IOException {
this.delegate.basicReject(deliveryTag, requeue);
}
public String basicConsume(String queue, Consumer callback)
throws IOException {
return this.delegate.basicConsume(queue, callback);
}
public String basicConsume(String queue, boolean autoAck, Consumer callback)
throws IOException {
return this.delegate.basicConsume(queue, autoAck, callback);
}
public String basicConsume(String queue, boolean autoAck,
String consumerTag, Consumer callback) throws IOException {
return this.delegate.basicConsume(queue, autoAck, consumerTag, callback);
}
public String basicConsume(String queue, boolean autoAck,
String consumerTag, boolean noLocal, boolean exclusive,
Map<String, Object> arguments, Consumer callback)
throws IOException {
return this.delegate.basicConsume(queue, autoAck, consumerTag, noLocal,
exclusive, arguments, callback);
}
public void basicCancel(String consumerTag) throws IOException {
this.delegate.basicCancel(consumerTag);
}
public RecoverOk basicRecover() throws IOException {
return this.delegate.basicRecover();
}
public RecoverOk basicRecover(boolean requeue) throws IOException {
return this.delegate.basicRecover(requeue);
}
@SuppressWarnings("deprecation")
public void basicRecoverAsync(boolean requeue) throws IOException {
this.delegate.basicRecoverAsync(requeue);
}
public SelectOk txSelect() throws IOException {
return this.delegate.txSelect();
}
public CommitOk txCommit() throws IOException {
return this.delegate.txCommit();
}
public RollbackOk txRollback() throws IOException {
return this.delegate.txRollback();
}
public com.rabbitmq.client.AMQP.Confirm.SelectOk confirmSelect()
throws IOException {
return this.delegate.confirmSelect();
}
public long getNextPublishSeqNo() {
return this.delegate.getNextPublishSeqNo();
}
public boolean waitForConfirms() throws InterruptedException {
return this.delegate.waitForConfirms();
}
public boolean waitForConfirms(long timeout) throws InterruptedException,
TimeoutException {
return this.delegate.waitForConfirms(timeout);
}
public void waitForConfirmsOrDie() throws IOException, InterruptedException {
this.delegate.waitForConfirmsOrDie();
}
public void waitForConfirmsOrDie(long timeout) throws IOException,
InterruptedException, TimeoutException {
this.delegate.waitForConfirmsOrDie(timeout);
}
public void asyncRpc(Method method) throws IOException {
this.delegate.asyncRpc(method);
}
public Command rpc(Method method) throws IOException {
return this.delegate.rpc(method);
}
public void addConfirmListener(ConfirmListener listener) {
this.delegate.addConfirmListener(listener);
}
public boolean removeConfirmListener(ConfirmListener listener) {
return this.delegate.removeConfirmListener(listener);
}
public void clearConfirmListeners() {
this.delegate.clearConfirmListeners();
}
public void addReturnListener(ReturnListener listener) {
this.delegate.addReturnListener(listener);
}
public boolean removeReturnListener(ReturnListener listener) {
return this.delegate.removeReturnListener(listener);
}
public synchronized void clearReturnListeners() {
this.delegate.clearReturnListeners();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// END PURE DELEGATE METHODS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void close() throws IOException {
this.delegate.close();
for (Entry<Listener, SortedMap<Long, PendingConfirm>> entry : this.pendingConfirms.entrySet()) {
Listener listener = entry.getKey();
listener.removePendingConfirmsReference(this, entry.getValue());
}
this.pendingConfirms.clear();
this.listenerForSeq.clear();
}
public synchronized SortedMap<Long, PendingConfirm> addListener(Listener listener) {
Assert.notNull(listener, "Listener cannot be null");
if (this.listeners.size() == 0) {
this.delegate.addConfirmListener(this);
this.delegate.addReturnListener(this);
}
if (!this.listeners.values().contains(listener)){
this.listeners.put(listener.getUUID(), listener);
this.pendingConfirms.put(listener, Collections.synchronizedSortedMap(new TreeMap<Long, PendingConfirm>()));
if (logger.isDebugEnabled()) {
logger.debug("Added listener " + listener);
}
}
return this.pendingConfirms.get(listener);
}
public synchronized boolean removeListener(Listener listener) {
Listener mappedListener = this.listeners.remove(listener.getUUID());
boolean result = mappedListener != null;
if (result && this.listeners.size() == 0) {
this.delegate.removeConfirmListener(this);
this.delegate.removeReturnListener(this);
}
Iterator<Entry<Long, Listener>> iterator = this.listenerForSeq.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Long, Listener> entry = iterator.next();
if (entry.getValue() == listener) {
iterator.remove();
}
}
this.pendingConfirms.remove(listener);
return result;
}
// ConfirmListener
public void handleAck(long seq, boolean multiple)
throws IOException {
if (logger.isDebugEnabled()) {
logger.debug(this.toString() + " PC:Ack:" + seq + ":" + multiple);
}
this.processAck(seq, true, multiple);
}
public void handleNack(long seq, boolean multiple)
throws IOException {
if (logger.isDebugEnabled()) {
logger.debug(this.toString() + " PC:Nack:" + seq + ":" + multiple);
}
this.processAck(seq, false, multiple);
}
private void processAck(long seq, boolean ack, boolean multiple) {
if (multiple) {
/*
* Piggy-backed ack - extract all Listeners for this and earlier
* sequences. Then, for each Listener, handle each of it's acks.
*/
synchronized(this.pendingConfirms) {
Map<Long, Listener> involvedListeners = this.listenerForSeq.headMap(seq + 1);
// eliminate duplicates
Set<Listener> listeners = new HashSet<Listener>(involvedListeners.values());
for (Listener involvedListener : listeners) {
// find all unack'd confirms for this listener and handle them
SortedMap<Long, PendingConfirm> confirmsMap = this.pendingConfirms.get(involvedListener);
if (confirmsMap != null) {
Map<Long, PendingConfirm> confirms = confirmsMap.headMap(seq + 1);
Iterator<Entry<Long, PendingConfirm>> iterator = confirms.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Long, PendingConfirm> entry = iterator.next();
iterator.remove();
doHandleConfirm(ack, involvedListener, entry.getValue());
}
}
}
}
}
else {
Listener listener = this.listenerForSeq.get(seq);
if (listener != null) {
PendingConfirm pendingConfirm = this.pendingConfirms.get(listener).remove(seq);
if (pendingConfirm != null) {
doHandleConfirm(ack, listener, pendingConfirm);
}
}
else {
logger.error("No listener for seq:" + seq);
}
}
}
private void doHandleConfirm(boolean ack, Listener listener, PendingConfirm pendingConfirm) {
try {
if (listener.isConfirmListener()) {
if (logger.isDebugEnabled()) {
logger.debug("Sending confirm " + pendingConfirm);
}
listener.handleConfirm(pendingConfirm, ack);
}
}
catch (Exception e) {
logger.error("Exception delivering confirm", e);
}
}
public void addPendingConfirm(Listener listener, long seq, PendingConfirm pendingConfirm) {
SortedMap<Long, PendingConfirm> pendingConfirmsForListener = this.pendingConfirms.get(listener);
Assert.notNull(pendingConfirmsForListener, "Listener not registered");
synchronized (this.pendingConfirms) {
pendingConfirmsForListener.put(seq, pendingConfirm);
}
this.listenerForSeq.put(seq, listener);
}
// ReturnListener
public void handleReturn(int replyCode,
String replyText,
String exchange,
String routingKey,
AMQP.BasicProperties properties,
byte[] body) throws IOException
{
Object uuidObject = properties.getHeaders().get(RETURN_CORRELATION).toString();
Listener listener = this.listeners.get(uuidObject);
if (listener == null || !listener.isReturnListener()) {
if (logger.isWarnEnabled()) {
logger.warn("No Listener for returned message");
}
}
else {
listener.handleReturn(replyCode, replyText, exchange, routingKey, properties, body);
}
}
// Object
@Override
public int hashCode() {
return this.delegate.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
return this.delegate.equals(obj);
}
@Override
public String toString() {
return "PublisherCallbackChannelImpl: " + this.delegate.toString();
}
}