Skip to content

Latest commit

 

History

History

azureeventhubreceiveevent

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Azure Event Hub receive event Java example

This folder contains a Java application example that handles Event Hubs on Microsoft Azure.

It handles an Event Hub and receive events from an event hub event stream.

Requirements

  • You must have a Microsoft Azure subscription.

  • You must have the following installed:

    • Java Development Kit (JDK) 8
    • Apache Maven 3
    • Azure CLI
  • The code was written for:

    • Java 8
    • Apache Maven 3
    • Azure SDK for Java: New Client Libraries (Azure Event Hub library v5) (com.azure)

Using the code

  • # Azure (Interactively).

    The Azure CLI's default authentication method for logins uses a web browser and access token to #.

    1. Run the Azure CLI login command.

      az login

      If the CLI can open your default browser, it will do so and load an Azure sign-in page.

      Otherwise, open a browser page at https://aka.ms/devicelogin and enter the authorization code displayed in your terminal.

      If no web browser is available or the web browser fails to open, use device code flow with az login --use-device-code.

    2. # with your account credentials in the browser.

    Make sure you select your subscription by:

    az account set --subscription <name or id>
  • Create an Event Hubs Namespace and an Event Hub.

    1. Create an Event Hubs Namespace.

      An Event Hubs namespace provides a unique scoping container, in which you create one or more event hubs.

      To create a namespace in your resource group using the portal, do the following actions:

      1. You must create the Event Hubs Namespace, using the Azure console.

      2. Select the your data for: Suscription, Resource group, Namespace name and Location.

      3. Choose Basic for the # tier.

    2. Create an Event Hub.

      You must create the Event Hub, using the Azure console.

      To create an event hub within the namespace, do the following actions:

      1. On the Event Hubs Namespace page, select Event Hubs in the left menu.

      2. At the top of the window, select + Event Hub.

      3. Type a name for your event hub, then select Create.

    3. Create a SAS Policy.

      You must create the SAS Policy, using the Azure console.

      1. On the Event Hubs page for the Event Hub created, select Shared access policies in the left menu.

      2. At the top of the window, select + Add.

      3. Type a name for your Policy, select Manage, that includes Send and Listen, then select Create.

  • Create an Azure storage account and a blob container.

    Create an Azure storage account and a blob container in it by doing the following steps, using the Azure console:

    1. Create an Azure Storage account.

    2. Create a blob container.

    3. Get the connection string to the storage account.

  • Configure your application.

    The configuration is stored in the app.properties properties file, located in the path src/main/resources. The file content is:

    StorageAccountConnectionString=<STORAGE_ACCOUNT_CONNECTION_STRING>
    StorageAccountContainerName=<STORAGE_ACCOUNT_CONTAINER_NAME>
    EventHubConnectionString=<EVENT_HUB_CONNECTION_STRING>
    EventHubName=<EVENT_HUB_NAME>

    You must edit the file app.properties and replace the values of:

    • <STORAGE_ACCOUNT_CONNECTION_STRING> by the Connection string for the Storage Account.
    • <STORAGE_ACCOUNT_CONTAINER_NAME> by the Blob name in the Storage Account.
    • <EVENT_HUB_CONNECTION_STRING> by the Connection string for the Event Hub.
    • <EVENT_HUB_NAME> by the name of the Event Hub.

    The application uses this information for accessing your Event Hub and Storage Account.

  • Run the code.

    Execute the receiver application:

    java -jar azureeventhubreceiveevent.jar

    You should see the next message:

    Starting event processor ...
    Waiting for an event ...
    Press enter to stop.

    The application is waiting for some event from the Event Hub.

  • Test the application.

    You must send an event to your Event Hub.

    You can use the Java application azureeventhubsendevent.jar (Event Hub send event). You can get it following this link: ../azureeventhubsendevent/

    In another command line console, execute the sender application:

    java -jar azureeventhubsendevent.jar

    You should see the next message:

    Preparing batch of events ...
    Sending batch of events to Event Hub ...
    Sent

    When the receiver application gets the events, you should see the next message in you receiver application:

    Processing event from partition <X> with sequence number <X> with body: First event
    Processing event from partition <X> with sequence number <X> with body: Second event
    Processing event from partition <X> with sequence number <X> with body: Third event