-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathPrintTopicMessage.java
55 lines (48 loc) · 2.12 KB
/
PrintTopicMessage.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
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package main.java.com.function;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.dapr.annotation.DaprServiceInvocationTrigger;
import com.microsoft.azure.functions.dapr.annotation.DaprStateInput;
import com.microsoft.azure.functions.dapr.annotation.DaprStateOutput;
import com.microsoft.azure.functions.dapr.annotation.DaprTopicTrigger;
import com.microsoft.azure.functions.OutputBinding;
import java.util.Optional;
import java.util.logging.Logger;
/**
* Azure Functions with DaprTopicTrigger.
*/
public class PrintTopicMessage {
/**
* This function gets invoked by dapr runtime, when a message is published to topic B.
*/
@FunctionName("PrintTopicMessage")
public String run(
@DaprTopicTrigger(
pubSubName = "%PubSubName%",
topic = "B")
String payload,
final ExecutionContext context) throws JsonProcessingException {
Logger logger = context.getLogger();
logger.info("Java function processed a PrintTopicMessage request from the Dapr Runtime.");
// Get the CloudEvent data from the request body as a JSON string
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(payload);
String data = jsonNode.get("data").asText();
logger.info("Topic B received a message: " + data);
return data;
}
}