Running the IBM MQ Bridge to blockchain client sample

We can use the JMS client sample that is provided with the IBM MQ Bridge to blockchain, to put a message on the input queue that the blockchain bridge is checking and see the reply that is received. This sample is based on using the IBM MQ Bridge to blockchain integrating with the Hyperledger Composer Trader network example.


Before starting

See /trade_network for more information

Your IBM MQ Bridge to blockchain is running and is connected to your IBM MQ Advanced, or IBM MQ Advanced for z/OS VUE, queue manager and your blockchain network.


Find the JMS sample application (ComposerBCBSamp.java) in the samp directory of the IBM MQ Bridge to blockchain.

For example: <MQ_INSTALL_ROOT>/mqbc/samp/ComposerBCBSamp.java, where <MQ_INSTALL_ROOT> is the:

  • Directory where IBM MQ is installed
  • USS directory where the USS components of IBM MQ are installed

.


Procedure

  1. Edit the client sample Java source file.

    Follow the instructions in the sample to configure it to match the IBM MQ environment and your blockchain network.

    The following code from the sample defines three JSON request messages to send to the bridge:
    1. Firstly, to remove an existing 'commodity'
    2. Secondly, to create a new 'commodity', ‘owner' and associated values,
    3. Finally, displays the new information about the 'commodity', following the previous two request messages

    private static JSONObject[] createMessageBodies() {
        JSONObject[] msgs = new JSONObject[3]; // This method creates 3 messages
        JSONObject m, m2;
        String commodityName = "BC";
    
        // Clean out the commodity in case it's already there. If
        // it's not there, there will be an error returned from Composer.
        m = new JSONObject();
        m.put("method", "DELETE");
        m.put("path", "api/Commodity/" + commodityName);
        msgs[0] = m;
    
        // To add the item to the table, the
        // operation looks like this:
        //
        // { "method": "POST",
        //   "path": "api/Commodity",
        //   "body" : {
        //     "$class": "org.example.trading.Commodity",
        //     "tradingSymbol" : "BC",
        //     "description" : "BC",
        //     "mainExchange" : "HERE",
        //     "owner" : "Me",
        //     "quantity" : 100
        //   }
        // }
        // We can see this structure in the API Explorer
        m = new JSONObject();
        m.put("method", "POST");
        m.put("path", "api/Commodity");
        m2 = new JSONObject();
        m2.put("$class", " org.example.trading.Commodity");
        m2.put("tradingSymbol", commodityName);
        m2.put("description", "Blockchain Sample Description");
        m2.put("mainExchange", "My Exchange");
        m2.put("owner", "Me");
        m2.put("quantity", 100);
        m.put("body", m2);
        msgs[1] = m;
    
        // And list all items that have been created
        m = new JSONObject();
        m.put("method", "GET");
        m.put("path", "api/Commodity");
        msgs[2] = m;
    
        return msgs;
      }
        
    
    
  2. Compile the sample. Point to the IBM MQ client classes and JSON4J.jar file that is shipped in the bridge directory.
    javac -cp <MQ_INSTALL_ROOT>/java/lib/*:<MQ_INSTALL_ROOT>/mqbc/prereqs/JSON4J.jar ComposerBCBClient.java
  3. Run the compiled class.
    java -cp <MQ_INSTALL_ROOT>/java/lib/*:<MQ_INSTALL_ROOT>/mqbc/prereqs/JSON4J.jar:. ComposerBCBClient
    Starting Simple MQ Blockchain Bridge Client
    Starting the connection.
    Sent message:
      {"method":"DELETE"," path ":"api\/Commodity\/BC"}
    Response text: 
    {
       "statusCode": 204,
       "statusType": "SUCCESS",
       "message": "OK",
       "data": ""
    }
    SUCCESS
    Sent message:
    {"body":{"$class":"org.example.trading.Commodity","owner":"Me","quantity":100,"description":"Blockchain Sample Description","mainExchange":"My Exchange","tradingSymbol":"BC"},"operation":"POST","url":"Commodity"}
    Response text: 
    {
       "statusCode": 200,
       "statusType": "SUCCESS",
       "message": "OK",
       "data": {
          "$class": "org.example.trading.Commodity",
          "description": "Blockchain Sample Description",
          "mainExchange": "My Exchange",
          "owner": "Me",
          "quantity": 100,
          "tradingSymbol": "BC"
       }
    }
    SUCCESS
    Sent message:
      {"method":"GET","path":"api\/Commodity"}
    Response text: 
    {
       "statusCode": 200,
       "statusType": "SUCCESS",
       "message": "OK",
       "data": [
          {
             "$class": "org.example.trading.Commodity",
             "description": "Blockchain Sample Description",
             "mainExchange": "My Exchange",
             "owner": "resource:org.example.trading.Trader#Me",
             "quantity": 100,
             "tradingSymbol": "BC"
          }
       ]
    }
    SUCCESS
    

    The message field contains either "OK" for a successfully processed message, or in the event of a failed request, information regarding the reason for the failure.

    If the client receives a timeout error waiting for the response, check that the bridge is running.

Parent topic: Running the IBM MQ Bridge to blockchain Parent topic: Running the IBM MQ Bridge to blockchain