hbase进行osgi bundle化以后配置文件加载问题

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);

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值