List forDLQ = null;
   public void cancelDeliveries(List cancels) throws JMSException   {      try      {         // deliveries must be cancelled in reverse order                    List forDLQ = null;                  Set channels = new HashSet();                                    for (int i = cancels.size() - 1; i >= 0; i--)         {            Cancel cancel = (Cancel)cancels.get(i);                        Delivery del = (Delivery)deliveries.remove(new Long(cancel.getDeliveryId()));                        if (del == null)            {               throw new IllegalStateException("Cannot find delivery to cancel " + cancel.getDeliveryId());            }                                                if (cancel.getDeliveryCount() >= maxDeliveryAttempts)            {               if (forDLQ == null)               {                  forDLQ = new ArrayList();               }                              forDLQ.add(del);            }            else            {                                                                  del.getReference().setDeliveryCount(cancel.getDeliveryCount());                              del.cancel();                              channels.add(del.getObserver());            }         }                  //Send stuff to DLQ                  if (forDLQ != null)         {            //We do this in a tx so we don't end up with the message in both the original queue            //and the dlq if it fails half way through            Transaction tx = tr.createTransaction();                        try            {                              for (int i = forDLQ.size() - 1; i >= 0; i--)               {                  Delivery del = (Delivery)forDLQ.get(i);                                    if (dlq != null)                  {                              //reset delivery count to zero                     del.getReference().setDeliveryCount(0);                                          dlq.handle(null, del.getReference(), tx);                                          del.acknowledge(tx);                             }                  else                  {                     log.warn("Cannot send to DLQ since DLQ has not been deployed! The message will be removed");                                          del.acknowledge(tx);                  }                                             }                              tx.commit();            }            catch (Throwable t)            {               tx.rollback();                              throw t;            }         }                  // need to prompt delivery for all affected channels                  promptDelivery(channels);      }      catch (Throwable t)      {         throw ExceptionUtil.handleJMSInvocation(t, this + " cancelDeliveries");      }   }         