Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transport » tcp » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.activemq.transport.tcp;
   18   
   19   import org.apache.activemq.command.Response;
   20   
   21   /**
   22    * ResponseHolder utility
   23    * 
   24    * @version $Revision: 1.1.1.1 $
   25    */
   26   public class ResponseHolder {
   27       protected Response response;
   28       protected Object lock = new Object();
   29       protected boolean notified;
   30   
   31       /**
   32        * Construct a receipt holder
   33        */
   34       public ResponseHolder() {
   35       }
   36   
   37       /**
   38        * Set the Response for this holder
   39        * 
   40        * @param r
   41        */
   42       public void setResponse(Response r) {
   43           synchronized (lock) {
   44               this.response = r;
   45               notified = true;
   46               lock.notify();
   47           }
   48       }
   49   
   50       /**
   51        * Get the Response
   52        * 
   53        * @return the Response or null if it is closed
   54        */
   55       public Response getResponse() {
   56           return getResponse(0);
   57       }
   58   
   59       /**
   60        * wait upto <Code>timeout</Code> timeout ms to get a receipt
   61        * 
   62        * @param timeout
   63        * @return
   64        */
   65       public Response getResponse(int timeout) {
   66           synchronized (lock) {
   67               if (!notified) {
   68                   try {
   69                       lock.wait(timeout);
   70                   } catch (InterruptedException e) {
   71                       Thread.currentThread().interrupt();
   72                   }
   73               }
   74           }
   75           return this.response;
   76       }
   77   
   78       /**
   79        * close this holder
   80        */
   81       public void close() {
   82           synchronized (lock) {
   83               notified = true;
   84               lock.notifyAll();
   85           }
   86       }
   87   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transport » tcp » [javadoc | source]