论坛首页 Java企业应用论坛

使用JIOPi模块库实现类库自动升级

浏览 1758 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-29   最后修改:2010-04-29

JIOPi规范定义了区分版本的标准JIOPi模块库,只需将本应部署在lib目录的Jar包放在JIOPi模块库中,并使用JIOPi风格访问,当在JIOPi模块库中添加一个新的版本时,所有使用JIOPi风格使用该模块库的程序都将自动使用新版本的程序,而无需进行Jar包替换。 

下面就以将apache httpclient 4.0.1发布到JIOPi模块库为例,展示如何以分离的模式部署Jar类库程序,并实现类库程序的自动升级。 

使用JIOPi模块库除了可以实现类库的自动升级,根据JIOPi对模块隔离运行的设计,在一个系统中可以同时运行一个类库程序的多个版本,这就如同一个Servlet容器运行多个webapp而互不影响是一样的实现原理。 

要使用 apache httpclient 4.0.1,你需要在lib下放3个jar:

httpclient-4.0.1.jar

httpcore-4.0.1.jar

commons-logging-1.1.1.jar

如果直接将这三个jar放在lib目录,你的使用代码如下: 

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient;
public class HTTPClientTest {
	public static void main(String[] args) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpHead httphead = new HttpHead("http://www.iteye.com/images/logo.gif");
		//HttpHead httphead = new HttpHead("http://groups.google.com/groups/img/gicons/discuss.jpg");
		try {
			HttpResponse response = httpclient.execute(httphead);
			StatusLine statusLine = response.getStatusLine();
			String status = statusLine.toString();
			System.out.println(status);
		} catch (IOException e) {
			e.printStackTrace();
		}
		httpclient.getConnectionManager().shutdown();
	}
}

 

 

这是标准Java代码风格,

不过当你需要对httpclient的版本进行升级时,你需要替换掉你所有项目的lib目录中对应的jar包 

 

使用JIOPi标准模块库,你则只需替换一个地方即可。

JIOPi标准模块库可以是任何一个可以被容器支持的URL,iBean目前支持http模块库和本地模块库。 

 

下面我们就来看看如何将这3个Jar放入JIOPi模块库并用JIOPi风格进行访问

 

 

添加模块库

要在JIOPi中添加一个新的模块库,只需要在jiopi.properties文件中添加两行

jiopi.resourcepool.org-jiopi.path=http://jiopi.vip7.es163.net/ibean/resourcepools/jiopi-ibean.xml

jiopi.resourcepool.org-jiopi.pri=1

其中org-jiopi是模块库的自定义名,只需保证同一配置文件不重名即可

path定义模块库的配置文件地址

pri定义模块库的优先级

运行时JIOPi容器会按优先级顺序从多个标准模块库中搜索符合条件的模块 

 

标准模块库配置文件

下面就要建立你的模块库配置,建议将模块库的配置文件放在模块库根目录

模块库配置文件如下,可以不用更改直接使用 

<?xml version="1.0" encoding="UTF-8"?>
<resourcepools xmlns="http://www.jiopi.org/2010/JIOPISchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jiopi.org/2010/JIOPISchema xsd/jiopi-config-resourcepools.xsd ">
  <modules url-format="${module}/${module}.xml"/>
</resourcepools> 

 

 

其中 url-format 指定了当访问一个模块时,模块的配置文件的位置,这里指定的是配置文件目录下的 module/module.xml

即当容器在该模块库搜索 模块 apache.httpclient 模块时,将会尝试获取 apache.httpclient/apache.httpclient.xml文件 

 

创建模块配置文件

1 在模块库根目录创建 文件夹 apache.httpclient

2 在新建的文件夹内创建 apache.httpclient.xml文件,内容如下: 

<?xml version="1.0" encoding="UTF-8"?>
<module name="modulename" xmlns="http://www.jiopi.org/2010/JIOPISchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jiopi.org/2010/JIOPISchema xsd/jiopi-config-module.xsd ">
      <release version="4.0.1.0" base-url="4.0.1.0" reference="apache.httpclient-4.0.1.0.xml"/>
</module> 

 

 

其中每个版本一个release元素,由于httpclient采取的是3位版本号,而JIOPi模块库的模块需要4位版本号,因此第4为补0

version 指定版本

base-url 指明当前版本所在的文件夹为 4.0.1.0 ,配置文件在该文件夹下,名为 apache.httpclient-4.0.1.0.xml 

 

创建版本配置文件

1 在模块目录创建 4.0.1.0 文件夹

2 将 该模块的3个Jar包放在该文件夹下

3 创建 apache.httpclient-4.0.1.0.xml 内容如下: 

 

<?xml version="1.0" encoding="UTF-8"?>
<release version="4.0.1.0" xmlns="http://www.jiopi.org/2010/JIOPISchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jiopi.org/2010/JIOPISchema xsd/jiopi-config-module-release.xsd ">
      <resource>httpcore-4.0.1.jar</resource>
      <resource>commons-logging-1.1.1.jar</resource>
       <resource>httpclient-4.0.1.jar</resource>
</release> 

 

 

这样便可以使用JIOPi风格访问 apache httpclient 4.0.1了

当有新版本时,如 4.0.2,则修改apache.httpclient.xml文件,增加一条新的 release 元素及配置该release

则所有使用该模块库的JIOPi风格的程序都将自动升级到 4.0.2(在程序重启后生效,不会立刻生效) 

 

此时资源库的目录结构如下:

+apache.httpclient

     +4.0.1.0

          apache.httpclient-4.0.1.0.xml

          commons-logging-1.1.1.jar

          httpclient-4.0.1.jar

          httpcore-4.0.1.jar

     apache.httpclient.xml

jiopi-ibean.xml

 

要使用该模块库

模块库

http://jiopi.vip7.es163.net/ibean/resourcepools/jiopi-ibean.xml

已经配置了 apache httpclient 4.0.1你只需在你的应用中部署iBean.jar和放置jiopi.propertiese文件即可书写JIOPi免部署风格的程序使用该类库

代码如下 

 

import org.jiopi.framework.CentralConsole;
import org.jiopi.framework.ModuleConsole;
import org.jiopi.framework.ControlPanel;
import org.jiopi.framework.FrameworkInitializer;
public class HTTPClientTest {
	public static void main(String[] args) {
		FrameworkInitializer.initialize();
		ModuleConsole apacheHttpclient = CentralConsole.accessModuleConsole("apache.httpclient");
		ControlPanel httpclient = apacheHttpclient.accessControlPanel("org.apache.http.impl.client.DefaultHttpClient", ControlPanel.class);
		ControlPanel httphead = apacheHttpclient.accessControlPanel("org.apache.http.client.methods.HttpHead",ControlPanel.class,"http://www.iteye.com/images/logo.gif");
		//ControlPanel httphead = apacheHttpclient.accessControlPanel("org.apache.http.client.methods.HttpHead",ControlPanel.class,"http://groups.google.com/groups/img/gicons/discuss.jpg");
		try {
			ControlPanel response = httpclient.operate("execute", ControlPanel.class, httphead);
			ControlPanel statusLine = response.operate("getStatusLine", ControlPanel.class);
			String status = statusLine.operate("toString", String.class);
			System.out.println(status);
		} catch (Exception e) {
			e.printStackTrace();
		}
		httpclient.operate("getConnectionManager", ControlPanel.class).operate("shutdown",void.class);
	}
}

 这是一段所见即所得的代码,只要项目内部放了iBean的jar包(默认配置的模块库包含了apache httpclient 4.0.1),则直接复制该段代码即可执行 

为了能够获得较好的Java代码风格,应当将使用API和实现分离,以便可以只部署API,并使用IoC框架(如Spring)进行实现对象的依赖注入,JIOPi的API则交由IoC框架来调用,避免程序中出现JIOPi的代码和反射方式调用,提高程序的执行效率和可读性 

但是apache httpclient 4.0.1并没有将 调用API和实现类有较为明确的划分(理想情况应当是 API一个jar,实现类一个或多个jar),所以这里无法给出使用API部署风格的调用代码。 

理解JIOPi标准模块库

JIOPi标准模块库提供了一种类库Jar分离式部署的方式,以避免项目lib目录的Jar包混乱,在API部署模式下,也可以将JIOPi的API看成一种从容器获取对象的方式,就如同Spring中调用 ApplicationContext的 getBean方法一样,只不过Spring将类名写在了配置文件中,使用id来获取对象,在JIOPi 0.1中,需要写全类名而已,当然,为了统一,你可以将Spring配置中应当配置类名的地方配置成JIOPi的API静态方法调用,从而可以从JIOPi标准模块库中获取对象,而不是从项目的ClassPath中获取对象,从而实现类库的分布式部署和自动升级。 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics