1

I have a Java SDK based client for the Hyperledger fabric and am able to connect and send transaction proposal requests, create entries in the ledger and query the ledger.

I am struggling with registering and listening to BLOCK and CHAINCODE events. I want to start a separate thread on the client that is dedicated to listening and processing these events.

What I found so far:

HFClient client = HFClient.createNewInstance();
Channel channel = client.newChannel("channel01");
EventHub eventHub = client.newEventHub("eh01", "grpc://abc.xyz.us:7053");
channel.addEventHub(eventHub);

Beyond this I am lost. Can someone please post a sample of how to do this?

hmahidhara
  • 13
  • 1
  • 7

2 Answers2

5

once you add the eventhub, you need to register the lister for this event. Please see the example below.

        BlockListener blockListener = new BlockListener() {         
        @Override
        public void received(BlockEvent arg0) {
            Block block = arg0.getBlock();

            System.out.println("BLock All FIelds :" + block.getAllFields());
            System.out.println("BLock Number :" + arg0.getBlockNumber());               

            System.out.println("THis is buyer Listener..");
        }
    };

    channel.registerBlockListener(blockListener);

Whatever you will will write in the received method will get called whenever the block is added to the blockchain. Hope this would help you.

Sandip Nirmal
  • 415
  • 4
  • 7
  • thanks for this example. how can I then handle chaincode events that are added to each transaction in a block? – a.hrdie Jun 15 '18 at 13:09
0

For transaction events you need another listener:

String chaincodeEventListenerHandle = channel.registerChaincodeEventListener(
                    Pattern.compile(".*"), Pattern.compile(Pattern.quote(EXPECTED_EVENT_NAME)),
                    (handle, blockEvent, chaincodeEvent) -> {
                        final String es = blockEvent.getPeer() != null
                                ? blockEvent.getPeer().getName()
                                : blockEvent.getEventHub().getName();
                        System.out.format(
                                "Chaincode event with handle: %s"
                                + ", thread name: %s"
                                + ", chaincode Id: %s"
                                + ", chaincode event name: %s"
                                + ", transaction id: %s"
                                + ", event payload: \"%s\""
                                + ", from eventhub: %s\n",
                                Thread.currentThread().getName(),
                                handle, 
                                chaincodeEvent.getChaincodeId(),
                                chaincodeEvent.getEventName(), 
                                chaincodeEvent.getTxId(),
                                new String(chaincodeEvent.getPayload()),
                                es);

                    });

...

 channel.unregisterChaincodeEventListener(chaincodeEventListenerHandle);