hbase如果要用到osgi环境中,需要进行bundle化,但是有一点比较特别的是其配置文件hbase-default.xml的加载。
通过HBaseConfiguration.create()实例化HBaseConfiguration以后,addHbaseResources会去加载hbase-default.xml以及hbase-site.xml
- public HBaseConfiguration() {
- //TODO:replace with private constructor, HBaseConfiguration should not extend Configuration
- super();
- addHbaseResources(this);
- LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use" +
- " HBaseConfiguration#create() to construct a plain Configuration");
- }
- public static Configuration addHbaseResources(Configuration conf) {
- conf.addResource("hbase-default.xml");
- conf.addResource("hbase-site.xml");
- checkDefaultsVersion(conf);
- checkForClusterFreeMemoryLimit(conf);
- return conf;
- }
完了之后还要通过checkDefaultsVersion来check 配置文件
- private static void checkDefaultsVersion(Configuration conf) {
- // if (true) return; // REMOVE
- if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
- String defaultsVersion = conf.get("hbase.defaults.for.version");
- String thisVersion = VersionInfo.getVersion();
- if (!thisVersion.equals(defaultsVersion)) {
- throw new RuntimeException(
- "hbase-default.xml file seems to be for and old version of HBase (" +
- defaultsVersion + "), this version is " + thisVersion);
- }
- }
问题的关键是配置文件的加载过程,由于conf是org.apache.hadoop.conf.Configuration的实例,而查阅hadoop源码可知,在其Configuration.java中:
- static{
- //print deprecation warning if hadoop-site.xml is found in classpath
- ClassLoader cL = Thread.currentThread().getContextClassLoader();
- if (cL == null) {
- cL = Configuration.class.getClassLoader();
- }
- if(cL.getResource("hadoop-site.xml")!=null) {
- LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
- "Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
- + "mapred-site.xml and hdfs-site.xml to override properties of " +
- "core-default.xml, mapred-default.xml and hdfs-default.xml " +
- "respectively");
- }
- addDefaultResource("core-default.xml");
- addDefaultResource("core-site.xml");
- }
即加载配置文件的class loader被设置为了当前线程上下文类加载器,在非osgi环境中,问题可能不大,类加载器基本都用一个,而osgi中很可能出现问题,比如把hbase单独封装成bundle,而另外一个bundle A依赖于hbase bundle, 当初始化hbase的时候会有问题,当前线程类加载器是bundle A加载器,而hbase-default.xml对其是不可见的,因此
在bundle A使用hbase之前需要设置类加载器:
- ClassLoader ocl = Thread.currentThread().getContextClassLoader();
- try {
- Thread.currentThread().setContextClassLoader(HBaseConfiguration.class.getClassLoader());
- Configuration conf = HBaseConfiguration.create();
- ... ...
- } finally {
- Thread.currentThread().setContextClassLoader(ocl);
- }