forked from Azure-Samples/azure-functions-samples-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventGridTriggerFunction.java
114 lines (92 loc) · 3.35 KB
/
EventGridTriggerFunction.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
package com.functions;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import java.util.*;
/**
* Azure Functions with Event Grid trigger.
* https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid-trigger?tabs=java%2Cbash
*/
public class EventGridTriggerFunction {
/**
* This function will be invoked when an event is received from Event Grid.
*/
@FunctionName("EventGridTriggerJava")
public void eventGridHandler(
@EventGridTrigger(name = "eventgrid") String eventContent,
final ExecutionContext context
) {
context.getLogger().info("Java Event Grid trigger function executed.");
context.getLogger().info(eventContent);
}
/**
* This function will be invoked when a http trigger is received, and sends a custom event to Event Grid.
*/
@FunctionName("EventGridOutputBindingJava")
public HttpResponseMessage eventGridOutputBinding(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
@EventGridOutput(name = "outputEvent",
topicEndpointUri = "AzureWebJobsEventGridOutputBindingTopicUriString",
topicKeySetting = "AzureWebJobsEventGridOutputBindingTopicKeyString") OutputBinding<EventGridEvent> outputEvent,
final ExecutionContext context
) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
String query = request.getQueryParameters().get("testuuid");
String message = request.getBody().orElse(query);
context.getLogger().info("testuuid:" + message);
final EventGridEvent eventGridOutputDocument = new EventGridEvent();
eventGridOutputDocument.setId("test-id");
eventGridOutputDocument.setEventType("test-event-1");
eventGridOutputDocument.setEventTime("2020-01-31T10:10:10+00:00");
eventGridOutputDocument.setDataVersion("1.0");
eventGridOutputDocument.setSubject("test-subject");
eventGridOutputDocument.setData("test-uuid: " + message);
outputEvent.setValue(eventGridOutputDocument);
return request.createResponseBuilder(HttpStatus.OK).build();
}
}
class EventGridEvent {
private String id;
private String eventType;
private String subject;
private String eventTime;
private String dataVersion;
private String data;
public String getId() {
return id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getDataVersion() {
return dataVersion;
}
public void setDataVersion(String dataVersion) {
this.dataVersion = dataVersion;
}
public String getEventTime() {
return eventTime;
}
public void setEventTime(String eventTime) {
this.eventTime = eventTime;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public void setId(String id) {
this.id = id;
}
}