KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > ResponseHolder


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.tcp;
19
20 import org.apache.activemq.command.Response;
21
22 /**
23  * ResponseHolder utility
24  *
25  * @version $Revision: 1.1.1.1 $
26  */

27 public class ResponseHolder {
28     protected Response response;
29     protected Object JavaDoc lock = new Object JavaDoc();
30     protected boolean notified;
31
32     /**
33      * Construct a receipt holder
34      */

35     public ResponseHolder() {
36     }
37
38     /**
39      * Set the Response for this holder
40      *
41      * @param r
42      */

43     public void setResponse(Response r) {
44         synchronized (lock) {
45             this.response = r;
46             notified = true;
47             lock.notify();
48         }
49     }
50
51     /**
52      * Get the Response
53      *
54      * @return the Response or null if it is closed
55      */

56     public Response getResponse() {
57         return getResponse(0);
58     }
59
60     /**
61      * wait upto <Code>timeout</Code> timeout ms to get a receipt
62      *
63      * @param timeout
64      * @return
65      */

66     public Response getResponse(int timeout) {
67         synchronized (lock) {
68             if (!notified) {
69                 try {
70                     lock.wait(timeout);
71                 }
72                 catch (InterruptedException JavaDoc e) {
73                     Thread.currentThread().interrupt();
74                 }
75             }
76         }
77         return this.response;
78     }
79
80     /**
81      * close this holder
82      */

83     public void close() {
84         synchronized (lock) {
85             notified = true;
86             lock.notifyAll();
87         }
88     }
89 }
90
Popular Tags