Skip to content

Azure-Samples/functions-quickstart-java-azd-eventhub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure Functions Event Hubs - Java

This project demonstrates an Azure Functions application written in Java that processes messages from Azure Event Hubs. It includes an Event Hub trigger function that reads messages from an input Event Hub, processes them, and sends the processed messages to an output Event Hub.

Features

  • Event Hub Trigger: Automatically processes messages from an input Event Hub
  • Event Hub Output Binding: Sends processed messages to an output Event Hub
  • Java: Azure Functions v4 programming model with Java annotations
  • Infrastructure as Code: Complete Bicep templates for Azure resources
  • Azure Developer CLI: Streamlined deployment with azd

Architecture

Input Event Hub → Azure Function → Output Event Hub
                      ↓
              Application Insights

Prerequisites

Project Structure

.
├── src/
│   └── main/
│       └── java/
│           └── com/
│               └── function/
│                   ├── EventHubsTrigger.java   # Event Hub trigger function
│                   └── TimerTrigger.java        # Timer trigger to generate test messages
├── infra/                                       # Bicep infrastructure files
│   ├── main.bicep                               # Main deployment template
│   ├── app/
│   │   ├── api.bicep                            # Function App configuration
│   │   ├── eventhubs.bicep                      # Event Hub resources
│   │   ├── rbac.bicep                           # Role assignments
│   │   └── vnet.bicep                           # Virtual network
│   ├── abbreviations.json                       # Resource naming conventions
│   └── main.parameters.json                     # Deployment parameters
├── pom.xml                                      # Maven build configuration
├── host.json
└── azure.yaml                                   # Azure Developer CLI configuration

Function Logic

The EventHubsTrigger function:

  1. Receives messages from the input Event Hub
  2. Parses and processes each message
  3. Adds metadata (ID, timestamp)
  4. Sends processed messages to the output Event Hub
  5. Logs processing information to Application Insights

Local Development

1. Build the Project

mvn clean package

2. Configure Local Settings

Create or update local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "java",
    "EventHubConnection__fullyQualifiedNamespace": "<your-eventhub-namespace>.servicebus.windows.net",
    "INPUT_EVENTHUB_NAME": "input-events",
    "OUTPUT_EVENTHUB_NAME": "output-events"
  }
}

3. Run Locally

mvn clean package
mvn azure-functions:run

Deploy to Azure

Using Azure Developer CLI (Recommended)

  1. Login to Azure:

    azd auth login
  2. Initialize the environment:

    azd env new

    Enter an environment name (e.g., "dev").

  3. Build and deploy:

    mvn clean package
    azd up

This command will:

  • Create all Azure resources (Event Hubs, Function App, Storage, Application Insights)
  • Deploy your function code
  • Configure all connections and settings

Manual Deployment

  1. Create Azure resources:

    az group create --name rg-eventhubs-java --location eastus
    az deployment group create --resource-group rg-eventhubs-java --template-file infra/main.bicep
  2. Deploy function code:

    mvn clean package
    func azure functionapp publish <function-app-name>

Testing

Send Test Messages

Use Azure CLI to send test messages to the input Event Hub:

az eventhubs eventhub message send \
  --namespace-name <your-namespace> \
  --eventhub-name input-events \
  --messages '[{"id": "1", "message": "Hello from CLI"}]'

Monitor Function Execution

  1. View logs in the portal:

    • Navigate to your Function App in Azure Portal
    • Go to "Monitor" → "Logs"
  2. View in Application Insights:

    az monitor app-insights query \
      --app <app-insights-name> \
      --analytics-query "traces | where message contains 'Event hub function' | order by timestamp desc | take 20"
  3. Check output Event Hub:

    • Verify processed messages in the output Event Hub using Azure Portal or Event Hub Explorer

Configuration

Environment Variables

Variable Description
EventHubConnection__fullyQualifiedNamespace Event Hub namespace (uses managed identity)
INPUT_EVENTHUB_NAME Name of the input Event Hub
OUTPUT_EVENTHUB_NAME Name of the output Event Hub
APPLICATIONINSIGHTS_CONNECTION_STRING Application Insights connection string

Managed Identity

The Function App uses managed identity to connect to Event Hubs. The infrastructure automatically assigns the "Event Hubs Data Owner" role to both your user account and the Function App's managed identity.

Troubleshooting

Function not triggering

  • Verify the Event Hub connection string and names
  • Check that messages are being sent to the input Event Hub
  • Review Application Insights logs for errors

Authentication errors

  • Ensure managed identity is enabled on the Function App
  • Verify role assignments (Event Hubs Data Owner) are configured
  • Check that the namespace FQDN is correct in settings

Node.js errors

  • Run npm install to ensure all dependencies are installed
  • Verify Node.js version (22.x or later)

Clean Up

To delete all Azure resources:

azd down

Or manually:

az group delete --name rg-eventhubs-javascript

Resources

License

This project is licensed under the MIT License.

About

About An Azure Functions QuickStart project that demonstrates how to use an Event Hubs Trigger with Azure Developer CLI (azd) for quick and easy deployment. This sample showcases a real-time news streaming system with automated content generation and intelligent processing.

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors