Hyperledger Java SDK Client Event Listener to register and listen Blocks

+2 votes

I am using Java SDK based client for the Hyperledger fabric and i am trying to register and listen to BLOCK and CHAINCODE events. I want to create a separate thread for this. 

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

Can someone tell me what to do next?

Oct 29, 2018 in Blockchain by digger
• 27,620 points
370 views

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+2 votes
Best answer

First add the eventhub, and then  register the listener for this event. Something like this.

        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);\
answered Oct 29, 2018 by Omkar
• 65,820 points

selected May 7 by digger
But is an (explicitly registered) Event Hub  needed in this case?
Yes, I think it is necessary to listen to the blocks.
I have running code that does not register Event Hubs and I still get Block & Chain Code events.
Hello @Curious. I don't think explicitly registered Event Hub is needed. You can listen to blocks even without that.
How are you running code without register Event Hubs? Because I tried to do that but it did not work.
+1 vote

I got this from the official documentation:

The following example will register a block listener to start receiving new blocks as they are added to the ledger.

// keep the block_reg to unregister with later if needed
block_reg = channel_event_hub.registerBlockEvent((block) => {
    console.log('Successfully received the block event');
    <do something with the block>
}, (error)=> {
    console.log('Failed to receive the block event ::'+error);
    <do something with the error>
});
answered May 7 by Pavan
0 votes
answered May 7 by Batin

Related Questions In Blockchain

+1 vote
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers
0 votes
1 answer

Hyperledger Fabric: How to retrieve success payload via nodejs client?

The channel.sendTransactionProposal generates a Respo ...READ MORE

answered Oct 30, 2018 in Blockchain by Omkar
• 65,820 points
27 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.