ReceiptHolder xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.message;
    19
    20  /***
    21   * ReceiptHolder is a utility class used for waiting for receipts from Packets sent to the broker
    22   *
    23   * @version $Revision: 1.5 $
    24   */
    25  public class ReceiptHolder {
    26      private Receipt receipt;
    27      private Object lock = new Object();
    28      private boolean notified;
    29
    30      /***
    31       * Construct a receipt holder
    32       */
    33      public ReceiptHolder() {
    34      }
    35
    36      /***
    37       * Set the Receipt for this holder
    38       *
    39       * @param r
    40       */
    41      public void setReceipt(Receipt r) {
    42          synchronized (lock) {
    43              this.receipt = r;
    44              notified = true;
    45              lock.notify();
    46          }
    47      }
    48
    49      /***
    50       * Get the Receipt
    51       *
    52       * @return the Receipt or null if it is closed
    53       */
    54      public Receipt getReceipt() {
    55          return getReceipt(0);
    56      }
    57
    58      /***
    59       * wait upto <Code>timeout</Code> timeout ms to get a receipt
    60       *
    61       * @param timeout
    62       * @return
    63       */
    64      public Receipt getReceipt(int timeout) {
    65          synchronized (lock) {
    66              if (!notified) {
    67                  try {
    68                      lock.wait(timeout);
    69                  }
    70                  catch (InterruptedException e) {
    71                      e.printStackTrace();
    72                  }
    73              }
    74          }
    75          return this.receipt;
    76      }
    77
    78      /***
    79       * close this holder
    80       */
    81      public void close() {
    82          synchronized (lock) {
    83              notified = true;
    84              lock.notifyAll();
    85          }
    86      }
    87  }