Json as Event Hubs message serialization format
Is it possible to use JSON serializer for serializing Event Hubs stream messages? We are trying to use the Event Hubs stream provider for communication between different clusters (with different codebase and purpose), and using default binary serializer puts usual compatibility issues of the binary format which is something we would like to avoid by using other serializer.
I think it is possible depends on how complicated your event hub message is, but I will @jason-bragg and @ReubenBond since they are the two people who are most familiar with the event hub and serializing.
To my knowledge no effort has been made to ensure that the eventhub stream provider works with json serialization.
The eventhub adapter does, however, support customization of how data is written to and read from eventhub. We have consumers that use this feature, so it has been demonstrated to work, but it's not very convenient.
If you need to control format of written data you'll need to override the QueueMessageBatchAsync in the adapter (implemented in EventHubAdapterFactory).
For custom deserialization of eventhub messages one will need to implement a specialization of the EventHubDataAdapter and configure the adapter to use it. We don't have a full example of this in our codebase, but a somewhat related example can be found in the StreamPerPartitionEventHubStreamProvider in the ServiceBus.Tests assembly. This stream provider uses a custom EventHubDataAdapter, similar to what you'll need to do, but the example only customizes how streamId is derived from the eventhub data, while you'll need to override the GetBatchContainer to control format of read data.
I understand that customizing the adapter in these ways is not desirable, and I hope to spend more time improving this, but in it's current state, it's 'possible' but not 'pretty'.
@jason-bragg
If you need to control format of written data you'll need to override the QueueMessageBatchAsync in the adapter (implemented in EventHubAdapterFactory).
It will help if that method is virtual
What if he just installs an external serializer and implements json ser for his stream data types? Wouldn't it just work?
@gabikliot Not really, because the Event Hubs provider uses the private nested class Orleans.ServiceBus.Providers.EventHubBatchContainer.Body as a message wrapper. However, as a temporary workaround, I will try to define an external serializer for this private class. There are two problems with this approach even if it works:
- Obviously it relies on the internal implementation details which can change at any moment
- The serializer can be set globally, so it will be not possible to have two Event Hubs providers with different serialization formats registered at the same time
Will it make sense to refactor Event Hubs provider implementation to allow the serialization logic to be pluggable?
@DixonDs
It will help if that method is virtual.
Agreed. One can work around this by explicitly implementing a IQueueAdapter.QueueMessageBatchAsync(..) call, but as with the rest of this workaround, it's possible but not pretty. I've not thought as deeply on this as I should, but I am clear on the fact that having a customizable data adapter (which is the currently supported attempt to resolve this) is incomplete unless it is utilized in both the reading and writing of data, which it currently is not.
Will it make sense to refactor Event Hubs provider implementation to allow the serialization logic to be pluggable?
Probably. It is currently crudely customizable, but that's far from a good experience. We either need to get much better at maintaining our serialized data structures, including supporting a backwards compatible serializers, or have a reasonable default with customizable serialization so users can at least solve the problem themselves. My preference would be to do both.
We are marking this issue as stale due to the lack of activity in the past six months. If there is no further activity within two weeks, this issue will be closed. You can always create a new issue based on the guidelines provided in our pinned announcement.
This issue has been marked stale for the past 30 and is being closed due to lack of activity.