@@ -68,6 +68,7 @@ class AmqpManagement implements Management {
68
68
private static final int CODE_200 = 200 ;
69
69
private static final int CODE_201 = 201 ;
70
70
private static final int CODE_204 = 204 ;
71
+ private static final int CODE_400 = 400 ;
71
72
private static final int CODE_404 = 404 ;
72
73
private static final int CODE_409 = 409 ;
73
74
@@ -173,6 +174,14 @@ public UnbindSpecification unbind() {
173
174
return new AmqpBindingManagement .AmqpUnbindSpecification (this );
174
175
}
175
176
177
+ @ Override
178
+ public void queuePurge (String queue ) {
179
+ Map <String , Object > responseBody = delete (queueLocation (queue ) + "/messages" , CODE_200 );
180
+ if (!responseBody .containsKey ("message_count" )) {
181
+ throw new AmqpException ("Response body should contain message_count" );
182
+ }
183
+ }
184
+
176
185
void setToken (String token ) {
177
186
if (!this .connection .setTokenSupported ()) {
178
187
throw new UnsupportedOperationException ("Token renewal requires at least RabbitMQ 4.1.0" );
@@ -477,18 +486,23 @@ private static void checkResponse(
477
486
if (!requestId .equals (response .correlationId ())) {
478
487
throw new AmqpException ("Unexpected correlation ID" );
479
488
}
480
- int responseCode = request .mapResponse ().code ();
489
+ int responseCode = request .response ().code ();
490
+ String explanation =
491
+ request .response ().body () instanceof String ? (String ) request .response ().body () : null ;
481
492
if (IntStream .of (expectedResponseCodes ).noneMatch (c -> c == responseCode )) {
482
- if (responseCode == CODE_404 ) {
483
- throw new AmqpException .AmqpEntityDoesNotExistException ("Entity does not exist" );
493
+ if (responseCode == CODE_404
494
+ || (responseCode == CODE_400 && queueDoesNotExist (explanation ))) {
495
+ explanation = explanation == null ? "Entity does not exist" : explanation ;
496
+ throw new AmqpException .AmqpEntityDoesNotExistException (explanation );
484
497
} else {
485
498
String message =
486
499
String .format (
487
- "Unexpected response code: %d instead of %s" ,
500
+ "Unexpected response code: %d instead of %s%s " ,
488
501
responseCode ,
489
502
IntStream .of (expectedResponseCodes )
490
503
.mapToObj (String ::valueOf )
491
- .collect (Collectors .joining (", " )));
504
+ .collect (Collectors .joining (", " )),
505
+ explanation != null ? " (message: '" + explanation : "')" );
492
506
try {
493
507
LOGGER .info (
494
508
"Management request failed: '{}'. Response body: '{}'" ,
@@ -502,6 +516,11 @@ private static void checkResponse(
502
516
}
503
517
}
504
518
519
+ private static boolean queueDoesNotExist (String explanation ) {
520
+ return explanation != null
521
+ && (explanation .contains ("no queue '" ) && explanation .contains ("in vhost '" ));
522
+ }
523
+
505
524
void bind (Map <String , Object > body ) {
506
525
declare (body , "/bindings" , POST , CODE_204 );
507
526
}
@@ -637,6 +656,10 @@ private <K, V> Response<Map<K, V>> mapResponse() {
637
656
return (Response <Map <K , V >>) this .response .get ();
638
657
}
639
658
659
+ private Response <?> response () {
660
+ return this .response .get ();
661
+ }
662
+
640
663
@ SuppressWarnings ("unchecked" )
641
664
private <K , V > Map <K , V > responseBodyAsMap () throws ClientException {
642
665
return (Map <K , V >) this .responseMessage .get ().body ();
0 commit comments