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  package org.apache.hadoop.hbase.ipc;
19  
20  import com.google.common.annotations.VisibleForTesting;
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.client.MetricsConnection;
24  import org.apache.hadoop.hbase.util.ReflectionUtils;
25  
26  import java.net.SocketAddress;
27  
28  /**
29   * Factory to create a {@link org.apache.hadoop.hbase.ipc.RpcClient}
30   */
31  @InterfaceAudience.Private
32  public final class RpcClientFactory {
33  
34    public static final String CUSTOM_RPC_CLIENT_IMPL_CONF_KEY = "hbase.rpc.client.impl";
35  
36    /**
37     * Private Constructor
38     */
39    private RpcClientFactory() {
40    }
41  
42    /** Helper method for tests only. Creates an {@code RpcClient} without metrics. */
43    @VisibleForTesting
44    public static RpcClient createClient(Configuration conf, String clusterId) {
45      return createClient(conf, clusterId, null);
46    }
47  
48    /**
49     * Creates a new RpcClient by the class defined in the configuration or falls back to
50     * RpcClientImpl
51     * @param conf configuration
52     * @param clusterId the cluster id
53     * @param metrics the connection metrics
54     * @return newly created RpcClient
55     */
56    public static RpcClient createClient(Configuration conf, String clusterId,
57        MetricsConnection metrics) {
58      return createClient(conf, clusterId, null, metrics);
59    }
60  
61    /**
62     * Creates a new RpcClient by the class defined in the configuration or falls back to
63     * RpcClientImpl
64     * @param conf configuration
65     * @param clusterId the cluster id
66     * @param localAddr client socket bind address.
67     * @param metrics the connection metrics
68     * @return newly created RpcClient
69     */
70    public static RpcClient createClient(Configuration conf, String clusterId,
71        SocketAddress localAddr, MetricsConnection metrics) {
72      String rpcClientClass =
73          conf.get(CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
74            RpcClientImpl.class.getName());
75      return ReflectionUtils.instantiateWithCustomCtor(
76          rpcClientClass,
77          new Class[] { Configuration.class, String.class, SocketAddress.class,
78              MetricsConnection.class },
79          new Object[] { conf, clusterId, localAddr, metrics }
80      );
81    }
82  }