forked from elasticio/java-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.java
More file actions
117 lines (111 loc) · 3.72 KB
/
Copy pathModule.java
File metadata and controls
117 lines (111 loc) · 3.72 KB
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
package io.elastic.api;
import javax.json.Json;
import javax.json.JsonObject;
/**
* A component is an unit implementing a custom business logic to be executed
* in the elastic.io runtime.
*
* <p>
* A component is executed with a {@link ExecutionParameters} which provides
* the component with a {@link Message}, configuration and a snapshot.
* </p>
*
* <p>
* The given {@link Message} represents component's input. Besides other things
* it contains a payload to be consumed by a component.
* </p>
*
* <p>
* A configuration is an instance {@link javax.json.JsonObject} containing
* required information, such as API key or username/password combination, that
* components needs to collect from user to function properly.
* </p>
*
* <p>
* A snapshot is an instance of {@link javax.json.JsonObject} that represents
* component's state. For example, a Twitter timeline component might store the id
* of the last retrieved tweet for the next execution in order to ask Twitter for
* tweets whose ids are greater than the one in the snapshot.
* </p>
*
* A component communicates with elastic.io runtime by emitting events, such as
* <i>data</i>, <i>error</i>, etc. For more info please check out {@link EventEmitter}.
*
* <p>
* The following example demonstrates a simple component which echos the incoming message.
* </p>
*
* <pre>
* <code>
*
* public class EchoComponent implements Module {
*
*
* @Override
* public void execute(ExecutionParameters parameters) {
*
* final JsonObject snapshot = Json.createObjectBuilder()
* .add("echo", parameters.getSnapshot())
* .build();
*
* parameters.getEventEmitter()
* .emitSnapshot(snapshot)
* .emitData(echoMessage(parameters));
* }
*
* private Message echoMessage(ExecutionParameters parameters) {
*
* final Message msg = parameters.getMessage();
*
* final JsonObject body = Json.createObjectBuilder()
* .add("echo", msg.getBody())
* .add("config", parameters.getConfiguration())
* .build();
*
* return new Message.Builder()
* .body(body)
* .attachments(msg.getAttachments())
* .build();
* }
* }
* </code>
* </pre>
*
* @see ExecutionParameters
* @see Message
* @see EventEmitter
*/
public interface Module {
/**
* Executes this component with the given {@link ExecutionParameters}.
*
* @param parameters parameters to execute component with
*/
void execute(ExecutionParameters parameters);
/**
* Used to initialize the component on flow start. For example, a webhook trigger can subscribe a webhook url
* to receive events from a target API inside this method. The subscription data, such as ID, can be returned
* from this method as JSON object for persistence in the platform. Once <i>shutdown</i> method is
* supported, the persisted JSON will be passed as argument to <i>shutdown</i> method where the subscription
* can be canceled on flow stop.
*
* @since 2.0
*
* @param configuration component's configuration
* @return JSON object to be persisted or null
*/
default JsonObject startup(final JsonObject configuration) {
return Json.createObjectBuilder().build();
}
/**
* Used to initialize a component before message processing. For polling flows this method
* is called once per scheduled execution. For real-time flows this method is called once on first execution.
*
* @since 2.0
*
* @param configuration component's configuration
*/
default void init(final JsonObject configuration) {
// default implementation does nothing
}
}