Fabric Java SDK for using Channel-based event hub service #fabric-sdk-java #fabric-sdk-node #hyperledger-fabric


chintanr97@...
 

I am trying to use Fabric Java SDK to perform chaincode related operations. For this purpose, I wish to make use of the new Channel Event Hub functionality for listening to different events, mainly Blocks Events and Transaction Events.
 
I know how to perform these tasks using Fabric Node SDK by creating an instance of `channelEventHub` class. But all the examples related to Java SDK mainly use event hub service by creating the event hub object as follows:
 
    EventHub eventHub = client.newEventHub("eventhub01", "grpcs://localhost:7053", props);
 
Which I feel is making use of old technique of connecting to peer node for listening to events (please correct me if this understanding is wrong). I wish to make use of the feature identical to channel event hub service in Node SDK as I am making use of Kubernetes to run my nodes and only communication that can be made to peers is over port `443` (being redirected to `7051`). 
 
 1. Do we have an example where we make use of channel event hub service in Java SDK? I think it should properly documented as good as the Node SDK documentation.
 2. I am understanding that the new channel event hub service would also connect to one of the peers on the channel because in Node SDK I find the function signature as follows: `newChannelEventHub(peer)`
 
Now, is this understanding correct? 
 
 - If yes, then in Java SDK is it correct to use `grpcs://<peerEndpoint>:443` for creating event hub connection, where `443` will be directed to internal port `7051`?
 - If no, then how to perform channel-based event hub service in Java SDK?


Mark Lewis
 

You are correct that EventHub is the wrong thing to be using, and this is removed in the v2.x SDK. If possible, I would strongly recommend using fabric-gateway-java rather than fabric-sdk-java. It layers on top of fabric-sdk-java and provides a much more friendly API, saving you worrying about the implementation details of what event service you are using. Documentation can be found at:

You can receive block events by simply adding your block listener to a Network object (which corresponds to a Channel):

Using this API you get fault tolerance, block ordering and deduplication for free. From a block event you can iterate over the transaction events contained within that block.

The fabric-gateway-java API already deals with monitoring transaction commit events after submitting a transaction for you, but if you really need to observe when transactions get committed on each of a set of peers, you can do that by adding a commit listener to a Network object.

If you want to do work in response to chaincode events emitted by committed transactions, you can do this by adding a contract listener to a Contract object (which corresponds to a smart contract / chaincode):