@@ -121,10 +121,8 @@ public ChannelN(AMQConnection connection, int channelNumber,
121121 * @throws IOException if any problem is encountered
122122 */
123123 public void open () throws IOException {
124- // wait for the Channel.OpenOk response, then ignore it
125- Channel .OpenOk openOk =
126- (Channel .OpenOk ) exnWrappingRpc (new Channel .Open (UNSPECIFIED_OUT_OF_BAND )).getMethod ();
127- Utility .use (openOk );
124+ // wait for the Channel.OpenOk response, and ignore it
125+ exnWrappingRpc (new Channel .Open (UNSPECIFIED_OUT_OF_BAND ));
128126 }
129127
130128 public void addReturnListener (ReturnListener listener ) {
@@ -167,6 +165,17 @@ public void clearConfirmListeners() {
167165 public boolean waitForConfirms ()
168166 throws InterruptedException
169167 {
168+ boolean confirms = false ;
169+ try {
170+ confirms = waitForConfirms (0L );
171+ } catch (TimeoutException e ) { }
172+ return confirms ;
173+ }
174+
175+ /** {@inheritDoc} */
176+ public boolean waitForConfirms (long timeout )
177+ throws InterruptedException , TimeoutException {
178+ long startTime = System .currentTimeMillis ();
170179 synchronized (unconfirmedSet ) {
171180 while (true ) {
172181 if (getCloseReason () != null ) {
@@ -177,7 +186,16 @@ public boolean waitForConfirms()
177186 onlyAcksReceived = true ;
178187 return aux ;
179188 }
180- unconfirmedSet .wait ();
189+ if (timeout == 0L ) {
190+ unconfirmedSet .wait ();
191+ } else {
192+ long elapsed = System .currentTimeMillis () - startTime ;
193+ if (timeout > elapsed ) {
194+ unconfirmedSet .wait (timeout - elapsed );
195+ } else {
196+ throw new TimeoutException ();
197+ }
198+ }
181199 }
182200 }
183201 }
@@ -186,9 +204,23 @@ public boolean waitForConfirms()
186204 public void waitForConfirmsOrDie ()
187205 throws IOException , InterruptedException
188206 {
189- if (!waitForConfirms ()) {
190- close (AMQP .REPLY_SUCCESS , "NACKS RECEIVED" , true , null , false );
191- throw new IOException ("nacks received" );
207+ try {
208+ waitForConfirmsOrDie (0L );
209+ } catch (TimeoutException e ) { }
210+ }
211+
212+ /** {@inheritDoc} */
213+ public void waitForConfirmsOrDie (long timeout )
214+ throws IOException , InterruptedException , TimeoutException
215+ {
216+ try {
217+ if (!waitForConfirms (timeout )) {
218+ close (AMQP .REPLY_SUCCESS , "NACKS RECEIVED" , true , null , false );
219+ throw new IOException ("nacks received" );
220+ }
221+ } catch (TimeoutException e ) {
222+ close (AMQP .PRECONDITION_FAILED , "TIMEOUT WAITING FOR ACK" );
223+ throw (e );
192224 }
193225 }
194226
@@ -919,8 +951,7 @@ public void basicCancel(final String consumerTag)
919951 throw new IOException ("Unknown consumerTag" );
920952 BlockingRpcContinuation <Consumer > k = new BlockingRpcContinuation <Consumer >() {
921953 public Consumer transformReply (AMQCommand replyCommand ) {
922- Basic .CancelOk dummy = (Basic .CancelOk ) replyCommand .getMethod ();
923- Utility .use (dummy );
954+ replyCommand .getMethod ();
924955 _consumers .remove (consumerTag ); //may already have been removed
925956 dispatcher .handleCancelOk (originalConsumer , consumerTag );
926957 return originalConsumer ;
@@ -930,8 +961,7 @@ public Consumer transformReply(AMQCommand replyCommand) {
930961 rpc (new Basic .Cancel (consumerTag , false ), k );
931962
932963 try {
933- Consumer callback = k .getReply ();
934- Utility .use (callback );
964+ k .getReply (); // discard result
935965 } catch (ShutdownSignalException ex ) {
936966 throw wrap (ex );
937967 }
0 commit comments