Hyperledger Java SDK Client Event Listener to register and listen Blocks

+1 vote

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");

Can someone tell me what to do next?

Oct 29, 2018 in Blockchain by digger
• 26,740 points

3 answers to this question.

+2 votes
Best answer

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

        BlockListener blockListener = new BlockListener() {         
        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..");

answered Oct 29, 2018 by Omkar
• 69,210 points

selected May 7, 2019 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, 2019 by Pavan
0 votes
answered May 7, 2019 by Batin

Related Questions In Blockchain

+1 vote
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
+1 vote
2 answers
0 votes
1 answer

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

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

answered Oct 30, 2018 in Blockchain by Omkar
• 69,210 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP