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

hbase如果要用到osgi环境中,需要进行bundle化,但是有一点比较特别的是其配置文件hbase-default.xml的加载。

通过HBaseConfiguration.create()实例化HBaseConfiguration以后,addHbaseResources会去加载hbase-default.xml以及hbase-site.xml

  1. public HBaseConfiguration() {  
  2.     //TODO:replace with private constructor, HBaseConfiguration should not extend Configuration  
  3.     super();  
  4.     addHbaseResources(this);  
  5.     LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use" +  
  6.             " HBaseConfiguration#create() to construct a plain Configuration");  
  7.   }  

  1. public static Configuration addHbaseResources(Configuration conf) {  
  2.    conf.addResource("hbase-default.xml");  
  3.    conf.addResource("hbase-site.xml");  
  4.    checkDefaultsVersion(conf);  
  5.    checkForClusterFreeMemoryLimit(conf);  
  6.    return conf;  
  7.  }  

完了之后还要通过checkDefaultsVersion来check 配置文件

  1. private static void checkDefaultsVersion(Configuration conf) {  
  2. //    if (true) return; // REMOVE  
  3.    if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;  
  4.    String defaultsVersion = conf.get("hbase.defaults.for.version");  
  5.    String thisVersion = VersionInfo.getVersion();  
  6.    if (!thisVersion.equals(defaultsVersion)) {  
  7.      throw new RuntimeException(  
  8.        "hbase-default.xml file seems to be for and old version of HBase (" +  
  9.        defaultsVersion + "), this version is " + thisVersion);  
  10.    }  
  11.  }  

问题的关键是配置文件的加载过程,由于conf是org.apache.hadoop.conf.Configuration的实例,而查阅hadoop源码可知,在其Configuration.java中:

  1. static{  
  2.     //print deprecation warning if hadoop-site.xml is found in classpath  
  3.     ClassLoader cL = Thread.currentThread().getContextClassLoader();  
  4.     if (cL == null) {  
  5.       cL = Configuration.class.getClassLoader();  
  6.     }  
  7.     if(cL.getResource("hadoop-site.xml")!=null) {  
  8.       LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +  
  9.           "Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "  
  10.           + "mapred-site.xml and hdfs-site.xml to override properties of " +  
  11.           "core-default.xml, mapred-default.xml and hdfs-default.xml " +  
  12.           "respectively");  
  13.     }  
  14.     addDefaultResource("core-default.xml");  
  15.     addDefaultResource("core-site.xml");  
  16.   }  

即加载配置文件的class loader被设置为了当前线程上下文类加载器,在非osgi环境中,问题可能不大,类加载器基本都用一个,而osgi中很可能出现问题,比如把hbase单独封装成bundle,而另外一个bundle A依赖于hbase bundle, 当初始化hbase的时候会有问题,当前线程类加载器是bundle A加载器,而hbase-default.xml对其是不可见的,因此

在bundle A使用hbase之前需要设置类加载器:

  1.  ClassLoader ocl = Thread.currentThread().getContextClassLoader();  
  2.   
  3. try {  
  4.   
  5. Thread.currentThread().setContextClassLoader(HBaseConfiguration.class.getClassLoader());  
  6.   
  7. Configuration conf = HBaseConfiguration.create();  
  8. ... ...  
  9.  } finally {  
  10.   
  11. Thread.currentThread().setContextClassLoader(ocl);  
  12.   
  13. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值