github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/docs/source/Setup/JAVAChaincode.rst (about)

     1  Java chaincode
     2  --------------
     3  
     4  Note: This guide generally assumes you have followed the Chaincode
     5  development environment setup tutorial
     6  `here <https://github.com/hyperledger/fabric/blob/master/docs/Setup/Chaincode-setup.md>`__.
     7  
     8  To get started developing Java chaincode
     9  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    10  
    11  1. Ensure you have gradle
    12  
    13  -  Download the binary distribution from
    14     http://gradle.org/gradle-download/
    15  -  Unpack, move to the desired location, and add gradle's bin directory
    16     to your system path
    17  -  Ensure ``gradle -v`` works from the command-line, and shows version
    18     2.12 or greater
    19  -  Optionally, enable the `gradle
    20     daemon <https://docs.gradle.org/current/userguide/gradle_daemon.html>`__
    21     for faster builds
    22  
    23  2. Ensure you have the Java 1.8 **JDK** installed. Also ensure Java's
    24     directory is on your path with ``java -version``
    25  
    26  -  Additionally, you will need to have the |JAVA_HOME|_ variable set to your
    27     **JDK** installation in your system path
    28  
    29  3. From your command line terminal, move to the ``devenv`` subdirectory
    30     of your workspace environment. Log into a Vagrant terminal by
    31     executing the following command:
    32  
    33  .. |JAVA_HOME| replace:: ``JAVA_HOME``
    34  .. _JAVA_HOME: https://docs.oracle.com/cd/E19182-01/821-0917/6nluh6gq9/index.html
    35  
    36     ::
    37  
    38         vagrant ssh
    39  
    40  4. Build and run the peer process.
    41  
    42     ::
    43  
    44         cd $GOPATH/src/github.com/hyperledger/fabric
    45         make peer
    46         peer node start
    47  
    48  5. The following steps is for deploying chaincode in non-dev mode.
    49  
    50     -  Deploy the chaincode,
    51  
    52  ::
    53  
    54          peer chaincode deploy -l java -p /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample -c '{"Args": ["init", "a","100", "b", "200"]}'
    55  
    56  ``6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9``
    57  
    58  ::
    59  
    60          * This command will give the 'name' for this chaincode, and use this value in all the further commands with the -n (name) parameter
    61  
    62  
    63          * PS. This may take a few minutes depending on the environment as it deploys the chaincode in the container,
    64  
    65  -  Invoke a transfer transaction,
    66  
    67  ::
    68  
    69          peer chaincode invoke -l java \
    70          -n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \
    71          -c '{"Args": ["transfer", "a", "b", "10"]}'
    72  
    73  ``c7dde1d7-fae5-4b68-9ab1-928d61d1e346``
    74  
    75  -  Query the values of a and b after the transfer
    76  
    77  ::
    78  
    79          peer chaincode query -l java \
    80          -n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \
    81          -c '{ "Args": ["query", "a"]}'
    82          {"Name":"a","Amount":"80"}
    83  
    84  
    85          peer chaincode query -l java \
    86          -n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \
    87          -c '{ "Args": ["query", "b"]}'
    88          {"Name":"b","Amount":"220"}
    89  
    90  Java chaincode deployment in DEV Mode
    91  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    92  
    93  1. Follow the step 1 to 3 as above,
    94  2. Build and run the peer process
    95  
    96     ::
    97  
    98         cd $GOPATH/src/github.com/hyperledger/fabric
    99         make peer
   100         peer node start --peer-chaincodedev
   101  
   102  3. Open the second Vagrant terminal and build the Java shim layer and
   103     publish it to Local Maven Repo
   104  
   105     ::
   106  
   107         cd $GOPATH/src/github.com/hyperledger/fabric/core/chaincode/shim/java
   108         gradle -b build.gradle clean
   109         gradle -b build.gradle build
   110  
   111  4. Change to examples folder to build and run,
   112  
   113     ::
   114  
   115         cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample
   116         gradle -b build.gradle build
   117  
   118  5. Run the SimpleSample chaincode using the
   119     ``gradle -b build.gradle run``
   120  
   121  6. Open the third Vagrant terminal to run init and invoke on the
   122     chaincode
   123  
   124     peer chaincode deploy -l java -n SimpleSample -c '{"Args": ["init",
   125     "a","100", "b", "200"]}'
   126  
   127     ::
   128  
   129         2016/06/28 19:10:15 Load docker HostConfig: %+v &{[] [] []  [] false map[] [] false [] [] [] [] host    { 0} [] { map[]} false []  0 0 0 false 0    0 0 0 []}
   130         19:10:15.461 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
   131         SimpleSample
   132  
   133     peer chaincode invoke -l java -n SimpleSample -c '{"Args":
   134     ["transfer", "a", "b", "10"]}'
   135  
   136  ::
   137  
   138      2016/06/28 19:11:13 Load docker HostConfig: %+v &{[] [] []  [] false map[] [] false [] [] [] [] host    { 0} [] { map[]} false []  0 0 0 false 0    0 0 0 []}
   139      19:11:13.553 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
   140      978ff89e-e4ef-43da-a9f8-625f2f6f04e5
   141  
   142  ::
   143  
   144      peer chaincode query -l java -n SimpleSample -c '{ "Args": ["query", "a"]}'
   145  
   146  ::
   147  
   148      2016/06/28 19:12:19 Load docker HostConfig: %+v &{[] [] []  [] false map[] [] false [] [] [] [] host    { 0} [] { map[]} false []  0 0 0 false 0    0 0 0 []}
   149      19:12:19.289 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
   150      {"Name":"a","Amount":"90"}
   151  
   152  ::
   153  
   154      peer chaincode query -l java -n SimpleSample -c '{"Args": ["query", "b"]}'
   155  
   156  ::
   157  
   158      2016/06/28 19:12:25 Load docker HostConfig: %+v &{[] [] []  [] false map[] [] false [] [] [] [] host    { 0} [] { map[]} false []  0 0 0 false 0    0 0 0 []}
   159      19:12:25.667 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
   160      {"Name":"b","Amount":"210"}
   161  
   162  Developing new JAVA chaincode
   163  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   164  
   165  1. Create a new Java project structure.
   166  2. Use existing ``build.grade`` from any example JAVA Chaincode project
   167     like ``examples/chaincode/java/SimpleSample``.
   168  3. Make your main class extend ``ChaincodeBase`` class and implement the
   169     following methods from base class.
   170  
   171     - ``public Response init(ChaincodeStub stub)``
   172     - ``public Response invoke(ChaincodeStub stub);``
   173     - ``public String getChaincodeID()``
   174  
   175  4. Modify the ``mainClassName`` in ``build.gradle`` to point to your new
   176     class.
   177  5. Build this project using ``gradle -b build.gradle build``
   178  6. Run this chaincode after starting a peer in dev-mode as above using
   179     ``gradle -b build.gradle run``