随笔 - 11  文章 - 79  trackbacks - 0
<2010年2月>
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

欢迎合作

 

联系方式:openhandx@foxmail.com

 

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

 

 

 

 

  OpenHandX-Common1.04

公共开发包

 

 

 

 

 

 

 

 

 

 

2012-10-01

 

作者:彭明华

openhandx@foxmail.com

 

本文档中的任何部分都不得以任何手段任何形式进行复制与传播

 

 

 

目录

1                前言

2                需求

3                JMS连接池

3.1       MessageConfig

3.2       MessageConnectionFactory

3.3       MessageDataSource

3.4       IMessageConnection

3.5       发送消息例子

3.6       接收消息例子

4                多数据源的数据库连接池

5                openHandx-unrealWebService连接池

6                自定义表达式运算

6.1       计算表达式

6.2       自定义函数接口

6.3       部署自定义函数

7                数据模型

7.1       单条数据存储对象

7.1.1         例子

7.2       多条数据存储对象(非持久的)

7.2.1         基本例子

7.2.2       求唯一例子

7.2.3       分组例子

7.2.4       重数据库加载例子

7.2.5       排序例子

7.2.6       统计例子

7.3       多条数据存储对象(持久的)

7.4       复合的数据存储对象

8                xml解析与生成

8.1       xml解析

8.2       xml生成

9                Class方法工具类

10              OpenHandX所有项目介绍

 

 


 

1    前言

OpenHandx-common整理了日常开发中可能经常用到的方法、工具类。其中包括:JMS连接池、多数据源的数据库连接池、自定义表达式运算、数据模型、树结构算法、上下文管理、数据压缩接压、数据加密解密、xml解析与生成、udp传输、数据转换等工具类。Common会随着后续项目的开发而不断补充。

由于common是最基础的工具包使用简单,大家看javadoc就可以了。本文就几个主要工具类的使用进行介绍。

1.04版本的对Model查询功能兼容性大大增强,支持sqlserver、oracle、sybase、mySql几个主流数据库。并增加了对WebService连接池的支持。

2          需求

OpenHandx-common 1.04需要:  

Java 5

commons-beanutils-1.7

commons-logging-1.0.4

db-derby-10.5.3.0 (可选)

geronimo-jms_1.1 (可选)

javassist3.3(可选)

open-unreal1.01(可选)

 

3          JMS连接池

JMS连接池的类路径在open.jms下,可以管理多个服务器的JMS连接,同时也支持多种不同厂商的JMS驱动(jms1.1的规范),开发人员不必了解过多的原来JMS细节就对JMS服务器可以发送和接收消息。

3.1   MessageConfig

open.jms.config.MessageConfig是JMS连接的配置类,保存着要间接JMS服务器的信息,一个MessageConfig配置实例相当于一个jms源。MessageConfig是一个简单的值对象,包含驱动、服务器地址、协议、JND名等。开发人员可以很容易把这些参数从配置文件中读取出来再构造出MessageConfig。

3.2   MessageConnectionFactory

open.jms.factory.MessageConnectionFactory是JMS连接工厂类,是其他工厂类的基础,bind管理多个服务器的JMS连接源,lookup获取一个服务器的JMS连接源,close关闭所有JMS连接源。

3.3   MessageDataSource

open.jms.connection.MessageDataSource是JMS连接源,管理多个JMS connection,由MessageConnectionFactory创建。getConnection获取一个连接IMessageConnection,close关闭当前连接IMessageConnection。

3.4   IMessageConnection

open.jms.connection.IMessageConnection是JMS连接,能获取javax.jms.Connection、消息发送器工厂、消息接收器工厂、消息监听器工厂等,消息发送器工厂能创建MessageQueueSender消息发送器,send可发送序列化的消息对象。消息接收器工厂能创建MessageQueueReceiver消息发送器,receive可接收序列化的消息对象。

3.5   发送消息例子

       try{

           MessageConfig config = new MessageConfig();

           //设置服务器地址

           config.setUrl("tcp://localhost:61616");

           //设置错误监听器类

           config.setExceptionListenerClassName("open.jms.listener.MessageExceptionListener");

           //创建JMS连接池工厂

           MessageConnectionFactory.bind("test", config);

           //获取JMS连接池

           MessageDataSource ds = MessageConnectionFactory.lookup("test");

           //获取JMS连接

           IMessageConnection cnn = ds.getConnection();

           //获取JMS发送器,并在服务器上创建名为query2的队列

           MessageQueueSender sender = cnn.getMessageSenderFactory().createQueueSender("query2");

           long t = System.currentTimeMillis();

           for(int i=0;i<1000;i++){

              //发送消息

              sender.send("0123456789");

           }

           System.out.println("send time:" + (System.currentTimeMillis()-t));

           //关闭JMS连接池工厂

           MessageConnectionFactory.close();

       }catch(Exception e){

           e.printStackTrace();

       }

3.6   接收消息例子

       try{

           MessageConfig config = new MessageConfig();

           //设置服务器地址

           config.setUrl("tcp://localhost:61616");

           //设置错误监听器类

           config.setExceptionListenerClassName("open.jms.listener.MessageExceptionListener");

           //创建JMS连接池工厂

           MessageConnectionFactory.bind("test", config);

           //获取JMS连接池

           MessageDataSource ds = MessageConnectionFactory.lookup("test");

           //获取JMS连接

           IMessageConnection cnn = ds.getConnection();

           //获取JMS接收器,并在服务器上创建名为query2的队列

           MessageQueueReceiver receive = cnn.getMessageReceiverFactory().createQueueReceiver("query2");

           long t = System.currentTimeMillis();

           for(int i=0;i<1001;i++){

              //接收消息

              System.out.println(receive.receive(-1));

           }

           System.out.println("receive time:" + (System.currentTimeMillis()-t));

           //关闭JMS连接池工厂

           MessageConnectionFactory.close();

       }catch(Exception e){

           e.printStackTrace();

       }

 

4          多数据源的数据库连接池

数据库连接池的类路径在open.database下,可以管理多数据源的数据库连接池。每个连接池负责分配、管理和释放数据库连接。并且支持空闲超时、连接中断自动重连。

例子:

       DatabaseConfig param = new DatabaseConfig();

       //指定数据库驱动

       param.setDriver("com.mysql.jdbc.Driver");

       //指定数据库用户密码

       param.setUserName("root");

       param.setPassword("111111");

       //指定数据库地址

       param.setUrl("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK");

       //指定数据库初始化最小连接数

       param.setMinConnection(5);

       //指定数据库初始化最大连接数

       param.setMaxConnection(10);

       //设置数据库最大空闲时间

       param.setConnectionTimeoutValue(20000);

       //指定连接池的名字为pool

       DatabaseConnectionFactory.bind("pool", param);

       System.out.println("bind datasource ok.");

       //以上代码是用来登记一个连接池对象,该操作可以在程序初始化只做一次即可

        //以下开始就是使用者真正需要写的代码

       DatabaseDataSource ds = DatabaseConnectionFactory.lookup("pool");

       try{

           for(int i=0;i<11;i++){

              Connection conn = ds.getConnection();

             

              try{

                  System.out.println(ds.getConnectionCount());

              }finally{

                  try{

                     //conn.close();

                  }catch(Exception e){}

              }

           }

       }catch(Exception e){

           e.printStackTrace();

       }finally{

           DatabaseConnectionFactory.unbind("pool");

           System.out.println("unbind datasource ok.");

           System.exit(0);

       }

 

5          openHandx-unrealWebService连接池

由于每个服务器对外提供的连接数存在上限,即使客户端连接关闭,服务器端也不会立刻释放,这就造成频繁向服务器发送WebService请求会造成拒绝连接的情况,使用WebService连接池很好地解决这类情况。

例子:

       ClientConfig param = new ClientConfig();

       //指定驱动

        param.setDriver("oracle.jdbc.driver.OracleDriver");

        //指定用户密码

       param.setUserName("root");

       param.setPassword("111111");

       param.setProxyHostName("localhost");

       param.setUserName("root");

        //指定地址

       param.setUrl("http://localhost:8080/OpenService");

      

      

       //指定初始化最小连接数

       param.setMinClient(2);

       //指定初始化最大连接数

       param.setMaxClient(10);

 

 

       //指定连接池的名字为pool

       ClientSource ds = ClientFactory.bind("pool", param);

       System.out.println("bind datasource ok.");

       //以上代码是用来登记一个连接池对象,该操作可以在程序初始化只做一次即可

       //以下开始就是使用者真正需要写的代码

       ds = ClientFactory.lookup("pool");

       Thread.sleep(1000);

       System.out.println(ds.getAvailableClientCount() + " , " + ds.getClientCount());

      

       try{

           for(int i=0;i<11;i++){

              IClient client = ds.getClient();

              try{

                  System.out.println(ds.getAvailableClientCount() + " , " + ds.getClientCount());

              }finally{

                  try{

                     client.close();

                  }catch(Exception e){}

              }

              System.out.println(ds.getAvailableClientCount() + " , " + ds.getClientCount());

           }

           IClient client = ds.getClient();

 

       }catch(Exception e){

           e.printStackTrace();

       }finally{

           ClientFactory.unbind("pool");

           System.out.println("unbind datasource ok.");

           System.exit(0);

       }

 

 

 

 

6          自定义表达式运算

自定义表达式运算的类路径在open.calculate下,按照用户自定义计算表达式进行计算,表达式的运算全部是以BigDecimal为基础保证了运算的精度无损失。同时支持加减乘除、括号、逻辑运算、条件判断、正玄函数、余玄函数、绝对值、平方、开方、幂运算、对数、最大值、最小值等函数。除此之外表达式中还支持变量,也允许开发人员为表达式计算开发新的函数。

6.1   计算表达式

open.calculate.Calculator是表达式的入口类,负责表达式的解析、计算、变量赋值。并且支持自动对表达式预先编译、对表达式解析成子表达式并将结果缓存,提高表达式反复赋值反复计算的性能,

 

普通的表达式例子:

        //表达式

        str = "(2757638.52)+0.00+((((iif(((7.16089040028)+0.00)-((115451.23)+(3.48))>0,((7.16089040028)+0.00)-((115451.23)+(3.48)),0))+((iif((0.00-0.00)>0,(0.00-0.00),0)))+((iif((0.00-0.00)>0,0.00,0))+(iif(((0.0)-(0.0))>0,(0.0),0))+(iif((0.00-0.00)>0,0.00,0))+(iif((0.00-0.00)>0,0.00,0)))+(iif(((0.0)-(55712.41))>0,((0.0)-(55712.41)),0))+((iif((0.00-0.00)>0,0.00,0))+(iif((0.00-0.00)>0,0.00,0))+((iif((0.00-0.00)>0,0.00,0)))))))";

        long t = System.currentTimeMillis();

        Object a = null;

        try

        {

            Calculator e = new Calculator(str, true);

            for(int i = 0; i < 30000; i++)

             //计算

                a = e.eval();

 

        }

        catch(CalculateException e1)

        {

            e1.printStackTrace();

        }

 

带变量表达式例子:

    //带变量的表达式

        String str = "x*10+y-z+10";

        long t = System.currentTimeMillis();

        Object a = null;

        try

        {

            Calculator e = new Calculator(str);

            for(int i = 0; i < 0x186a0; i++)

            {

             //给变量赋值

                e.putParam("x", new BigDecimal(i));

                e.putParam("y", "43.03");

                e.putParam("z", "34.21");

                //计算

                a = e.eval();

            }

            System.out.println(a);

 

        }

        catch(CalculateException e1)

        {

            e1.printStackTrace();

        }

 

带函数的表达式例子:

        //表达式

        str = "x+((((iif(((7.16089040028)+0.00)-((115451.23)+(3.48))>0,((7.16089040028)+0.00)-((115451.23)+(3.48)),0))+((iif((0.00-0.00)>0,(0.00-0.00),0)))+((iif((0.00-0.00)>0,0.00,0))+(iif(((0.0)-(0.0))>0,(0.0),0))+(iif((0.00-0.00)>0,0.00,0))+(iif((0.00-0.00)>0,0.00,0)))+(iif(((0.0)-(55712.41))>0,((0.0)-(55712.41)),0))+((iif((0.00-0.00)>0,0.00,0))+(iif((0.00-0.00)>0,0.00,0))+((iif((0.00-0.00)>0,0.00,0)))))))";

        t = System.currentTimeMillis();

        try

        {

            Calculator e = new Calculator(str);

            for(int i = 0; i < 10000; i++)

            {

             //给变量赋值

                e.putParam("x", new BigDecimal(i));

              //计算

                a = e.eval();

            }

 

        }

        catch(CalculateException e1)

        {

            e1.printStackTrace();

        }

 

6.2   自定义函数接口

open.calculate.api.IFunction接口是开发人员开发自己自定义函数的接口,一般情况函数都有函数名称、函数参数个数、函数的运算,因此接口定义如下:

public interface IFunction {

 

    /**

     * 开发人员开发自己自定义函数的接口

     * @return String

     */

    public String getName();

   

    /**

     * 参数个数

     * @return int

     */

    public int getParamCount();

   

    /**

     * 函数的计算方法

     * @param params

     * @return BigDecimal

     * @throws CalculateException

     */

    public BigDecimal eval(List<BigDecimal> params)throws CalculateException;

   

}

 

条件表达式实现的例子:

 

/**

 * 条件表达式

 * @author pengminghua

 *

 */

public class Iif implements IFunction{

 

    private static final BigDecimal zero = new BigDecimal(0);

   

    /**

     * @see IFunction#eval(List)

     */

    public BigDecimal eval(List<BigDecimal> params) throws CalculateException {

       try{

           BigDecimal cond = convert(params.get(0));

           BigDecimal value = null;

           if(cond.compareTo(zero)!=0){

              value = convert(params.get(1));

           }else{

              value = convert(params.get(2));

           }

           return value;

       }catch(Exception e){

           throw new CalculateException(e);

       }

    }

   

    /**

     * 数据转换

     * @param param

     * @return

     */

    private BigDecimal convert(Object param){

       BigDecimal value = null;

       if(param instanceof BigDecimal){

           value = (BigDecimal)param;

       }else{

           value = new BigDecimal((String)param);

       }

       return value;

    }

   

    /**

     * @see IFunction#getParamCount()

     */

    public int getParamCount() {

       return 3;

    }

   

    /**

     * @see IFunction#getName()

     */

    public String getName() {

       return "iif";

    }

}

 

6.3 部署自定义函数

自定义函数需要部署到表达式解析器中,方法如下:

1ExpressionParse.setPackageNames(“[函数的类路径1], [函数的类路径2]… …”)

2ExpressionParse.init();

例子:

ExpressionParse.setPackageNames("open.calculate.function,com.user.functuon")

ExpressionParse.init();

 

7          数据模型

数据模型的类路径在open.model下,数据模型的主要功能是保存数据库记录的。虽然现在很多开发使用Pojo对象,面向对象的领域模型出发点是好的,但实际上应用的并不是那么好。贫血模型的只是简单的值对象意义不是很大,充血模型又需要开发者有足够的抽象和设计能力,另外国内项目需求变化太大,不好的领域模型很难适应变化。OpenHandx的数据模型是面向数据的,容易理解,并支持数据的查询、分组、排序等功能。

数据模型分为三种:单条数据存储对象、多条数据存储对象(非持久的、持久的)、复合的数据存储对象

7.1   单条数据存储对

单条数据存储对象open.model.RecordVO能动态保存多字段,并且每个字段值能使用不同的方法转化成需要数据类型

7.1.1 例子

        RecordVO rvo = new RecordVO();

        rvo.add("test1", "a1");

        rvo.add("test2", "a2");

        System.out.println(rvo.toString());

        rvo.add("test1", "a3");

        System.out.println(rvo.toString());

        rvo.remove(0);

        System.out.println(rvo.toString());

       rvo.add("test1", "a1");

        System.out.println(rvo.toString());

        rvo.remove("test2");

        System.out.println(rvo.toString());

        rvo.clear();

        System.out.println(rvo.toString());

        rvo.add("test1", "a1");

        rvo.add(0, "test2", "a2");

        System.out.println(rvo.toString());

        System.out.println("type test...");

        rvo.add("test3", Integer.valueOf(1));

        rvo.add("test4", Long.valueOf(1L));

        rvo.add("test5", Float.valueOf(1.1F));

        rvo.add("test6", Double.valueOf(1.1000000000000001D));

        rvo.add("test7", Boolean.valueOf(true));

        rvo.add("test8", new BigDecimal("1.1"));

        rvo.add("test9", new BigInteger("1"));

        System.out.println(rvo.toString());

        rvo.clear();

        rvo.add("test1", Integer.valueOf(1), open.model.Field.DataType.BIGINTEGER);

        rvo.add("test2", Integer.valueOf(1), open.model.Field.DataType.BIGDECIMAL);

        rvo.add("test3", "ABC", open.model.Field.DataType.STRING);

        rvo.add("test4", "100", open.model.Field.DataType.BIGINTEGER);

        rvo.add("test5", "11.7", open.model.Field.DataType.BIGDECIMAL);

        rvo.add("test6", Integer.valueOf(1), open.model.Field.DataType.BOOLEAN);

        rvo.add("test7", Integer.valueOf(0), open.model.Field.DataType.BOOLEAN);

        System.out.println(rvo.toString());

运行结果:

test1:a1:STRING,test2:a2:STRING,

test1:a3:STRING,test2:a2:STRING,

test2:a2:STRING,

test2:a2:STRING,test1:a1:STRING,

test1:a1:STRING,

 

test2:a2:STRING,test1:a1:STRING,

type test...

test2:a2:STRING,test1:a1:STRING,test3:1:INTEGER,test4:1:LONG,test5:1.1:FLOAT,test6:1.1:DOUBLE,test7:true:BOOLEAN,test8:1.1:BIGDECIMAL,test9:1:BIGINTEGER,

test1:1:BIGINTEGER,test2:1:BIGDECIMAL,test3:ABC:STRING,test4:100:BIGINTEGER,test5:11.7:BIGDECIMAL,test6:true:BOOLEAN,test7:false:BOOLEAN,

 

7.2   多条数据存储对象(非持久的)

多条数据存储对象open.model.RecordSetVO,实现了IRecordSetVO接口,能动态保存多字段和多条数据,并且还有对RecordSetVO内的数据进行按任意字段排序、分组、统计、分组求和统计、求唯一、查询、索引查询、从数据库加载的功能。这些功能对开发人员来说都是很常用的。她与PersistenceRecordSetVO相比优势在于性能。

7.2.1 基本例子

public static void main(String arg[])

        throws ModelException, IllegalAccessException, InvocationTargetException, NoSuchMethodException

    {

    List<Field> list = new ArrayList<Field>();

    list.add(new Field("code",Field.DataType.STRING));

    list.add(new Field("name",Field.DataType.STRING));

     list.add(new Field("amt",Field.DataType.BIGDECIMAL));

    RecordSetVO rsvo = new RecordSetVO(list);

   

    setRecordVO(rsvo);

    out(rsvo);

    rsvo.remove(0);

    out(rsvo);

   

    rsvo.clear();

    setPojo(rsvo);

    out(rsvo);

    }

        

    private static void setPojo(RecordSetVO rsvo) throws ModelException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{

    TestModel pojo = new TestModel();

    pojo.setCode("Pojo code1");

    pojo.setName("Pojo name1");

    pojo.setAmt(1000);

    rsvo.add(pojo);

    pojo = new TestModel();

    pojo.setCode("Pojo code2");

    pojo.setName("Pojo name2");

    pojo.setAmt(2000);

    rsvo.add(pojo);

 

    }

   

    private static void setRecordVO(RecordSetVO rsvo) throws ModelException{

        RecordVO rvo = new RecordVO();

        rvo.add("code", "RecordVO code1");

        rvo.add("name", "RecordVO name1");

        rvo.add("amt", 100.00);

        rsvo.add(rvo);

        rvo = new RecordVO();

        rvo.add("code", "RecordVO code2");

        rvo.add("name", "RecordVO name2");

        rvo.add("amt", 200.00);

    rsvo.add(rvo);

        rvo = new RecordVO();

        rvo.add("code", "RecordVO code3");

        rvo.add("name", "RecordVO name3");

        rvo.add("amt", 300.00);

    rsvo.add(rvo);

    }

       

    private static void out(RecordSetVO rsvo){

    for(int i=0;i<rsvo.size();i++){

        RowVO rowvo = rsvo.getRowVO(i);

        System.out.println(rowvo.getValue(0));

        System.out.println(rowvo.getValue(1));

        System.out.println(rowvo.getValue(2));

    }

    }

运行结果:

RecordVO code1

RecordVO name1

100.0

RecordVO code2

RecordVO name2

200.0

RecordVO code3

RecordVO name3

300.0

RecordVO code2

RecordVO name2

200.0

RecordVO code3

RecordVO name3

300.0

Pojo code1

Pojo name1

1000.0

Pojo code2

Pojo name2

2000.0

 

7.2.2 求唯一例子

       RecordSetVO rsvo = new RecordSetVO();

       rsvo.addField(new Field("key1", Field.DataType.STRING));

       rsvo.addField(new Field("key2", Field.DataType.STRING));

       rsvo.addField(new Field("key3", Field.DataType.INTEGER));

       rsvo.add(new Test("A","B",3));

       rsvo.add(new Test("C","C",3));

       rsvo.add(new Test("B","B",3));

       rsvo.add(new Test("B","C",3));

       rsvo.add(new Test("A","A",3));

       rsvo.add(new Test("A","B",3));

       System.out.println(rsvo);

       Distinct distinct = new Distinct(rsvo);

       System.out.println(distinct.distinct("key1","key2"));

 

       for(int i=0;i<10000;i++){

           char a = (char)((int)(Math.random()*26)+65);

           char b = (char)((int)(Math.random()*26)+65);

           char c = (char)((int)(Math.random()*26)+65);

           rsvo.add(new Test(String.valueOf(a),String.valueOf(b),(int)c));

       }

       System.out.println(rsvo);

       long time = System.currentTimeMillis();

       String s = distinct.distinct("key1","key2").toString();

       time = System.currentTimeMillis()-time;

       System.out.println(s);

       System.out.println("time:" + time);

运行结果:

[key1:STRING, key2:STRING]

{key1=A,key2=B}5c963bbb-d635-446f-b7fa-26671ee2a14b

{key1=C,key2=C}a1b5dd41-ee31-434e-a80a-6166fb90ea5c

{key1=B,key2=B}f88077a2-f966-4d3b-abdc-f79471956191

{key1=B,key2=C}dda04235-c8ff-4059-84c8-c3cad3968459

{key1=A,key2=A}949c11ba-4cac-47e6-aa56-d0a4cccb380c

{key1=W,key2=V}d0276485-dddb-4a78-8510-4eea9aa66eae

{key1=F,key2=P}7d1f21ad-3af0-4d55-ac1d-719caaa91642

{key1=J,key2=Q}5095348b-9339-4106-ab50-42f882d3f5c6

{key1=S,key2=Y}f880a531-36e4-43fe-85b1-dcff5aa3060d

{key1=U,key2=E}af52821f-e798-488c-8831-7c977878567c

{key1=R,key2=O}c5ad5d64-762a-4808-bb94-c19c55b6e992

{key1=R,key2=V}80440ee2-14fc-4b5d-8f3c-9cb197851c7b

……

7.2.3 分组例子

       RecordSetVO rsvo = new RecordSetVO();

       rsvo.addField(new Field("key1", Field.DataType.STRING));

       rsvo.addField(new Field("key2", Field.DataType.STRING));

       rsvo.addField(new Field("key3", Field.DataType.INTEGER));

       rsvo.add(new Test("A","B",3));

       rsvo.add(new Test("C","C",3));

       rsvo.add(new Test("B","B",3));

       rsvo.add(new Test("B","C",3));

       rsvo.add(new Test("A","A",3));

       rsvo.add(new Test("A","B",3));

       System.out.println(rsvo);

       Group group = new Group(rsvo,"key1","key2");

       System.out.println(group.group());

 

       for(int i=0;i<10000;i++){

           char a = (char)((int)(Math.random()*26)+65);

           char b = (char)((int)(Math.random()*26)+65);

           char c = (char)((int)(Math.random()*26)+65);

           rsvo.add(new Test(String.valueOf(a),String.valueOf(b),(int)c));

       }

       System.out.println(rsvo);

       long time = System.currentTimeMillis();

       String s = group.group().toString();

       time = System.currentTimeMillis()-time;

       System.out.println(s);

       System.out.println("time:" + time);

运行结果:

c84674a6-dc44-4eca-bf48-92ced334902f

[key1:STRING, key2:STRING, key3:INTEGER]

{key1=A,key2=B,key3=3}d979039d-779d-40fb-aa88-88a611bcd266

{key1=C,key2=C,key3=3}9ac0530d-12a0-4821-a559-468c8745ce58

{key1=B,key2=B,key3=3}a49c6c8a-9e70-4c49-856d-47b5177456ee

{key1=B,key2=C,key3=3}455ab163-d1ad-4422-b5b4-284dd7091a17

{key1=A,key2=A,key3=3}de7ac8fb-4509-443d-9ea2-bad0c4099064

{key1=A,key2=B,key3=3}e7f32941-33b1-4146-ab7b-fc2caf1ecce7

72bdfe4c-7950-420c-924e-7072980ccde1

[key1:STRING, key2:STRING, key3:INTEGER]

{key1=A,key2=B,key3=3}982dee7c-8499-49f8-8230-5bb08102d9c5

{key1=A,key2=B,key3=3}7a3401a7-0517-4517-bd94-e6b6cefd71e8

{key1=C,key2=C,key3=3}b6a9c884-e54b-44e0-ae30-60d84d3f22eb

{key1=B,key2=B,key3=3}86f7e857-204e-488c-8a33-8152f124caee

{key1=B,key2=C,key3=3}5f275eca-65ca-4ac2-ac81-5b079034ff4b

{key1=A,key2=A,key3=3}9a8691f7-7e0c-4fd3-b492-c7471bdda4d7

c84674a6-dc44-4eca-bf48-92ced334902f

[key1:STRING, key2:STRING, key3:INTEGER]

{key1=A,key2=B,key3=3}d979039d-779d-40fb-aa88-88a611bcd266

{key1=C,key2=C,key3=3}9ac0530d-12a0-4821-a559-468c8745ce58

{key1=B,key2=B,key3=3}a49c6c8a-9e70-4c49-856d-47b5177456ee

{key1=B,key2=C,key3=3}455ab163-d1ad-4422-b5b4-284dd7091a17

{key1=A,key2=A,key3=3}de7ac8fb-4509-443d-9ea2-bad0c4099064

{key1=A,key2=B,key3=3}e7f32941-33b1-4146-ab7b-fc2caf1ecce7

{key1=T,key2=O,key3=84}597baf6f-ab99-41ea-ac0b-e29e20db566e

{key1=O,key2=E,key3=73}5a057672-ea55-4175-8141-54cb8d6cbf22

……

7.2.4 重数据库加载例子

           System.out.println(Reader.readPersistence(cnn, "select * from rp_city",1));

          

           System.out.println(Reader.readPersistence(cnn, "select * from rp_city",5,5));

运行结果:

[id:INTEGER, area:STRING, province:STRING, city:STRING]

{id=2,area=华北,province=河北,city=石家庄}ea7f9b99-171f-4d5b-9d25-d3d4e3736814

{id=3,area=华北,province=河北,city=保定}d72499ac-71f1-4e8c-a7e9-55386cdf7509

{id=4,area=东北,province=吉林,city=长春}dbbdca62-90e8-439b-be55-a1527aacd0f5

{id=5,area=东北,province=吉林,city=吉林}137308b9-c242-458e-8a6d-5d37aa3a03e8

{id=6,area=东北,province=辽宁,city=沈阳}7737b89a-bb08-4485-8051-b0e900a4c831

{id=7,area=东北,province=辽宁,city=大连}890d3f2f-2943-479e-9668-ae598c63f67c

{id=8,area=东北,province=黑龙江,city=哈尔滨}b0b602d0-0c40-4845-a246-e4ff5a888977

{id=9,area=东北,province=黑龙江,city=大庆}f17678fb-3955-41d0-88f0-70ff8baa13be

{id=10,area=华中,province=江西,city=南昌}c814f431-b567-4603-b018-646e007fd6ef

{id=11,area=华中,province=江西,city=九江}dab0e080-c56d-4f6e-b5b4-d60075ebf270

{id=12,area=华中,province=江西,city=景德镇}cc0ea869-924e-4a3b-a5a1-8a36c03db371

{id=13,area=华中,province=湖南,city=长沙}c973bc66-f325-4acd-b51f-34c3247a4e8b

{id=14,area=华南,province=广东,city=广州}b042f3ec-adfa-4823-815c-bcb1204c4be4

{id=15,area=华南,province=广东,city=深圳}6c62ed23-ebde-4f5c-8186-1496eb177e8f

{id=16,area=华南,province=广东,city=佛山}687423df-9f35-475a-a2ac-be921e35cd65

{id=17,area=华南,province=广东,city=东莞}6c30a224-9686-4792-b42a-7570bde21731

{id=18,area=华南,province=广东,city=中山}243bfbc8-62dc-42a5-aa11-7500c5800304

[id:INTEGER, area:STRING, province:STRING, city:STRING]

{id=6,area=东北,province=辽宁,city=沈阳}352ce3b9-6472-4fa4-9ad1-e7b614ed80fa

{id=7,area=东北,province=辽宁,city=大连}7c4bd8b7-fcfe-49e2-b9dc-c7e0940c9e3c

{id=8,area=东北,province=黑龙江,city=哈尔滨}f6a4e89d-f59a-4572-849e-7b7ce341639b

{id=9,area=东北,province=黑龙江,city=大庆}f8da42e8-9ebd-4f42-96a6-eb4ef882f9d0

{id=10,area=华中,province=江西,city=南昌}764751aa-a130-440a-b206-7f43d60dc389

7.2.5 排序例子

       RecordSetVO rsvo = new RecordSetVO();

       rsvo.addField(new Field("key1", Field.DataType.STRING));

       rsvo.addField(new Field("key2", Field.DataType.STRING));

       rsvo.addField(new Field("key3", Field.DataType.STRING));

       rsvo.add(new Test("C","C","C"));

       rsvo.add(new Test("B","B","C"));

       rsvo.add(new Test("B","C","C"));

       rsvo.add(new Test("A","A","C"));

       rsvo.add(new Test("A","B","C"));

       rsvo.add(new Test("A","B","C"));

       System.out.println(rsvo);

       QuickSort sort = new QuickSort(rsvo,"key1","key2","key3");

       sort.sort(false);

       System.out.println(rsvo);

       sort.sort(true);

       System.out.println(rsvo);

      

       for(int i=0;i<15;i++){

           char a = (char)((int)(Math.random()*26)+65);

           char b = (char)((int)(Math.random()*26)+65);

           char c = (char)((int)(Math.random()*26)+65);

           rsvo.add(new Test(String.valueOf(a),String.valueOf(b),String.valueOf(c)));

       }

       long time = System.currentTimeMillis();

       for(int i=0;i<10000;i++){

           if(i%2==1){

              sort.sort(false);

           }else{

              sort.sort(true);

           }

       }

       time = System.currentTimeMillis()-time;

       System.out.println("time:" + time);

运行结果:

8e352714-f630-4103-a918-1c5f9d522ab6

[key1:STRING, key2:STRING, key3:STRING]

{key1=C,key2=C,key3=C}84412b8b-8f66-4257-a588-798f40e4cb16

{key1=B,key2=B,key3=C}f881f2c4-8cb9-4e59-b026-ece81465f88e

{key1=B,key2=C,key3=C}d0cb1a1f-528d-42ee-ac2e-bddad01dbbd1

{key1=A,key2=A,key3=C}5a0ed1dc-0c41-40ec-876a-bc475027db8f

{key1=A,key2=B,key3=C}41942f5c-14e8-46ad-90d7-70fd363dc76c

{key1=A,key2=B,key3=C}3a82006d-83aa-4435-98f8-db8e0bacb876

8e352714-f630-4103-a918-1c5f9d522ab6

[key1:STRING, key2:STRING, key3:STRING]

{key1=A,key2=A,key3=C}5a0ed1dc-0c41-40ec-876a-bc475027db8f

{key1=A,key2=B,key3=C}3a82006d-83aa-4435-98f8-db8e0bacb876

{key1=A,key2=B,key3=C}41942f5c-14e8-46ad-90d7-70fd363dc76c

{key1=B,key2=B,key3=C}f881f2c4-8cb9-4e59-b026-ece81465f88e

{key1=B,key2=C,key3=C}d0cb1a1f-528d-42ee-ac2e-bddad01dbbd1

{key1=C,key2=C,key3=C}84412b8b-8f66-4257-a588-798f40e4cb16

8e352714-f630-4103-a918-1c5f9d522ab6

[key1:STRING, key2:STRING, key3:STRING]

{key1=C,key2=C,key3=C}84412b8b-8f66-4257-a588-798f40e4cb16

{key1=B,key2=C,key3=C}d0cb1a1f-528d-42ee-ac2e-bddad01dbbd1

{key1=B,key2=B,key3=C}f881f2c4-8cb9-4e59-b026-ece81465f88e

{key1=A,key2=B,key3=C}3a82006d-83aa-4435-98f8-db8e0bacb876

{key1=A,key2=B,key3=C}41942f5c-14e8-46ad-90d7-70fd363dc76c

{key1=A,key2=A,key3=C}5a0ed1dc-0c41-40ec-876a-bc475027db8f

7.2.6 统计例子

       RecordSetVO rsvo = new RecordSetVO();

       rsvo.addField(new Field("key1", Field.DataType.STRING));

       rsvo.addField(new Field("key2", Field.DataType.STRING));

       rsvo.addField(new Field("key3", Field.DataType.INTEGER));

       rsvo.addField(new Field("key4", Field.DataType.BIGDECIMAL));

       rsvo.addField(new Field("key5", Field.DataType.BIGINTEGER));

       rsvo.addField(new Field("key6", Field.DataType.DOUBLE));

       rsvo.add(new Test("A","B",1,BigDecimal.valueOf(1),BigInteger.valueOf(1),0.1));

       rsvo.add(new Test("C","C",2,BigDecimal.valueOf(2),BigInteger.valueOf(2),0.2));

       rsvo.add(new Test("B","B",3,BigDecimal.valueOf(3),BigInteger.valueOf(3),0.3));

       rsvo.add(new Test("B","C",4,BigDecimal.valueOf(4),BigInteger.valueOf(4),0.4));

       rsvo.add(new Test("A","A",5,BigDecimal.valueOf(5),BigInteger.valueOf(5),0.5));

       rsvo.add(new Test("A","B",6,BigDecimal.valueOf(6),BigInteger.valueOf(6),0.6));

       System.out.println(rsvo);

       Stat sum = new Stat(rsvo,"key1");

       RecordSetVO rvo = sum.sum("key3","key4","key5","key6");

       System.out.println(rvo);

 

       for(int i=0;i<100000;i++){

           char a = (char)((int)(Math.random()*26)+65);

           char b = (char)((int)(Math.random()*26)+65);

           char c = (char)((int)(Math.random()*26)+65);

           double d = Math.random()*100;

           rsvo.add(new Test(String.valueOf(a),String.valueOf(b),(int)d,BigDecimal.valueOf(d),BigInteger.valueOf((int)d),d));

       }

      

       Group group = new Group(rsvo,"key1");

       group.group();

      

       System.out.println(rsvo);

       long time = System.currentTimeMillis();

       sum = new Stat(rsvo,"key1");

       RecordSetVO rvo2 = sum.sum("key3","key4","key5","key6");

       time = System.currentTimeMillis()-time;

       System.out.println(rvo2);

       System.out.println("time:" + time);

      

       sum = new Stat(rsvo);

       RecordSetVO rvo3 = sum.sum("key3","key4","key5","key6");

       System.out.println(rvo3);

       sum = new Stat(rvo2);

       RecordSetVO rvo4 = sum.sum("key3","key4","key5","key6");

       System.out.println(rvo4);

运行结果:

ff22d6bb-1216-4e16-9e4e-82a7cb036a86

[key1:STRING, key2:STRING, key3:INTEGER, key4:BIGDECIMAL, key5:BIGINTEGER, key6:DOUBLE]

{key1=A,key2=B,key3=1,key4=1,key5=1,key6=0.1}0248d861-7747-40af-912b-fcb76736fe5a

{key1=C,key2=C,key3=2,key4=2,key5=2,key6=0.2}04efe645-e8bf-4af6-a8ac-5372f12b6d2a

{key1=B,key2=B,key3=3,key4=3,key5=3,key6=0.3}f65ec227-96ca-4f9d-a08a-4b67626e912e

{key1=B,key2=C,key3=4,key4=4,key5=4,key6=0.4}d124ac99-51a9-4353-8cc4-cdb8b2e6e943

{key1=A,key2=A,key3=5,key4=5,key5=5,key6=0.5}e0639ef0-4f2a-4e00-8879-befdd186da01

{key1=A,key2=B,key3=6,key4=6,key5=6,key6=0.6}1fbb2676-5b7e-4217-8d23-ca7e1a821392

9fafc252-c1db-4f37-bed2-396e9671512e

[RecordSetVO_COUNT:LONG, key1:STRING, key3:INTEGER, key4:BIGDECIMAL, key5:BIGINTEGER, key6:DOUBLE]

{RecordSetVO_COUNT=1,key1=A,key3=1,key4=1,key5=1,key6=0.1}123ef824-2926-4c18-a275-4325162e8f15

{RecordSetVO_COUNT=1,key1=C,key3=2,key4=2,key5=2,key6=0.2}bf131f20-2e22-4aba-8405-9e9bad62360a

{RecordSetVO_COUNT=2,key1=B,key3=7,key4=7,key5=7,key6=0.7}dfa128c7-5363-4c2f-9fd2-c23cda09340d

{RecordSetVO_COUNT=2,key1=A,key3=11,key4=11,key5=11,key6=1.1}1ed27aab-cda4-456b-a659-7839615bd3c4

ff22d6bb-1216-4e16-9e4e-82a7cb036a86

[key1:STRING, key2:STRING, key3:INTEGER, key4:BIGDECIMAL, key5:BIGINTEGER, key6:DOUBLE]

{key1=A,key2=B,key3=1,key4=1,key5=1,key6=0.1}0248d861-7747-40af-912b-fcb76736fe5a

{key1=C,key2=C,key3=2,key4=2,key5=2,key6=0.2}04efe645-e8bf-4af6-a8ac-5372f12b6d2a

{key1=B,key2=B,key3=3,key4=3,key5=3,key6=0.3}f65ec227-96ca-4f9d-a08a-4b67626e912e

{key1=B,key2=C,key3=4,key4=4,key5=4,key6=0.4}d124ac99-51a9-4353-8cc4-cdb8b2e6e943

{key1=A,key2=A,key3=5,key4=5,key5=5,key6=0.5}e0639ef0-4f2a-4e00-8879-befdd186da01

{key1=A,key2=B,key3=6,key4=6,key5=6,key6=0.6}1fbb2676-5b7e-4217-8d23-ca7e1a821392

{key1=U,key2=W,key3=43,key4=43.37386713329803,key5=43,key6=43.37386713329803}0e6d140a-d811-41b0-9c4f-feb8831c4591

{key1=S,key2=N,key3=57,key4=57.42766495870948,key5=57,key6=57.42766495870948}c7742d17-53e9-4693-add4-9ada670be7dc

{key1=B,key2=D,key3=25,key4=25.74833734215616,key5=25,key6=25.74833734215616}498477ee-0957-4afa-9581-090bb17c3ad6

{key1=H,key2=C,key3=76,key4=76.58383640524428,key5=76,key6=76.58383640524428}8eb5bc8a-3704-4af3-85fa-1b827c9ae09f

{key1=Y,key2=H,key3=36,key4=36.94254509985046,key5=36,key6=36.94254509985046}3f1d68d8-d65a-46b8-aa6b-d35ef577af64

{key1=Q,key2=H,key3=95,key4=95.39296675151823,key5=95,key6=95.39296675151823}edd8ea7c-69f7-4a81-a3a7-da5e0aa502d6

{key1=B,key2=M,key3=33,key4=33.59597337869399,key5=33,key6=33.59597337869399}81969c98-9fd0-4f39-97fc-812d91501456

{key1=H,key2=D,key3=96,key4=96.88514732110404,key5=96,key6=96.88514732110404}7ea8a997-a411-48b4-ba84-a8dffa769ad3

{key1=E,key2=P,key3=48,key4=48.52717112395334,key5=48,key6=48.52717112395334}fbc02421-2dfe-4e6e-bf45-c0256ba38cb8

{key1=I,key2=K,key3=79,key4=79.73567070062981,key5=79,key6=79.73567070062981}c41072e0-80cb-4590-9c90-a272f61b6944

f60b65ef-40b1-4494-afd3-411cccd613a1

[RecordSetVO_COUNT:LONG, key1:STRING, key3:INTEGER, key4:BIGDECIMAL, key5:BIGINTEGER, key6:DOUBLE]

{RecordSetVO_COUNT=1,key1=A,key3=1,key4=1,key5=1,key6=0.1}93b71cfa-bbd8-4ac4-a84e-fbe84581bfb2

{RecordSetVO_COUNT=1,key1=C,key3=2,key4=2,key5=2,key6=0.2}32c26bd8-eb38-423f-b106-44a742b00f30

{RecordSetVO_COUNT=2,key1=B,key3=7,key4=7,key5=7,key6=0.7}6ab9359d-8534-4898-a80b-30f17ec30184

{RecordSetVO_COUNT=2,key1=A,key3=11,key4=11,key5=11,key6=1.1}86e60477-b9bc-48b6-95f7-fd82a2d34823

{RecordSetVO_COUNT=1,key1=U,key3=43,key4=43.37386713329803,key5=43,key6=43.37386713329803}e42634e6-2682-495d-b93a-b097884276ea

{RecordSetVO_COUNT=1,key1=S,key3=57,key4=57.42766495870948,key5=57,key6=57.42766495870948}f2289a4b-307b-434f-8ba3-329856215f13

{RecordSetVO_COUNT=1,key1=B,key3=25,key4=25.74833734215616,key5=25,key6=25.74833734215616}0f17b67f-4815-4547-a970-13a2b9c0e672

{RecordSetVO_COUNT=1,key1=H,key3=76,key4=76.58383640524428,key5=76,key6=76.58383640524428}1dd91f25-79c9-4041-9776-fc1c7449c40e

{RecordSetVO_COUNT=1,key1=Y,key3=36,key4=36.94254509985046,key5=36,key6=36.94254509985046}0b9f1a1c-95a9-4f04-988d-13df7dceac2d

{RecordSetVO_COUNT=1,key1=Q,key3=95,key4=95.39296675151823,key5=95,key6=95.39296675151823}351b9acd-b256-40a5-b4cc-36fb66724424

{RecordSetVO_COUNT=1,key1=B,key3=33,key4=33.59597337869399,key5=33,key6=33.59597337869399}b620e344-00e1-4175-9903-54e7adb9c228

{RecordSetVO_COUNT=1,key1=H,key3=96,key4=96.88514732110404,key5=96,key6=96.88514732110404}5d0927ac-0dc6-43d9-809c-d03900bdafd7

{RecordSetVO_COUNT=1,key1=E,key3=48,key4=48.52717112395334,key5=48,key6=48.52717112395334}2570e1ed-3d6d-4d71-adae-23a566f242d1

{RecordSetVO_COUNT=1,key1=I,key3=79,key4=79.73567070062981,key5=79,key6=79.73567070062981}7cba4514-8df3-4dff-8e71-72e02743518f

……

 

bbb3f75c-cdac-4f86-adcd-dd2c9d7d26f8

[RecordSetVO_COUNT:LONG, key3:INTEGER, key4:BIGDECIMAL, key5:BIGINTEGER, key6:DOUBLE]

{RecordSetVO_COUNT=16,key3=609,key4=615.21318021515782,key5=609,key6=596.3131802151579}bf592e4c-f62d-4d06-a086-249d41074fb6

a0d39b2d-582d-483c-91e3-9cb43c946202

[RecordSetVO_COUNT:LONG, key3:INTEGER, key4:BIGDECIMAL, key5:BIGINTEGER, key6:DOUBLE]

{RecordSetVO_COUNT=14,key3=609,key4=615.21318021515782,key5=609,key6=596.3131802151579}4bd404f3-b0fa-47bf-b50f-add110a5bb49

 

 

7.3   多条数据存储对象(持久的)

多条数据存储对象open.model.PersistenceRecordSetVO,也实现了IRecordSetVO接口除了具有RecordSetVO的绝大部分功能,还能将数据持久到本地服务器硬盘中,这就让PersistenceRecordSetVO能保存海量的数据,数据计算都可以在本地服务器中进行,减少数据库服务器的压力。

 

7.4   复合的数据存储对象

复合的数据存储对象open.model.MulripleVO,能保存一个单条数据存储对象和多个多条数据存储对象,目的是为了保存复杂关系的数据。

 

8    xml解析与生成

8.1   xml解析

xml解析类open.xml.XmlReader能将xml字符或文件读取到Pojo值对象中,开发人员只需要几行代码就能完成工作,而不必了解dom等复杂的xml解析工具类。目前XmlReader能支持java的基本数据类型、List接口、自定义的Pojo及嵌套。XmlReader支持属性方式、描述方式。

        HashMap m = new HashMap();

        //指定List属性中元素的class

        m.put("field", Field.class);

        XmlReader xr = new XmlReader();

        //设置为描述方式

        xr.setOption(false);

        //ABIS_BDBTH.XML文件读取到Fmt,并生成Fmt实例

        Object b = xr.readFile(XmlReaderTest.class.getResource("ABIS_BDBTH.XML").getPath(), Fmt.class, m);

//        Object b = XmlReader.readFile(XmlReaderTest.class.getResource("ATTRIB.XML").getPath(), Fmt.class, m);

        System.out.print(b);//        Object b = XmlReader.readFile(XmlReaderTest.class.getResource("ATTRIB.XML").getPath(), Fmt.class, m);

        System.out.print(b);

……

 

    public Fmt()

    {

        gDecl = null;

        dataBase = null;

        table = null;

        subPriTag = null;

        addFieldDate = null;

        isDates = null;

        subPriKeys = null;

        specialTimeFlags = null;

        allIndexs = null;

        percent = 0.0D;

    }

 

    public void setGDecl(GDecl gDecl)

    {

        this.gDecl = gDecl;

    }

 

    public GDecl getGDecl()

    {

        return gDecl;

    }

……

 

8.2   xml生成

xml生成open.xml.XmlWriter是XmlReader逆过程,能将pojo生成xml字符或文件。目前XmlWriter能支持java的基本数据类型、List接口、自定义的Pojo及嵌套。XmlWriter也支持属性方式、描述方式。她的好处是可以是程序在运行时修改xml,从而达到修改配置的目的。

        HashMap m = new HashMap();

        m.put("field", Field.class);

        XmlReader xr = new XmlReader();

        //设置描述方式

        xr.setOption(false);

        //构造出Fmt的实例

        Object b = xr.readFile(XmlReaderTest.class.getResource("ABIS_BDBTH.XML").getPath(), Fmt.class, m);

 

       XmlWriter xw = new XmlWriter();

       //设置描述方式

       xw.setOption(false);

       //生成xml字符

       System.out.println(xw.writeString(b));

9    Class方法工具类

Class方法工具类open.tools.classmethod.ClassMethod能获取某个Class的get、set方法,并对这些方法取值和赋值,根据方法名称、参数类型、参数名字获取的方法定义。

 

10 OpenHandX所有项目介绍

项目名称

说明

open-Common

公共开发包,整理了日常开发中可能经常用到的方法、工具类

open-Chart

图表工具,核心采用jfreechart,将jfreechart重新包装简化,方便使用

open-Report

多维度报表工具,支持表达式计算、汇总、分组、排序、集成图表,支持Excel输出

open-Real

持久层报表工具,封装jdbc的增、删、改、查。具有自动匹配sql条件,orm影射

open-Batch

分布式批量架构,处理复杂耗时的数据加工或ETL加工

open-Unreal

SSScript-Service)脚本服务Web开发框架,并提供WebServie功能

open-River

基于open-unrealESB产品,提供代理和点对点模式,并提供快速消息传输通道

OpenHandX下载地址:http://code.google.com/p/openhandx2/

 

 

 

posted on 2010-02-02 19:32 彭明华 阅读(2018) 评论(2)  编辑  收藏 所属分类: OpenHandX核心

FeedBack:
# re: OpenHandX-Common 1.0[未登录] 2010-02-03 09:51 Jack
Dear master:

Open source? But i can't find the code...
- -#  回复  更多评论
  
# re: OpenHandX-Common 1.0 2010-02-03 17:27 彭明华
@Jack
现在还没计划加入Open source,打算多发布几个项目再加入。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: