Reputation: 1487
My first approach was to use the com.microsoft.azure:azure-eventhubs:3.2.0
dependency but I had the following problem: Client hangs when calling Azure Event Hub and facing connection error
So my 2nd approach can be
Could you please recommend me library that supports OAuth2?
Or a sample code which supports message sending to Event Hub over AMQP with OAuth2?
Or a 3rd approach ...?
Thanks,
V.
Upvotes: 0
Views: 5488
Reputation: 6508
Regarding your hang issue, I posted a new answer in the original thread Client hangs when calling Azure Event Hub and facing connection error. That should fix your issue.
Now coming back to this question, primarily you should follow the latest tutorial (uses Event Hub v5 sdk using Producer/Consumer pattern).
import com.azure.messaging.eventhubs.*;
public class Sender {
public static void main(String[] args) {
final String connectionString = "EVENT HUBS NAMESPACE CONNECTION STRING";
final String eventHubName = "EVENT HUB NAME";
// create a producer using the namespace connection string and event hub name
EventHubProducerClient producer = new EventHubClientBuilder()
.connectionString(connectionString, eventHubName)
.buildProducerClient();
// prepare a batch of events to send to the event hub
EventDataBatch batch = producer.createBatch();
batch.tryAdd(new EventData("First event"));
batch.tryAdd(new EventData("Second event"));
batch.tryAdd(new EventData("Third event"));
batch.tryAdd(new EventData("Fourth event"));
batch.tryAdd(new EventData("Fifth event"));
// send the batch of events to the event hub
producer.send(batch);
// close the producer
producer.close();
}
}
Regarding the question on OAuth2, it is very much possible to leverage AAD authentication approach instead of Shared Access Signature (the default connection string approach). You can follow this tutorial for Managed Identity scenario OR this tutorial for custom AAD application based auth.
Regarding the recommendation on OAuth2 library, the first party choice in the Azure world would be MSAL for seamless integration and support.
Regarding the question on writing the whole thing by custom code, while it's technically possible, but honestly it's super impractical to invest on this mammoth task due to following reasons:
Using standard libraries not only saves you from lot of efforts and money, but ensures reusability, and support from both the creator and communities.
Upvotes: 1