Skip to content

Commit c1db46d

Browse files
Merge pull request #209370 from cicorias/patch-1
Provide a detailed example of using EventHubTrigger
2 parents 9315fd6 + 2545083 commit c1db46d

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,64 @@ public void eventHubProcessor(
286286

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

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

0 commit comments

Comments
 (0)