FabricJavaPool

文章目录

FabricJavaPool

https://github.com/SamYuan1990/FabricJavaPool

Why

我们来看一下下边两个图,如果我们用web,java,fabric network的结构做一个可交互的页面来展示区块链的数据。(实际上本人更倾向于用java+spring做rest服务器)
理论上讲,我们不希望每次页面请求数据都做一次IO,相对的,我们希望有个链接池,在池子处理好IO。当然逻辑上我们可以把这个pool和用户的session或者cookie绑定(另外的故事了)

PersonwebUIJava ServerBlockchain Networkclickgive me some data 1It takes some timefor IO asnetwork/msp etc..give me some data 1give me some data 2It takes some timefor IO asnetwork/msp etc..give me some data 2give me some data ngive me some data nclick anothergive me some Data 2It takes some timefor IO asnetwork/msp etc..give me some Data 2PersonwebUIJava ServerBlockchain NetworkPersonwebUIJava ServerBlockchain Networkclickgive me some data 1It takes some timefor IO asnetwork/msp etcas a pool?give me some data 2give me some data 2give me some datangive me some data nclick anothergive me some Data 2give me some Data 2PersonwebUIJava ServerBlockchain Network

How

介于fabric-java-sdk本身不提供这个功能,我就自己造了个*。
用了common-pool和fabric-java-sdk,详情见test代码。

        ObjectPool<Channel>  myChannelPool= new FabricJavaPool("./src/test/resources/Networkconfig.json",getUser(),"mychannel");
        try {
            Channel myChannel = myChannelPool.borrowObject();
            assertNotEquals("Test borrow item channel not null",myChannel,null);
            assertEquals("Test borrow item channel",myChannel.isInitialized(),true);
            Channel myChannel2 = myChannelPool.borrowObject();
            assertNotEquals("Test borrow item channel2 not null",myChannel2,null);
            assertEquals("Test borrow item channel2",myChannel2.isInitialized(),true);
            assertEquals("Test item should diff",myChannel2.equals(myChannel),false);
            myChannelPool.returnObject(myChannel);
            myChannelPool.returnObject(myChannel2);
            String rs=Query(myChannel,"mycc","query","a");
            assertEquals("90",rs);
            String rs2=Query(myChannel2,"mycc","query","a");
            assertNotEquals("91",rs2);
        } catch (Exception e) {
            e.printStackTrace();
        }
上一篇:MyBatis中in的使用


下一篇:js循环数组方法some和forEach怎么用