KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > FutureResponse


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;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InterruptedIOException JavaDoc;
22
23 import org.apache.activemq.command.Response;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.util.concurrent.ArrayBlockingQueue JavaDoc;
28 import java.util.concurrent.TimeUnit JavaDoc;
29
30 public class FutureResponse {
31     private static final Log log = LogFactory.getLog(FutureResponse.class);
32            
33     private final ResponseCallback responseCallback;
34     private final ArrayBlockingQueue JavaDoc responseSlot = new ArrayBlockingQueue JavaDoc(1);
35     
36     public FutureResponse(ResponseCallback responseCallback) {
37         this.responseCallback = responseCallback;
38     }
39
40     public Response getResult() throws IOException JavaDoc {
41         try {
42             return (Response) responseSlot.take();
43         }
44         catch (InterruptedException JavaDoc e) {
45             Thread.currentThread().interrupt();
46             if (log.isDebugEnabled()) {
47                 log.debug("Operation interupted: " + e, e);
48             }
49             throw new InterruptedIOException JavaDoc("Interrupted.");
50         }
51     }
52     
53     public Response getResult(int timeout) throws IOException JavaDoc {
54         try {
55             return (Response) responseSlot.poll(timeout,TimeUnit.MILLISECONDS);
56         } catch (InterruptedException JavaDoc e) {
57             throw new InterruptedIOException JavaDoc("Interrupted.");
58         }
59     }
60     
61     public void set(Response result) {
62         if( responseSlot.offer(result) ) {
63             if( responseCallback !=null ) {
64                 responseCallback.onCompletion(this);
65             }
66         }
67     }
68 }
69
Popular Tags