View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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  
19  package org.apache.hadoop.hbase.ipc;
20  
21  
22  import com.google.protobuf.RpcCallback;
23  import com.google.protobuf.RpcController;
24  
25  import java.util.concurrent.atomic.AtomicBoolean;
26  import java.util.concurrent.atomic.AtomicReference;
27  
28  public class TimeLimitedRpcController implements RpcController {
29  
30    /**
31     * The time, in ms before the call should expire.
32     */
33    protected volatile Integer callTimeout;
34    protected volatile boolean cancelled = false;
35    protected final AtomicReference<RpcCallback<Object>> cancellationCb =
36        new AtomicReference<RpcCallback<Object>>(null);
37  
38    public Integer getCallTimeout() {
39      return callTimeout;
40    }
41  
42    public void setCallTimeout(int callTimeout) {
43      this.callTimeout = callTimeout;
44    }
45  
46    public boolean hasCallTimeout(){
47      return callTimeout != null;
48    }
49  
50    @Override
51    public String errorText() {
52      throw new UnsupportedOperationException();
53    }
54  
55    @Override
56    public boolean failed() {
57      throw new UnsupportedOperationException();
58    }
59  
60    @Override
61    public boolean isCanceled() {
62      return cancelled;
63    }
64  
65    @Override
66    public void notifyOnCancel(RpcCallback<Object> cancellationCb) {
67      this.cancellationCb.set(cancellationCb);
68    }
69  
70    @Override
71    public void reset() {
72      throw new UnsupportedOperationException();
73    }
74  
75    @Override
76    public void setFailed(String arg0) {
77      throw new UnsupportedOperationException();
78    }
79  
80    @Override
81    public void startCancel() {
82      cancelled = true;
83      if (cancellationCb.get() != null) {
84        cancellationCb.get().run(null);
85      }
86    }
87  }