您的位置:首页 > 其它

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

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

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

[java]
view plaincopy

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

[java]
view plaincopy

public static Configuration addHbaseResources(Configuration conf) {
conf.addResource("hbase-default.xml");
conf.addResource("hbase-site.xml");
checkDefaultsVersion(conf);
checkForClusterFreeMemoryLimit(conf);
return conf;
}

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

[java]
view plaincopy

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中:

[java]
view plaincopy

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之前需要设置类加载器:

[java]
view plaincopy

ClassLoader ocl = Thread.currentThread().getContextClassLoader();

try {

Thread.currentThread().setContextClassLoader(HBaseConfiguration.class.getClassLoader());

Configuration conf = HBaseConfiguration.create();
... ...
} finally {

Thread.currentThread().setContextClassLoader(ocl);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐