文章目录
- FabricJavaPool
- Why
- How
FabricJavaPool
https://github.com/SamYuan1990/FabricJavaPool
Why
我们来看一下下边两个图,如果我们用web,java,fabric network的结构做一个可交互的页面来展示区块链的数据。(实际上本人更倾向于用java+spring做rest服务器)
理论上讲,我们不希望每次页面请求数据都做一次IO,相对的,我们希望有个链接池,在池子处理好IO。当然逻辑上我们可以把这个pool和用户的session或者cookie绑定(另外的故事了)
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();
}