Skip to content

Instantly share code, notes, and snippets.

@anuchandy
Last active November 1, 2022 00:44
Show Gist options
  • Save anuchandy/e249aec9fd432ee4785b07c511283d8e to your computer and use it in GitHub Desktop.
Save anuchandy/e249aec9fd432ee4785b07c511283d8e to your computer and use it in GitHub Desktop.

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:

  1. Install docker.
  2. Download and extract the squid.zip file.
c:\squid
  |
  |-- docker-compose.yaml
  |-- password
  |-- squid.conf
  1. From the terminal, switch to the extracted directory squid.
  2. In the terminal, run the command docker-compose up, to start the docker container running the Squid Proxy.

The Proxy uses container port 3128, which is mapped to the client machine port 8080.

The Proxy connection details are

  1. The proxy_host = "127.0.0.1"
  2. The proxy_port = "8080"
  3. The proxy_username = "anu"
  4. The proxy_password = "1StepAhead!"

If you want to host the Proxy with a different username and password

  1. Install apache2-utils

    sudo apt install apache2-utils

  2. Generate a file usrpwd with the new proxy credentials
    • Run the command

      htpasswd -c usrpwd <user-name>

    • Enter the new password when prompted
    • A file named usrpwd will be created with the user name and encoded password.
  3. Copy the content of the file usrpwd, and paste it as the content of password file in the extracted squid directory.
  4. (Stop proxy container if already running and), start the Proxy container by running

    docker-compose up

That's It

At this point, the java code can connect to Event Hubs using custom endpoint with a domain through Squid Proxy.

List<EventData> telemetryEvents = Arrays.asList(
        new EventData("Roast beef".getBytes(UTF_8)),
        new EventData("Cheese".getBytes(UTF_8)),
        new EventData("Tofu".getBytes(UTF_8)),
        new EventData("Turkey".getBytes(UTF_8)));


AzureNamedKeyCredential credential = new AzureNamedKeyCredential("RootManageSharedAccessKey", 
        "<Shared Access Policy Primary or Secondary Key from the Portal>");

ProxyOptions proxyOptions = new ProxyOptions(
        ProxyAuthenticationType.BASIC,
        new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)),
        "anu", "1StepAhead!");

EventHubProducerClient producer = new EventHubClientBuilder()
        .proxyOptions(proxyOptions)
        .credential("<eventubs-namespace>.servicebus.windows.net", "<eventhubs-name>", credential)
        .customEndpointAddress("https://<custom-domain-app-gateway>") // e.g. "https://apg-eh.anuchan.us"
        .transportType(AmqpTransportType.AMQP_WEB_SOCKETS)
        .buildProducerClient();

EventDataBatch currentBatch = producer.createBatch();

for (EventData event : telemetryEvents) {
    if (currentBatch.tryAdd(event)) {
        continue;
    }

    producer.send(currentBatch);
    currentBatch = producer.createBatch();

    if (!currentBatch.tryAdd(event)) {
        System.err.printf("Event is too large for an empty batch. Skipping. Max size: %s. Event: %s%n",
                currentBatch.getMaxSizeInBytes(), event.getBodyAsString());
    }
}

producer.send(currentBatch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment