@@ -68,6 +68,7 @@ class AmqpManagement implements Management {
6868 private static final int CODE_200 = 200 ;
6969 private static final int CODE_201 = 201 ;
7070 private static final int CODE_204 = 204 ;
71+ private static final int CODE_400 = 400 ;
7172 private static final int CODE_404 = 404 ;
7273 private static final int CODE_409 = 409 ;
7374
@@ -173,6 +174,14 @@ public UnbindSpecification unbind() {
173174 return new AmqpBindingManagement .AmqpUnbindSpecification (this );
174175 }
175176
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+
176185 void setToken (String token ) {
177186 if (!this .connection .setTokenSupported ()) {
178187 throw new UnsupportedOperationException ("Token renewal requires at least RabbitMQ 4.1.0" );
@@ -477,18 +486,23 @@ private static void checkResponse(
477486 if (!requestId .equals (response .correlationId ())) {
478487 throw new AmqpException ("Unexpected correlation ID" );
479488 }
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 ;
481492 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 );
484497 } else {
485498 String message =
486499 String .format (
487- "Unexpected response code: %d instead of %s" ,
500+ "Unexpected response code: %d instead of %s%s " ,
488501 responseCode ,
489502 IntStream .of (expectedResponseCodes )
490503 .mapToObj (String ::valueOf )
491- .collect (Collectors .joining (", " )));
504+ .collect (Collectors .joining (", " )),
505+ explanation != null ? " (message: '" + explanation : "')" );
492506 try {
493507 LOGGER .info (
494508 "Management request failed: '{}'. Response body: '{}'" ,
@@ -502,6 +516,11 @@ private static void checkResponse(
502516 }
503517 }
504518
519+ private static boolean queueDoesNotExist (String explanation ) {
520+ return explanation != null
521+ && (explanation .contains ("no queue '" ) && explanation .contains ("in vhost '" ));
522+ }
523+
505524 void bind (Map <String , Object > body ) {
506525 declare (body , "/bindings" , POST , CODE_204 );
507526 }
@@ -637,6 +656,10 @@ private <K, V> Response<Map<K, V>> mapResponse() {
637656 return (Response <Map <K , V >>) this .response .get ();
638657 }
639658
659+ private Response <?> response () {
660+ return this .response .get ();
661+ }
662+
640663 @ SuppressWarnings ("unchecked" )
641664 private <K , V > Map <K , V > responseBodyAsMap () throws ClientException {
642665 return (Map <K , V >) this .responseMessage .get ().body ();
0 commit comments