forked from rabbitmq/rabbitmq-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChannelN.java
1621 lines (1448 loc) · 61.2 KB
/
ChannelN.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
package com.rabbitmq.client.impl;
import com.rabbitmq.client.*;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Method;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.impl.AMQImpl.Channel;
import com.rabbitmq.client.impl.AMQImpl.Queue;
import com.rabbitmq.client.impl.AMQImpl.*;
import com.rabbitmq.utility.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException;
/**
* Main interface to AMQP protocol functionality. Public API -
* Implementation of all AMQChannels except channel zero.
* <p>
* To open a channel,
* <pre>
* {@link Connection} conn = ...;
* {@link ChannelN} ch1 = conn.{@link Connection#createChannel createChannel}();
* </pre>
*/
public class ChannelN extends AMQChannel implements com.rabbitmq.client.Channel {
private static final int MAX_UNSIGNED_SHORT = 65535;
private static final String UNSPECIFIED_OUT_OF_BAND = "";
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelN.class);
/** Map from consumer tag to {@link Consumer} instance.
* <p/>
* Note that, in general, this map should ONLY ever be accessed
* from the connection's reader thread. We go to some pains to
* ensure this is the case - see the use of
* BlockingRpcContinuation to inject code into the reader thread
* in basicConsume and basicCancel.
*/
private final Map<String, Consumer> _consumers =
Collections.synchronizedMap(new HashMap<String, Consumer>());
/* All listeners collections are in CopyOnWriteArrayList objects */
/** The ReturnListener collection. */
private final Collection<ReturnListener> returnListeners = new CopyOnWriteArrayList<ReturnListener>();
/** The ConfirmListener collection. */
private final Collection<ConfirmListener> confirmListeners = new CopyOnWriteArrayList<ConfirmListener>();
/** Sequence number of next published message requiring confirmation.*/
private long nextPublishSeqNo = 0L;
/** The current default consumer, or null if there is none. */
private volatile Consumer defaultConsumer = null;
/** Dispatcher of consumer work for this channel */
private final ConsumerDispatcher dispatcher;
/** Future boolean for shutting down */
private volatile CountDownLatch finishedShutdownFlag = null;
/** Set of currently unconfirmed messages (i.e. messages that have
* not been ack'd or nack'd by the server yet. */
private final SortedSet<Long> unconfirmedSet =
Collections.synchronizedSortedSet(new TreeSet<Long>());
/** Whether any nacks have been received since the last waitForConfirms(). */
private volatile boolean onlyAcksReceived = true;
protected final MetricsCollector metricsCollector;
/**
* Construct a new channel on the given connection with the given
* channel number. Usually not called directly - call
* Connection.createChannel instead.
* @see Connection#createChannel
* @param connection The connection associated with this channel
* @param channelNumber The channel number to be associated with this channel
* @param workService service for managing this channel's consumer callbacks
*/
public ChannelN(AMQConnection connection, int channelNumber,
ConsumerWorkService workService) {
this(connection, channelNumber, workService, new NoOpMetricsCollector());
}
/**
* Construct a new channel on the given connection with the given
* channel number. Usually not called directly - call
* Connection.createChannel instead.
* @see Connection#createChannel
* @param connection The connection associated with this channel
* @param channelNumber The channel number to be associated with this channel
* @param workService service for managing this channel's consumer callbacks
* @param metricsCollector service for managing metrics
*/
public ChannelN(AMQConnection connection, int channelNumber,
ConsumerWorkService workService, MetricsCollector metricsCollector) {
super(connection, channelNumber);
this.dispatcher = new ConsumerDispatcher(connection, this, workService);
this.metricsCollector = metricsCollector;
}
/**
* Package method: open the channel.
* This is only called from {@link ChannelManager}.
* @throws IOException if any problem is encountered
*/
public void open() throws IOException {
// wait for the Channel.OpenOk response, and ignore it
exnWrappingRpc(new Channel.Open(UNSPECIFIED_OUT_OF_BAND));
}
@Override
public void addReturnListener(ReturnListener listener) {
returnListeners.add(listener);
}
@Override
public ReturnListener addReturnListener(ReturnCallback returnCallback) {
ReturnListener returnListener = (replyCode, replyText, exchange, routingKey, properties, body) -> returnCallback.handle(new Return(
replyCode, replyText, exchange, routingKey, properties, body
));
this.addReturnListener(returnListener);
return returnListener;
}
@Override
public boolean removeReturnListener(ReturnListener listener) {
return returnListeners.remove(listener);
}
@Override
public void clearReturnListeners() {
returnListeners.clear();
}
@Override
public void addConfirmListener(ConfirmListener listener) {
confirmListeners.add(listener);
}
@Override
public ConfirmListener addConfirmListener(ConfirmCallback ackCallback, ConfirmCallback nackCallback) {
ConfirmListener confirmListener = new ConfirmListener() {
@Override
public void handleAck(long deliveryTag, boolean multiple) throws IOException {
ackCallback.handle(deliveryTag, multiple);
}
@Override
public void handleNack(long deliveryTag, boolean multiple) throws IOException {
nackCallback.handle(deliveryTag, multiple);
}
};
this.addConfirmListener(confirmListener);
return confirmListener;
}
@Override
public boolean removeConfirmListener(ConfirmListener listener) {
return confirmListeners.remove(listener);
}
@Override
public void clearConfirmListeners() {
confirmListeners.clear();
}
/** {@inheritDoc} */
@Override
public boolean waitForConfirms()
throws InterruptedException
{
boolean confirms = false;
try {
confirms = waitForConfirms(0L);
} catch (TimeoutException e) { }
return confirms;
}
/** {@inheritDoc} */
@Override
public boolean waitForConfirms(long timeout)
throws InterruptedException, TimeoutException {
if (nextPublishSeqNo == 0L)
throw new IllegalStateException("Confirms not selected");
long startTime = System.currentTimeMillis();
synchronized (unconfirmedSet) {
while (true) {
if (getCloseReason() != null) {
throw Utility.fixStackTrace(getCloseReason());
}
if (unconfirmedSet.isEmpty()) {
boolean aux = onlyAcksReceived;
onlyAcksReceived = true;
return aux;
}
if (timeout == 0L) {
unconfirmedSet.wait();
} else {
long elapsed = System.currentTimeMillis() - startTime;
if (timeout > elapsed) {
unconfirmedSet.wait(timeout - elapsed);
} else {
throw new TimeoutException();
}
}
}
}
}
/** {@inheritDoc} */
@Override
public void waitForConfirmsOrDie()
throws IOException, InterruptedException
{
try {
waitForConfirmsOrDie(0L);
} catch (TimeoutException e) { }
}
/** {@inheritDoc} */
@Override
public void waitForConfirmsOrDie(long timeout)
throws IOException, InterruptedException, TimeoutException
{
try {
if (!waitForConfirms(timeout)) {
close(AMQP.REPLY_SUCCESS, "NACKS RECEIVED", true, null, false);
throw new IOException("nacks received");
}
} catch (TimeoutException e) {
close(AMQP.PRECONDITION_FAILED, "TIMEOUT WAITING FOR ACK");
throw(e);
}
}
/** Returns the current default consumer. */
@Override
public Consumer getDefaultConsumer() {
return defaultConsumer;
}
/**
* Sets the current default consumer.
* A null argument is interpreted to mean "do not use a default consumer".
*/
@Override
public void setDefaultConsumer(Consumer consumer) {
defaultConsumer = consumer;
}
/**
* Sends a ShutdownSignal to all active consumers.
* Idempotent.
* @param signal an exception signalling channel shutdown
*/
private void broadcastShutdownSignal(ShutdownSignalException signal) {
this.finishedShutdownFlag = this.dispatcher.handleShutdownSignal(Utility.copy(_consumers), signal);
}
/**
* Start to shutdown -- defer rest of processing until ready
*/
private void startProcessShutdownSignal(ShutdownSignalException signal,
boolean ignoreClosed,
boolean notifyRpc)
{ super.processShutdownSignal(signal, ignoreClosed, notifyRpc);
}
/**
* Finish shutdown processing -- idempotent
*/
private void finishProcessShutdownSignal()
{
this.dispatcher.quiesce();
broadcastShutdownSignal(getCloseReason());
synchronized (unconfirmedSet) {
unconfirmedSet.notifyAll();
}
}
/**
* Protected API - overridden to quiesce consumer work and broadcast the signal
* to all consumers after calling the superclass's method.
*/
@Override public void processShutdownSignal(ShutdownSignalException signal,
boolean ignoreClosed,
boolean notifyRpc)
{
startProcessShutdownSignal(signal, ignoreClosed, notifyRpc);
finishProcessShutdownSignal();
}
CountDownLatch getShutdownLatch() {
return this.finishedShutdownFlag;
}
private void releaseChannel() {
getConnection().disconnectChannel(this);
}
/**
* Protected API - Filters the inbound command stream, processing
* Basic.Deliver, Basic.Return and Channel.Close specially. If
* we're in quiescing mode, all inbound commands are ignored,
* except for Channel.Close and Channel.CloseOk.
*/
@Override public boolean processAsync(Command command) throws IOException
{
// If we are isOpen(), then we process commands normally.
//
// If we are not, however, then we are in a quiescing, or
// shutting-down state as the result of an application
// decision to close this channel, and we are to discard all
// incoming commands except for a close and close-ok.
Method method = command.getMethod();
// we deal with channel.close in the same way, regardless
if (method instanceof Channel.Close) {
asyncShutdown(command);
return true;
}
if (isOpen()) {
// We're in normal running mode.
if (method instanceof Basic.Deliver) {
processDelivery(command, (Basic.Deliver) method);
return true;
} else if (method instanceof Basic.Return) {
callReturnListeners(command, (Basic.Return) method);
return true;
} else if (method instanceof Channel.Flow) {
Channel.Flow channelFlow = (Channel.Flow) method;
synchronized (_channelMutex) {
_blockContent = !channelFlow.getActive();
transmit(new Channel.FlowOk(!_blockContent));
_channelMutex.notifyAll();
}
return true;
} else if (method instanceof Basic.Ack) {
Basic.Ack ack = (Basic.Ack) method;
callConfirmListeners(command, ack);
handleAckNack(ack.getDeliveryTag(), ack.getMultiple(), false);
return true;
} else if (method instanceof Basic.Nack) {
Basic.Nack nack = (Basic.Nack) method;
callConfirmListeners(command, nack);
handleAckNack(nack.getDeliveryTag(), nack.getMultiple(), true);
return true;
} else if (method instanceof Basic.RecoverOk) {
for (Map.Entry<String, Consumer> entry : Utility.copy(_consumers).entrySet()) {
this.dispatcher.handleRecoverOk(entry.getValue(), entry.getKey());
}
// Unlike all the other cases we still want this RecoverOk to
// be handled by whichever RPC continuation invoked Recover,
// so return false
return false;
} else if (method instanceof Basic.Cancel) {
Basic.Cancel m = (Basic.Cancel)method;
String consumerTag = m.getConsumerTag();
Consumer callback = _consumers.remove(consumerTag);
// Not finding any matching consumer isn't necessarily an indication of an issue anywhere.
// Sometimes there's a natural race condition between consumer management on the server and client ends.
// E.g. Channel#basicCancel called just before a basic.cancel for the same consumer tag is received.
// See https://github.com/rabbitmq/rabbitmq-java-client/issues/525
if (callback == null) {
callback = defaultConsumer;
}
if (callback != null) {
try {
this.dispatcher.handleCancel(callback, consumerTag);
} catch (WorkPoolFullException e) {
// couldn't enqueue in work pool, propagating
throw e;
} catch (Throwable ex) {
getConnection().getExceptionHandler().handleConsumerException(this,
ex,
callback,
consumerTag,
"handleCancel");
}
} else {
LOGGER.warn("Could not cancel consumer with unknown tag {}", consumerTag);
}
return true;
} else {
return false;
}
} else {
// We're in quiescing mode == !isOpen()
if (method instanceof Channel.CloseOk) {
// We're quiescing, and we see a channel.close-ok:
// this is our signal to leave quiescing mode and
// finally shut down for good. Let it be handled as an
// RPC reply one final time by returning false.
return false;
} else {
// We're quiescing, and this inbound command should be
// discarded as per spec. "Consume" it by returning
// true.
return true;
}
}
}
protected void processDelivery(Command command, Basic.Deliver method) {
Basic.Deliver m = method;
Consumer callback = _consumers.get(m.getConsumerTag());
if (callback == null) {
if (defaultConsumer == null) {
// No handler set. We should blow up as this message
// needs acking, just dropping it is not enough. See bug
// 22587 for discussion.
throw new IllegalStateException("Unsolicited delivery -" +
" see Channel.setDefaultConsumer to handle this" +
" case.");
}
else {
callback = defaultConsumer;
}
}
Envelope envelope = new Envelope(m.getDeliveryTag(),
m.getRedelivered(),
m.getExchange(),
m.getRoutingKey());
try {
// call metricsCollector before the dispatching (which is async anyway)
// this way, the message is inside the stats before it is handled
// in case a manual ack in the callback, the stats will be able to record the ack
metricsCollector.consumedMessage(this, m.getDeliveryTag(), m.getConsumerTag());
this.dispatcher.handleDelivery(callback,
m.getConsumerTag(),
envelope,
(BasicProperties) command.getContentHeader(),
command.getContentBody());
} catch (WorkPoolFullException e) {
// couldn't enqueue in work pool, propagating
throw e;
} catch (Throwable ex) {
getConnection().getExceptionHandler().handleConsumerException(this,
ex,
callback,
m.getConsumerTag(),
"handleDelivery");
}
}
private void callReturnListeners(Command command, Basic.Return basicReturn) {
try {
for (ReturnListener l : this.returnListeners) {
l.handleReturn(basicReturn.getReplyCode(),
basicReturn.getReplyText(),
basicReturn.getExchange(),
basicReturn.getRoutingKey(),
(BasicProperties) command.getContentHeader(),
command.getContentBody());
}
} catch (Throwable ex) {
getConnection().getExceptionHandler().handleReturnListenerException(this, ex);
} finally {
metricsCollector.basicPublishUnrouted(this);
}
}
private void callConfirmListeners(@SuppressWarnings("unused") Command command, Basic.Ack ack) {
try {
for (ConfirmListener l : this.confirmListeners) {
l.handleAck(ack.getDeliveryTag(), ack.getMultiple());
}
} catch (Throwable ex) {
getConnection().getExceptionHandler().handleConfirmListenerException(this, ex);
} finally {
metricsCollector.basicPublishAck(this, ack.getDeliveryTag(), ack.getMultiple());
}
}
private void callConfirmListeners(@SuppressWarnings("unused") Command command, Basic.Nack nack) {
try {
for (ConfirmListener l : this.confirmListeners) {
l.handleNack(nack.getDeliveryTag(), nack.getMultiple());
}
} catch (Throwable ex) {
getConnection().getExceptionHandler().handleConfirmListenerException(this, ex);
} finally {
metricsCollector.basicPublishNack(this, nack.getDeliveryTag(), nack.getMultiple());
}
}
private void asyncShutdown(Command command) throws IOException {
ShutdownSignalException signal = new ShutdownSignalException(false,
false,
command.getMethod(),
this);
synchronized (_channelMutex) {
try {
processShutdownSignal(signal, true, false);
quiescingTransmit(new Channel.CloseOk());
} finally {
releaseChannel();
notifyOutstandingRpc(signal);
}
}
notifyListeners();
}
/** Public API - {@inheritDoc} */
@Override
public void close()
throws IOException, TimeoutException {
close(AMQP.REPLY_SUCCESS, "OK");
}
/** Public API - {@inheritDoc} */
@Override
public void close(int closeCode, String closeMessage)
throws IOException, TimeoutException {
close(closeCode, closeMessage, true, null, false);
}
/** Public API - {@inheritDoc} */
@Override
public void abort()
throws IOException
{
abort(AMQP.REPLY_SUCCESS, "OK");
}
/** Public API - {@inheritDoc} */
@Override
public void abort(int closeCode, String closeMessage)
throws IOException
{
try {
close(closeCode, closeMessage, true, null, true);
} catch (IOException _e) {
/* ignored */
} catch (TimeoutException _e) {
/* ignored */
}
}
/**
* Protected API - Close channel with code and message, indicating
* the source of the closure and a causing exception (null if
* none).
* @param closeCode the close code (See under "Reply Codes" in the AMQP specification)
* @param closeMessage a message indicating the reason for closing the connection
* @param initiatedByApplication true if this comes from an API call, false otherwise
* @param cause exception triggering close
* @param abort true if we should close and ignore errors
* @throws IOException if an error is encountered
*/
protected void close(int closeCode,
String closeMessage,
boolean initiatedByApplication,
Throwable cause,
boolean abort)
throws IOException, TimeoutException {
// First, notify all our dependents that we are shutting down.
// This clears isOpen(), so no further work from the
// application side will be accepted, and any inbound commands
// will be discarded (unless they're channel.close-oks).
Channel.Close reason = new Channel.Close(closeCode, closeMessage, 0, 0);
ShutdownSignalException signal = new ShutdownSignalException(false,
initiatedByApplication,
reason,
this);
if (cause != null) {
signal.initCause(cause);
}
BlockingRpcContinuation<AMQCommand> k = new BlockingRpcContinuation<AMQCommand>(){
@Override
public AMQCommand transformReply(AMQCommand command) {
ChannelN.this.finishProcessShutdownSignal();
return command;
}};
boolean notify = false;
try {
// Synchronize the block below to avoid race conditions in case
// connection wants to send Connection-CloseOK
synchronized (_channelMutex) {
startProcessShutdownSignal(signal, !initiatedByApplication, true);
quiescingRpc(reason, k);
}
// Now that we're in quiescing state, channel.close was sent and
// we wait for the reply. We ignore the result.
// (It's NOT always close-ok.)
notify = true;
// do not wait indefinitely
k.getReply(10000);
} catch (TimeoutException ise) {
if (!abort)
throw ise;
} catch (ShutdownSignalException sse) {
if (!abort)
throw sse;
} catch (IOException ioe) {
if (!abort)
throw ioe;
} finally {
if (abort || notify) {
// Now we know everything's been cleaned up and there should
// be no more surprises arriving on the wire. Release the
// channel number, and dissociate this ChannelN instance from
// our connection so that any further frames inbound on this
// channel can be caught as the errors they are.
releaseChannel();
notifyListeners();
}
}
}
/** Public API - {@inheritDoc} */
@Override
public void basicQos(int prefetchSize, int prefetchCount, boolean global)
throws IOException
{
if (prefetchCount < 0 || prefetchCount > MAX_UNSIGNED_SHORT) {
throw new IllegalArgumentException("Prefetch count must be between 0 and " + MAX_UNSIGNED_SHORT);
}
exnWrappingRpc(new Basic.Qos(prefetchSize, prefetchCount, global));
}
/** Public API - {@inheritDoc} */
@Override
public void basicQos(int prefetchCount, boolean global)
throws IOException
{
basicQos(0, prefetchCount, global);
}
/** Public API - {@inheritDoc} */
@Override
public void basicQos(int prefetchCount)
throws IOException
{
basicQos(0, prefetchCount, false);
}
/** Public API - {@inheritDoc} */
@Override
public void basicPublish(String exchange, String routingKey,
BasicProperties props, byte[] body)
throws IOException
{
basicPublish(exchange, routingKey, false, props, body);
}
/** Public API - {@inheritDoc} */
@Override
public void basicPublish(String exchange, String routingKey,
boolean mandatory,
BasicProperties props, byte[] body)
throws IOException
{
basicPublish(exchange, routingKey, mandatory, false, props, body);
}
/** Public API - {@inheritDoc} */
@Override
public void basicPublish(String exchange, String routingKey,
boolean mandatory, boolean immediate,
BasicProperties props, byte[] body)
throws IOException
{
final long deliveryTag;
if (nextPublishSeqNo > 0) {
deliveryTag = getNextPublishSeqNo();
unconfirmedSet.add(deliveryTag);
nextPublishSeqNo++;
} else {
deliveryTag = 0;
}
if (props == null) {
props = MessageProperties.MINIMAL_BASIC;
}
AMQCommand command = new AMQCommand(
new Basic.Publish.Builder()
.exchange(exchange)
.routingKey(routingKey)
.mandatory(mandatory)
.immediate(immediate)
.build(), props, body);
try {
transmit(command);
} catch (IOException | AlreadyClosedException e) {
metricsCollector.basicPublishFailure(this, e);
throw e;
}
metricsCollector.basicPublish(this, deliveryTag);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
boolean durable, boolean autoDelete,
Map<String, Object> arguments)
throws IOException
{
return exchangeDeclare(exchange, type,
durable, autoDelete, false,
arguments);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type,
boolean durable, boolean autoDelete,
Map<String, Object> arguments)
throws IOException
{
return exchangeDeclare(exchange, type.getType(),
durable, autoDelete,
arguments);
}
@Override
public void exchangeDeclareNoWait(String exchange,
String type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments) throws IOException {
transmit(new AMQCommand(new Exchange.Declare.Builder()
.exchange(exchange)
.type(type)
.durable(durable)
.autoDelete(autoDelete)
.internal(internal)
.arguments(arguments)
.passive(false)
.nowait(true)
.build()));
}
@Override
public void exchangeDeclareNoWait(String exchange,
BuiltinExchangeType type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments) throws IOException {
exchangeDeclareNoWait(exchange, type.getType(),
durable, autoDelete, internal,
arguments);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments)
throws IOException
{
return (Exchange.DeclareOk)
exnWrappingRpc(new Exchange.Declare.Builder()
.exchange(exchange)
.type(type)
.durable(durable)
.autoDelete(autoDelete)
.internal(internal)
.arguments(arguments)
.build())
.getMethod();
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments)
throws IOException
{
return exchangeDeclare(exchange, type.getType(),
durable, autoDelete, internal,
arguments);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
boolean durable)
throws IOException
{
return exchangeDeclare(exchange, type, durable, false, null);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type,
boolean durable)
throws IOException
{
return exchangeDeclare(exchange, type.getType(), durable);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, String type)
throws IOException
{
return exchangeDeclare(exchange, type, false, false, null);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type)
throws IOException
{
return exchangeDeclare(exchange, type.getType());
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeclareOk exchangeDeclarePassive(String exchange)
throws IOException
{
return (Exchange.DeclareOk)
exnWrappingRpc(new Exchange.Declare.Builder()
.exchange(exchange)
.type("")
.passive()
.build())
.getMethod();
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeleteOk exchangeDelete(String exchange, boolean ifUnused)
throws IOException
{
return (Exchange.DeleteOk)
exnWrappingRpc(new Exchange.Delete.Builder()
.exchange(exchange)
.ifUnused(ifUnused)
.build())
.getMethod();
}
/** Public API - {@inheritDoc} */
@Override
public void exchangeDeleteNoWait(String exchange, boolean ifUnused) throws IOException {
transmit(new AMQCommand(new Exchange.Delete.Builder()
.exchange(exchange)
.ifUnused(ifUnused)
.nowait(true)
.build()));
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.DeleteOk exchangeDelete(String exchange)
throws IOException
{
return exchangeDelete(exchange, false);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.BindOk exchangeBind(String destination, String source,
String routingKey, Map<String, Object> arguments)
throws IOException {
return (Exchange.BindOk)
exnWrappingRpc(new Exchange.Bind.Builder()
.destination(destination)
.source(source)
.routingKey(routingKey)
.arguments(arguments)
.build())
.getMethod();
}
/** Public API - {@inheritDoc} */
@Override
public void exchangeBindNoWait(String destination,
String source,
String routingKey,
Map<String, Object> arguments) throws IOException {
transmit(new AMQCommand(new Exchange.Bind.Builder()
.destination(destination)
.source(source)
.routingKey(routingKey)
.arguments(arguments)
.nowait(true)
.build()));
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.BindOk exchangeBind(String destination, String source,
String routingKey) throws IOException {
return exchangeBind(destination, source, routingKey, null);
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.UnbindOk exchangeUnbind(String destination, String source,
String routingKey, Map<String, Object> arguments)
throws IOException {
return (Exchange.UnbindOk)
exnWrappingRpc(new Exchange.Unbind.Builder()
.destination(destination)
.source(source)
.routingKey(routingKey)
.arguments(arguments)
.build())
.getMethod();
}
/** Public API - {@inheritDoc} */
@Override
public Exchange.UnbindOk exchangeUnbind(String destination, String source,
String routingKey) throws IOException {
return exchangeUnbind(destination, source, routingKey, null);
}
/** Public API - {@inheritDoc} */
@Override
public void exchangeUnbindNoWait(String destination, String source,
String routingKey, Map<String, Object> arguments)
throws IOException {
transmit(new AMQCommand(new Exchange.Unbind.Builder()
.destination(destination)
.source(source)
.routingKey(routingKey)
.arguments(arguments)
.nowait(true)
.build()));
}
/** Public API - {@inheritDoc} */
@Override
public Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive,
boolean autoDelete, Map<String, Object> arguments)
throws IOException
{
validateQueueNameLength(queue);
return (Queue.DeclareOk)
exnWrappingRpc(new Queue.Declare.Builder()
.queue(queue)
.durable(durable)
.exclusive(exclusive)
.autoDelete(autoDelete)
.arguments(arguments)
.build())
.getMethod();
}
/** Public API - {@inheritDoc} */
@Override
public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclare()
throws IOException
{
return queueDeclare("", false, true, true, null);
}
/** Public API - {@inheritDoc} */
@Override
public void queueDeclareNoWait(String queue,
boolean durable,
boolean exclusive,
boolean autoDelete,
Map<String, Object> arguments) throws IOException {
validateQueueNameLength(queue);
transmit(new AMQCommand(new Queue.Declare.Builder()
.queue(queue)
.durable(durable)
.exclusive(exclusive)
.autoDelete(autoDelete)