forked from Azure-Samples/azure-functions-samples-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHubTriggerFunction.java
125 lines (111 loc) · 6.92 KB
/
EventHubTriggerFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.functions;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import java.util.*;
/**
* Azure Functions with Azure Event Hub.
* https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs-trigger?tabs=java
*/
public class EventHubTriggerFunction {
/**
* This function will be invoked when a new message is received at the specified EventHub. The message contents are provided as input to this function.
*/
@FunctionName("EventHubTriggerAndOutputJSON")
public void EventHubTriggerAndOutputJSON(
@EventHubTrigger(name = "messages", eventHubName = "test-inputjson-java", connection = "AzureWebJobsEventHubSender", cardinality = Cardinality.MANY) List<String> messages,
@EventHubOutput(name = "output", eventHubName = "test-outputjson-java", connection = "AzureWebJobsEventHubSender") OutputBinding<String> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub trigger received " + messages.size() + " messages");
output.setValue(messages.get(0));
}
@FunctionName("EventHubTriggerAndOutputString")
public void EventHubTriggerAndOutputString(
@EventHubTrigger(name = "messages", eventHubName = "test-input-java", connection = "AzureWebJobsEventHubSender", dataType = "string", cardinality = Cardinality.MANY) String[] messages,
@BindingName("SystemPropertiesArray") SystemProperty[] systemPropertiesArray,
@EventHubOutput(name = "output", eventHubName = "test-output-java", connection = "AzureWebJobsEventHubSender") OutputBinding<String> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub trigger received " + messages.length + " messages");
context.getLogger().info("SystemProperties for message[0]: EnqueuedTimeUtc=" + systemPropertiesArray[0].EnqueuedTimeUtc + " Offset=" + systemPropertiesArray[0].Offset);
output.setValue(messages[0]);
}
@FunctionName("EventHubTriggerCardinalityOne")
public void EventHubTriggerCardinalityOne(
@EventHubTrigger(name = "message", eventHubName = "test-inputOne-java", connection = "AzureWebJobsEventHubSender", dataType = "string", cardinality = Cardinality.ONE) String message,
@EventHubOutput(name = "output", eventHubName = "test-outputone-java", connection = "AzureWebJobsEventHubSender") OutputBinding<String> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub trigger received message" + message);
output.setValue(message);
}
/**
* This function verifies the above functions
*/
@FunctionName("EventHubOutputJson")
public void TestEventHubOutputJson(
@EventHubTrigger(name = "message", eventHubName = "test-outputjson-java", connection = "AzureWebJobsEventHubSender") String message,
@QueueOutput(name = "output", queueName = "test-eventhuboutputjson-java", connection = "AzureWebJobsStorage") OutputBinding<String> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub Output function processed a message: " + message);
output.setValue(message);
}
@FunctionName("EventHubOutput")
public void TestEventHubOutput(
@EventHubTrigger(name = "message", eventHubName = "test-output-java", connection = "AzureWebJobsEventHubSender", cardinality = Cardinality.ONE) String message,
@QueueOutput(name = "output", queueName = "test-eventhuboutput-java", connection = "AzureWebJobsStorage") OutputBinding<String> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub Output function processed a message: " + message);
output.setValue(message);
}
@FunctionName("EventHubOutputInputOne")
public void TestEventHubOutputInputOne(
@EventHubTrigger(name = "message", eventHubName = "test-outputone-java", connection = "AzureWebJobsEventHubSender", cardinality = Cardinality.ONE) String message,
@QueueOutput(name = "output", queueName = "test-eventhuboutputone-java", connection = "AzureWebJobsStorage") OutputBinding<String> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub Output function processed a message: " + message);
output.setValue(message);
}
@FunctionName("EventHubTriggerAndOutputBinaryCardinalityManyListBinary")
public void EventHubTriggerAndOutputBinaryCardinalityManyListBinary(
@EventHubTrigger(name = "messages", eventHubName = "test-binary-input-cardinality-many-list-java", connection = "AzureWebJobsEventHubSender_2", dataType = "binary", cardinality = Cardinality.MANY) List<byte[]> messages,
@QueueOutput(name = "output", queueName = "test-binary-output-cardinality-many-list-java", connection = "AzureWebJobsStorage") OutputBinding<byte[]> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub trigger received " + messages.size() + " messages");
output.setValue(messages.get(0));
}
@FunctionName("EventHubTriggerAndOutputBinaryCardinalityOne")
public void EventHubTriggerAndOutputBinaryCardinalityOne(
@EventHubTrigger(name = "message", eventHubName = "test-binary-input-cardinality-one-java", connection = "AzureWebJobsEventHubSender_2", dataType = "binary", cardinality = Cardinality.ONE) byte[] message,
@QueueOutput(name = "output", queueName = "test-binary-output-cardinality-one-java", connection = "AzureWebJobsStorage") OutputBinding<byte[]> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub trigger received message" + message);
output.setValue(message);
}
@FunctionName("EventHubTriggerAndOutputBinaryCardinalityManyArrayBinary")
public void EventHubTriggerAndOutputBinaryCardinalityManyArrayBinary(
@EventHubTrigger(name = "messages", eventHubName = "test-binary-input-cardinality-many-array-java", connection = "AzureWebJobsEventHubSender_2", dataType = "binary", cardinality = Cardinality.MANY) byte[][] messages,
@QueueOutput(name = "output", queueName = "test-binary-output-cardinality-many-array-java", connection = "AzureWebJobsStorage") OutputBinding<byte[]> output,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub trigger received " + messages.length + " messages");
output.setValue(messages[0]);
}
public static class SystemProperty {
public String SequenceNumber;
public String Offset;
public String PartitionKey;
public String EnqueuedTimeUtc;
public SystemProperty(String sequenceNumber, String offset, String partitionKey, String enqueuedTimeUtc) {
this.SequenceNumber = sequenceNumber;
this.Offset = offset;
this.PartitionKey = partitionKey;
this.EnqueuedTimeUtc = enqueuedTimeUtc;
}
}
}