Listening to events -fabric client java
Hi,
How to subscribe to block events in fabric client (java)?
I was looking for an equivalent of this in fabric client:
Consumer<BlockEvent> blockListener = blockEvent -> blockListenerProcess();
networkObj.addBlockListener(startBlockNumber, blockListener);
Will it be possible to extract chaincode events from block events in this case?
In fabric gateway,we can use like this:
blockEvent.getTransactionEvents()
for (BlockEvent.TransactionEvent tEvent : transEvents)
tEvent.getTransactionActionInfo(0).getEvent().getEventName()
Is it possible to do something similar in fabric client (java)?
Hello – yes I’ve got an example of that… somewhere…
I’ll push it a github repo as it’s a useful example
Thanks Matthew
Sent: 25 October 2022 10:05
To: fabric@...
Subject: [EXTERNAL] [Hyperledger Fabric] Listening to events -fabric client java
Hi, How to subscribe to block events in fabric client (java)? I was looking for an equivalent of this in fabric client: Consumer<BlockEvent> blockListener = blockEvent -> blockListenerProcess(); networkObj. addBlockListener(startBlockNumber,
ZjQcmQRYFpfptBannerStart
|
ZjQcmQRYFpfptBannerEnd
Hi,
How to subscribe to block events in fabric client (java)?
I was looking for an equivalent of this in fabric client:
Consumer<BlockEvent> blockListener = blockEvent -> blockListenerProcess();
networkObj.addBlockListener(startBlockNumber, blockListener);
Will it be possible to extract chaincode events from block events in this case?
In fabric gateway,we can use like this:
blockEvent.getTransactionEvents()
for (BlockEvent.TransactionEvent tEvent : transEvents)
tEvent.getTransactionActionInfo(0).getEvent().getEventName()
Is it possible to do something similar in fabric client (java)?
IBM United Kingdom Limited
Registered in England and Wales with number 741598
Registered office: PO Box 41, North Harbour, Portsmouth, Hants. PO6 3AU
- Fabric Gateway client API (Fabric v2.4+): Network.getChaincodeEvents() with examples of usage at the top of the Network API documentation and the asset-transfer-events sample.
- fabric-gateway-java: Contract.addContractListener().
The Fabric Gateway client API provides by far the most efficient Java client implementation of both block and chaincode eventing.
I would encourage you to avoid using fabric-sdk-java and prefer one of the high-level APIs above depending on the version of Fabric you are targeting. If you really must use the low-level legacy SDK for some reason, it provides chaincode event listening with Channel.registerChaincodeEventListener().
If you want to do the work yourself of extracting chaincode events from block events using the low-level legacy SDK, the block events you receive from fabric-sdk-java are the same as the ones surfaced by fabric-gateway-java, and you can listen for them using Channel.registerBlockListener(). Just be aware that you may receive duplicate or out-of-order events using fabric-sdk-java.
fabric-sdk-java provides very weak support for replay of events. The recommended approach is to create a new channel, remove all the peers and then re-add them to the channel with their peer options modified to specify an event start block. If you want to pick apart how fabric-gateway-java does this on top of fabric-sdk-java, the code is here:
https://github.com/hyperledger/fabric-gateway-java/blob/00afa0c6a376feea492b9d9390ae9415b404289d/src/main/java/org/hyperledger/fabric/gateway/impl/event/ReplayListenerSession.java#L31-L71