hbase进行osgi bundle化以后配置文件加载问题
2012-12-25 14:46
337 查看
hbase如果要用到osgi环境中,需要进行bundle化,但是有一点比较特别的是其配置文件hbase-default.xml的加载。
通过HBaseConfiguration.create()实例化HBaseConfiguration以后,addHbaseResources会去加载hbase-default.xml以及hbase-site.xml
完了之后还要通过checkDefaultsVersion来check 配置文件
问题的关键是配置文件的加载过程,由于conf是org.apache.hadoop.conf.Configuration的实例,而查阅hadoop源码可知,在其Configuration.java中:
即加载配置文件的class loader被设置为了当前线程上下文类加载器,在非osgi环境中,问题可能不大,类加载器基本都用一个,而osgi中很可能出现问题,比如把hbase单独封装成bundle,而另外一个bundle A依赖于hbase bundle, 当初始化hbase的时候会有问题,当前线程类加载器是bundle A加载器,而hbase-default.xml对其是不可见的,因此
在bundle A使用hbase之前需要设置类加载器:
通过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); }
相关文章推荐
- hbase进行osgi bundle化以后配置文件加载问题
- AutoCAD.Net/C#.Net QQ群:193522571 多个框架共一套代码在进行迁移时的问题,properties,resource,未能加载文件或程序集“System.Drawing, Version=4.0.0.0
- eclipse中web项目部署以后jsp的java文件找不到问题(Tomcat配置serverlocations)
- java动态加载配置文件不成功的问题
- 同时配置redis和hbase的配置文件遇到的问题
- chrome hosts文件管理工具 空白问题的解决--插件无法加载hosts配置文件
- springboot配置文件加载不到的问题
- SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】
- Spring 加载xml配置文件路径问题
- 关于VS2008单元测试中加载配置文件的问题
- 设置php在apache下加载ini配置文件路径,~和curl扩展无法加载的问题
- <util:properties/>加载的配置文件中有中文导致乱码,如何通过分析源码解决问题?
- HbaseTemplate进阶:利用hbase.properties文件对Hbase的集群和端口进行配置
- [logback] 关于logback.xml加载外部配置文件问题
- 解决安装MySQL进行写入配置文件时卡住或报错问题
- 加载配置文件之路径问题
- myeclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题
- IIS配置遇到的问题——未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序。
- 用加载字符串配置文件的方式解决cocos2D-x项目中文乱码问题
- 关于logback.xml加载外部配置文件问题