Skip to content

Instantly share code, notes, and snippets.

@anuchandy
anuchandy / SessionIdleTimeout.md
Last active February 1, 2024 22:28
The Session Idle Timeout

The Session Idle Timeout

The session idle timeout is a client-side feature. It enables applications to transparently roll to a new session when no message arrives in the current session for a period.

Current implementation:

In the current implementation, the idle timer starts as soon as a message is emitted downstream. The timer is canceled if the next message arrives within the timeout. If no messages arrive within the timeout, the timeout is triggered to close the session.

A simplified flow looks like below [where Stage* is a stage in the asynchronous pipeline] :

@anuchandy
anuchandy / PublishEventListUsingBatch.java
Created May 31, 2023 23:22
PublishEventListUsingBatch.java
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.messaging.eventhubs;
import com.azure.messaging.eventhubs.models.CreateBatchOptions;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.Iterator;

The message batching

When sending a batch of messages, Service Bus and Event Hubs libraries have the following logic -

  1. Allocate a byte array for the Uber batch message.
  2. Iterate through the list of messages, encode each message, and append it to the above byte array.

The encode and append operations are handled by the 'encode' API1 in the QPid library.

NonSessionProcessor

NonSessionProcessor

MessagePump (internal to NonSessionProcessor)

MessagePump

NonSessionProcessor

NonSessionProcessor

MessagePump (internal to NonSessionProcessor)

MessagePump

HTTP Proxy and Event Hubs SDK

This document is a continuation of the Gist AzureEventHubsJavaSDKCustomEndpointWebSockets.md, which demonstrated front-ending Event Hubs with a custom endpoint (endpoint as AppGateway).

As the next step, we'll place an "HTTP Proxy" between the Event Hubs SDK running in the client machine and the host (AppGateway) that the custom endpoint addresses.

Setting up the HTTP Proxy

The blog explains in detail how to run the popular HTTP Proxy, Squid, on a docker container in a client machine. To simplify the setup process, follow the steps below:

Custom Domain and CustomEndpointAddress

This document is a continuation of the Gist AzureEventHubsJavaSDKCustomEndpointWebSockets.md, where we are going to use a custom domain (e.g., apg-eh.anuchan.us) to connect to the Application Gateway (instead of using its Public IPv4 address, e.g., 20.232.196.115).

If you own the root domain (e.g., anuchan.us), we can use Azure DNS Zone to create a DNS record (e.g., apg-eh.anuchan.us) under the root and associate it with the Application Gateway Public IPv4 address.

Public Azure DNS Zone

Let's create an Azure DNS Zone by following the official documentation.

Private Endpoint enabled Event Hubs

Enabling Private Endpoint assigns a Private IP from a subnet in an Azure Virtual Network to the Event Hubs.

If we enable Private Endpoint and configure the Event Hubs to restrict all public access, then only the hosts (e.g., VM) belonging to a subnet in the same virtual network can access the Event Hubs.

It means we can run Azure Event Hubs SDK in such a VM to access the Event Hubs securely. A DNS lookup from the VM for FQDN of the Event Hubs will get resolved to the Private IP assigned to the Event Hubs.

Event Hubs, Service Principal (and optional CustomEndpointAddress)

Assuming you already have an Event Hubs namespace and an Event Hub in it, let's create a Service Principal and associate the Event Hub Send and Receive role to access the Event Hubs namespace.

We'll then use the Service Principal in Java code to authenticate and send events to the Event Hub.

Create Service Principal

az ad sp create-for-rbac -n eh-service-principal --skip-assignment

Azure Event Hubs Java SDK Custom endpoint and AMQP WebSockets

This document demonstrates the "CustomEndpointAddress" feature of Azure Event Hubs Java SDK with AMQP WebSockets transport.

The custom endpoint address refers to a developer-provided endpoint address resolvable to EventHubs or configured to route traffic to EventHubs.

The document uses an AppGateway configured to route traffic to its "Public IP" to an EventHubs, so AppGateway's "PublicIP" is the custom endpoint address.

Before going into details, an important note -