Coder Social home page Coder Social logo

fabricjavapool's Introduction

FabricJavaPool

A Connection pool manager for Fabric development

based on fabric-sdk-java 1.4.6 , fabric-gateway-java 1.4.3 and JDK8 following design of JDBC Basing common pool and Fabric Java SDK, able with cache support for query with memcache. Will provide you a config and a pool object of channel obj base on User. Mostly used as query chain code for a specific user.

Why

Assuming we have a webUI for client, and the UI need request times to fabric network to fetch data. For performance exception, we don't want to have IO session many times. So a connection pool basing on user's msp, and try to reuse the connection on java server side by session or cookie.

Sample usage:

Gradle

	implementation group: 'com.github.samyuan1990', name:'FabricJavaPool', version: '0.0.3'

For SNAPSHOT version

   repositories {
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
	implementation group: 'com.github.samyuan1990', name:'FabricJavaPool', version: '0.0.3-SNAPSHOT'

Pool config

Fabric network config file in json format and file path as configNetworkPath.

configNetworkPath=./src/test/resources/Networkconfig.json
maxTotal=10
maxIdle=8
minIdle=2
maxWaitMillis=1000
# if you want to use fabric gateway
walletPath=./src/test/resources/crypto-wallet/peerOrganizations/org1.example.com/users
# if you want to use cache
UseCache=true
cacheURL=127.0.0.1:11211
cacheTimeout=300

Get Connection

        //if you want to use fabric gateway
        GenericObjectPool<FabricConnection> pool = FabricConnectionPoolFactory.getPool(TestUtil.userName, TestUtil.myChannel);
        //if you want to use fabric java sdk
        GenericObjectPool<FabricConnection> pool = FabricConnectionPoolFactory.getPool(TestUtil.getUser(), TestUtil.myChannel);

        // By Default it will read pool config from ./resources/FabricJavaPool.properties
        try {
            FabricConnection fabricConnectionImpl = fabricConnectionPool.borrowObject();
            FabricConnection fabricConnectionImpl2 = fabricConnectionPool.borrowObject();
            String rs = fabricConnectionImpl.query(TestUtil.chaincodeID, "query", "a");
            String rs2 = fabricConnectionImpl2.query(TestUtil.chaincodeID, "query", "a");
            }
            fabricConnectionPool.returnObject(fabricConnectionImpl);
            fabricConnectionPool.returnObject(fabricConnectionImpl2);
        } catch (Exception e) {
            e.printStackTrace();
        }

Make ChainCode

        ChaincodeID cci = Util.generateChainCodeID(TestUtil.myCC, TestUtil.myCCVersion);

Query

FabricConnection myConnection = myChannelPool.borrowObject();
ExecuteResult rs = myConnection.query("mycc", "query", "a");
Assert.assertEquals("90", rs.getResult());

Invoke

FabricConnection myConnection = myChannelPool.borrowObject();
ExecuteResult rs = myConnection.invoke("mycc", "query", "a");
Assert.assertEquals("90", rs.getResult());

Query or Invoke exception

                myConnection.invoke(TestUtil.chaincodeID, "error", "a");
            } catch (RunTimeException e) {
                Assert.assertEquals(ChaincodeResponse.Status.FAILURE, e.getStatus());
                Assert.assertEquals(Util.errorHappenDuringQuery, e.getMsg());

Query or Invoke exception due to chaincode results are different on peers

            myConnection.query(TestUtil.chaincodeID, "query", "a");
        } catch (RunTimeException e) {
            Assert.assertEquals(ChaincodeResponse.Status.SUCCESS, e.getStatus());
            Assert.assertEquals(Util.resultOnPeersDiff, e.getMsg());
        }

For develop this project

gradle clean build

Optional:

Copy crypto-config to your local byfn

byfn.sh up

Start memcache

docker run -p 11211:11211 --name memcache memcached

Add ect hosts with

127.0.0.1       peer0.org2.example.com
127.0.0.1       orderer.example.com
127.0.0.1       peer0.org1.example.com

After change.

export ORG_GRADLE_PROJECT_LocalFabric=true
gradle clean build

Version

0.0.1 as basic version 0.0.2 Add query and invoke support, prop file support. 0.0.3 Add support for fabric-java-gateway, cache

To do

if there is no more feature request, will update to 1.0.0 release

fabricjavapool's People

Contributors

samyuan1990 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fabricjavapool's Issues

Description update

A Pool project for Fabric Java SDK
could be changed to be->

**A Connection pool manager for Fabric development**

based on [fabric-sdk-java](https://github.com/hyperledger/fabric-sdk-java) 
following design of JDBC

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.