Skip to content

Commit cfcc62e

Browse files
authored
Provide a detailed example of using EventHubTrigger
>NOTE: this is a duplicate of PR #97567. Moved here as I'm internal. This example provides a clear example and details on using Bindings of EventHubTrigger in Java -- any more details are here and please advise on what else to add: https://github.com/cicorias/azfunct-java-eventhubs
1 parent 77915ec commit cfcc62e

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

includes/functions-bindings-event-hubs-trigger.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,65 @@ public void eventHubProcessor(
287287
In the [Java functions runtime library](/java/api/overview/azure/functions/runtime), use the `EventHubTrigger` annotation on parameters whose value comes from the event hub. Parameters with these annotations cause the function to run when an event arrives. This annotation can be used with native Java types, POJOs, or nullable values using `Optional<T>`.
288288

289289
::: zone-end
290+
291+
::: zone pivot="programming-language-java"
292+
293+
The following example illustrates extensive use of `SystemProperties` and other Binding options for further introspection of the Event along with providing a well-formed `BlobOutput` path that is Date hierarchical.
294+
295+
```java
296+
package com.example;
297+
import java.util.Map;
298+
import java.time.ZonedDateTime;
299+
300+
import com.microsoft.azure.functions.annotation.*;
301+
import com.microsoft.azure.functions.*;
302+
303+
/**
304+
* Azure Functions with Event Hub trigger.
305+
* and Blob Output using date in path along with sequenct and partition ID
306+
*/
307+
public class EventHubReceiver {
308+
309+
@FunctionName("EventHubReceiver")
310+
@StorageAccount("bloboutput")
311+
312+
public void run(
313+
@EventHubTrigger(name = "message",
314+
eventHubName = "%eventhub%",
315+
consumerGroup = "%consumergroup%",
316+
connection = "eventhubconnection",
317+
cardinality = Cardinality.ONE)
318+
String message,
319+
320+
final ExecutionContext context,
321+
322+
@BindingName("Properties") Map<String, Object> properties,
323+
@BindingName("SystemProperties") Map<String, Object> systemProperties,
324+
@BindingName("PartitionContext") Map<String, Object> partitionContext,
325+
@BindingName("EnqueuedTimeUtc") Object enqueuedTimeUtc,
326+
327+
@BlobOutput(
328+
name = "outputItem",
329+
path = "iotevents/{datetime:yy}/{datetime:MM}/{datetime:dd}/{datetime:HH}/" +
330+
"{datetime:mm}/{PartitionContext.PartitionId}/{SystemProperties.SequenceNumber}.json")
331+
OutputBinding<String> outputItem) {
332+
333+
var et = ZonedDateTime.parse(enqueuedTimeUtc + "Z"); // needed as the UTC time presented does not have a TZ
334+
// indicator
335+
context.getLogger().info("Event hub message received: " + message + ", properties: " + properties);
336+
context.getLogger().info("Properties: " + properties);
337+
context.getLogger().info("System Properties: " + systemProperties);
338+
context.getLogger().info("partitionContext: " + partitionContext);
339+
context.getLogger().info("EnqueuedTimeUtc: " + et);
340+
341+
outputItem.setValue(message);
342+
}
343+
}
344+
345+
```
346+
347+
::: zone-end
348+
290349
::: zone pivot="programming-language-csharp"
291350
## Attributes
292351

0 commit comments

Comments
 (0)