View Javadoc

1   /*
2    * Copyright (c) 2010.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.client;
22  
23  import org.apache.hadoop.hbase.hbql.impl.AsyncExecutorImpl;
24  import org.apache.hadoop.hbase.hbql.impl.Utils;
25  import org.apache.hadoop.hbase.hbql.util.Maps;
26  
27  import java.util.Map;
28  import java.util.Set;
29  
30  public class AsyncExecutorManager {
31  
32      public static int defaultMinThreadCount = 1;
33      public static int defaultMaxThreadCount = 10;
34      public static long defaultKeepAliveSecs = Long.MAX_VALUE;
35  
36      private static Map<String, AsyncExecutorImpl> executorMap = Maps.newConcurrentHashMap();
37  
38      private static Map<String, AsyncExecutorImpl> getExecutorMap() {
39          return AsyncExecutorManager.executorMap;
40      }
41  
42      public static AsyncExecutor newAsyncExecutor(final String name,
43                                                   final int minThreadCount,
44                                                   final int maxThreadCount,
45                                                   final long keepAliveSecs) throws HBqlException {
46  
47          if (Utils.isValidString(name) && getExecutorMap().containsKey(name))
48              throw new HBqlException("AsyncExecutor already exists: " + name);
49  
50          final AsyncExecutorImpl asyncExecutor = new AsyncExecutorImpl(name,
51                                                                        minThreadCount,
52                                                                        maxThreadCount,
53                                                                        keepAliveSecs);
54          AsyncExecutorManager.getExecutorMap().put(asyncExecutor.getName(), asyncExecutor);
55  
56          return asyncExecutor;
57      }
58  
59      public static boolean dropAsyncExecutor(final String name) {
60  
61          if (Utils.isValidString(name) && getExecutorMap().containsKey(name)) {
62              final AsyncExecutorImpl asyncExecutor = getExecutorMap().remove(name);
63              asyncExecutor.shutdown();
64              return true;
65          }
66  
67          return false;
68      }
69  
70      public static boolean asyncExecutorExists(final String name) {
71          return Utils.isValidString(name) && AsyncExecutorManager.getExecutorMap().containsKey(name);
72      }
73  
74      public static AsyncExecutor getAsyncExecutor(final String name) throws HBqlException {
75          if (!AsyncExecutorManager.getExecutorMap().containsKey(name))
76              throw new HBqlException("AsyncExecutor does not exist: " + name);
77  
78          return AsyncExecutorManager.getExecutorMap().get(name);
79      }
80  
81      public static Set<String> getAsyncExecutorNames() {
82          return AsyncExecutorManager.getExecutorMap().keySet();
83      }
84  }