/
SpringCloudEventsApplication.java
134 lines (116 loc) · 4.65 KB
/
SpringCloudEventsApplication.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
126
127
128
129
130
131
132
133
134
package functions;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.ID;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SOURCE;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SPECVERSION;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SUBJECT;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.function.cloudevent.CloudEventHeaderEnricher;
import org.springframework.cloud.function.web.util.HeaderUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.messaging.Message;
@SpringBootApplication
public class SpringCloudEventsApplication {
private static final Logger LOGGER = Logger.getLogger(
SpringCloudEventsApplication.class.getName());
public static void main(String[] args) {
SpringApplication.run(SpringCloudEventsApplication.class, args);
}
private List<String> todos = new ArrayList<>();
private List<String> meetingReminders = new ArrayList<>();
@Bean
public Function<Message<Input>, Output> addTODO(CloudEventHeaderEnricher enricher) {
// If you are using CloudEvent Type based routing you should probably check that the CloudEvent that you
// are getting is the one that you are expecting.
return m -> {
HttpHeaders httpHeaders = HeaderUtils.fromMessage(m.getHeaders());
log(httpHeaders);
Input input = m.getPayload();
LOGGER.log(Level.INFO, "Input {0} ", input);
todos.add(input.input);
Output output = new Output();
output.input = input.input;
output.operation = httpHeaders.getFirst(SUBJECT)+"-ACK";;
output.output = input.input != null ? Arrays.toString(todos.toArray()) : "I don't remember anything to do.";
return output;
};
}
@Bean
public Function<Message<Input>, Output> addMeetingReminder(CloudEventHeaderEnricher enricher) {
// If you are using CloudEvent Type based routing you should probably check that the CloudEvent that you
// are getting is the one that you are expecting.
return m -> {
HttpHeaders httpHeaders = HeaderUtils.fromMessage(m.getHeaders());
log(httpHeaders);
Input input = m.getPayload();
LOGGER.log(Level.INFO, "Input {0} ", input);
meetingReminders.add(input.input);
Output output = new Output();
output.input = input.input;
output.operation = httpHeaders.getFirst(SUBJECT)+"-ACK";
output.output = input.input != null ? Arrays.toString(meetingReminders.toArray()) : "Weird.. I don't have any meetings to attend.";
return output;
};
}
@Bean
public Function<Message<Input>, Output> dysfunction(CloudEventHeaderEnricher enricher) {
// If you are using CloudEvent Type based routing you should probably check that the CloudEvent that you
// are getting is the one that you are expecting.
return m -> {
HttpHeaders httpHeaders = HeaderUtils.fromMessage(m.getHeaders());
log(httpHeaders);
Input input = m.getPayload();
LOGGER.log(Level.INFO, "Input {0} ", input);
todos.add(input.input);
Output output = new Output();
output.input = input.input;
output.operation = httpHeaders.getFirst(SUBJECT)+"-ACK";;
output.output = "Dysfunction is a function";
return output;
};
}
private void log(HttpHeaders httpHeaders) {
LOGGER.log(Level.INFO, "Input CE Id:{0}", httpHeaders.getFirst(
ID));
LOGGER.log(Level.INFO, "Input CE Spec Version:{0}",
httpHeaders.getFirst(SPECVERSION));
LOGGER.log(Level.INFO, "Input CE Source:{0}",
httpHeaders.getFirst(SOURCE));
LOGGER.log(Level.INFO, "Input CE Subject:{0}",
httpHeaders.getFirst(SUBJECT));
}
@Bean
public CloudEventHeaderEnricher attributesProvider() {
return attributes -> attributes
.setSpecVersion("1.0")
.setId(UUID.randomUUID()
.toString())
.setSource("http://salaboy.com/springnative-example")
.setType("com.salaboy.springnative.events");
}
/**
* Health checks
*
* @return
*/
@Bean
public Function<String, String> health() {
return probe -> {
if ("readiness".equals(probe)) {
return "ready";
} else if ("liveness".equals(probe)) {
return "live";
} else {
return "OK";
}
};
}
}