author | egahlin |
Mon, 03 Jun 2019 16:21:47 +0200 | |
branch | JEP-349-branch |
changeset 57386 | acdd0dbe37ee |
parent 57380 | 6a7e7743b82f |
child 57434 | 216bf2e3b542 |
permissions | -rw-r--r-- |
57361 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.consumer; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.file.Path; |
|
30 |
import java.time.Duration; |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
31 |
import java.time.Instant; |
57361 | 32 |
import java.util.function.Consumer; |
33 |
||
34 |
/** |
|
35 |
* Represents a stream of event that actions can be performed up on. |
|
36 |
*/ |
|
37 |
public interface EventStream extends AutoCloseable { |
|
38 |
||
39 |
/** |
|
40 |
* Creates a stream starting from the next written event in a disk |
|
41 |
* repository. |
|
42 |
* |
|
43 |
* @param directory location of the disk repository, not {@code null} |
|
44 |
* @return an event stream, not {@code null} |
|
45 |
*/ |
|
46 |
public static EventStream openRepository(Path directory) throws IOException { |
|
47 |
throw new UnsupportedOperationException("Not yet implemented"); |
|
57376 | 48 |
// AccessControlContext acc = AccessController.getContext(); |
49 |
// return new EventDirectoryStream(acc); |
|
57361 | 50 |
} |
51 |
||
52 |
/** |
|
53 |
* Creates an event stream starting from the first event in a file. |
|
54 |
* |
|
55 |
* @param file location of the file, not {@code null} |
|
56 |
* @return an event stream, not {@code null} |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
57 |
* |
57376 | 58 |
* @throws IOException if a stream can't be opened,or an I/O error occurs |
59 |
* during reading |
|
57361 | 60 |
*/ |
61 |
public static EventStream openFile(Path file) throws IOException { |
|
57377
528d85d2fce5
Add EventStream::setReuse. Works for file streams
egahlin
parents:
57376
diff
changeset
|
62 |
return new EventFileStream(file); |
57361 | 63 |
} |
64 |
||
65 |
/** |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
66 |
* Creates an event stream starting start time and end time in a file. |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
67 |
* |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
68 |
* @param file location of the file, not {@code null} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
69 |
* |
57376 | 70 |
* @param the start start time for the stream, or {@code null} to get data |
71 |
* from the beginning of the |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
72 |
* |
57376 | 73 |
* @param the end end time for the stream, or {@code null} to get data until |
74 |
* the end. |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
75 |
* |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
76 |
* @throws IllegalArgumentException if {@code end} happens before |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
77 |
* {@code start} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
78 |
* |
57376 | 79 |
* @throws IOException if a stream can't be opened,or an I/O error occurs |
80 |
* during reading |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
81 |
*/ |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
82 |
public static EventStream openFile(Path file, Instant from, Instant to) throws IOException { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
83 |
throw new UnsupportedOperationException("Not yet implemented"); |
57376 | 84 |
// return new EventFileStream(file); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
85 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
86 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
87 |
/** |
57361 | 88 |
* Performs an action on all events in the stream. |
89 |
* |
|
90 |
* @param action an action to be performed on each {@code RecordedEvent}, |
|
91 |
* not {@code null} |
|
92 |
*/ |
|
93 |
void onEvent(Consumer<RecordedEvent> action); |
|
94 |
||
95 |
/** |
|
96 |
* Performs an action on all events in the stream with a specified name. |
|
97 |
* |
|
98 |
* @param eventName the name of the event, not {@code null} |
|
99 |
* @param action an action to be performed on each {@code RecordedEvent} |
|
100 |
* that matches the event name, not {@code null} |
|
101 |
*/ |
|
102 |
void onEvent(String eventName, Consumer<RecordedEvent> action); |
|
103 |
||
104 |
/** |
|
105 |
* Performs an action when the event stream has been flushed. |
|
106 |
* |
|
107 |
* @param action an action to be performed after stream has been flushed, |
|
108 |
* not {@code null} |
|
109 |
*/ |
|
110 |
void onFlush(Runnable action); |
|
111 |
||
112 |
/** |
|
113 |
* Performs an action when the event stream is closed. |
|
114 |
* |
|
115 |
* @param action an action to be performed after the stream has been closed, |
|
116 |
* not {@code null} |
|
117 |
*/ |
|
118 |
void onClose(Runnable action); |
|
119 |
||
120 |
/** |
|
121 |
* Releases all resources associated with this event stream. |
|
122 |
*/ |
|
123 |
void close(); |
|
124 |
||
125 |
/** |
|
126 |
* Removes an action from the stream. |
|
127 |
* <p> |
|
128 |
* If the action has been added multiple times, all instance of it will be |
|
129 |
* removed. |
|
130 |
* |
|
131 |
* @param action the action to remove, not {@code null} |
|
132 |
* @return {@code true} if the action was removed, {@code false} otherwise |
|
133 |
* |
|
134 |
* @see #onClose(Runnable) |
|
135 |
* @see #onFlush(Runnable) |
|
136 |
* @see #onEvent(Consumer) |
|
137 |
* @see #onEvent(String, Consumer) |
|
138 |
*/ |
|
139 |
boolean remove(Object action); |
|
140 |
||
141 |
/** |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
142 |
* Hint that the event object in an {@link #onEvent(Consumer)} action |
57376 | 143 |
* may be reused. |
144 |
* <p> |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
145 |
* If reuse is set to {@code true), a callback should not keep a reference |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
146 |
* to the event object after the callback from {@code onEvent} has returned. |
57376 | 147 |
* <p> |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
148 |
* By default reuse is set to {@code true} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
149 |
* |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
150 |
* @param resuse if event objects can be reused between calls to {@code #onEvent(Consumer)} |
57376 | 151 |
* |
152 |
*/ |
|
153 |
public void setReuse(boolean reuse); |
|
154 |
||
155 |
/** |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
156 |
* Orders events in chronological order aft the end timestamp |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
157 |
* |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
158 |
* TODO: WHAT ABOUT EVENTS THAT OCCUR WAY OUT OF ORDER |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
159 |
* <p> |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
160 |
* By default ordered is set to {@code true} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
161 |
* |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
162 |
* @param ordered if event objects arrive in chronological order to {@code #onEvent(Consumer)} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
163 |
*/ |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
164 |
public void setOrdered(boolean ordered); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
165 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
166 |
/** |
57361 | 167 |
* Starts processing events in the stream. |
168 |
* <p> |
|
169 |
* All actions will performed on this stream will happen in the current |
|
170 |
* thread. |
|
171 |
* |
|
172 |
* @throws IllegalStateException if the stream is already started or if it |
|
173 |
* has been closed |
|
174 |
*/ |
|
175 |
void start(); |
|
176 |
||
177 |
/** |
|
178 |
* Starts processing events in the stream asynchronously. |
|
179 |
* <p> |
|
180 |
* All actions on this stream will be performed in a separate thread. |
|
181 |
* |
|
182 |
* @throws IllegalStateException if the stream is already started or if it |
|
183 |
* has been closed |
|
184 |
*/ |
|
185 |
void startAsync(); |
|
186 |
||
187 |
/** |
|
188 |
* Blocks the current thread until the stream is finished, closed, or it |
|
189 |
* times out. |
|
190 |
* |
|
191 |
* @param timeout the maximum time to wait, not {@code null} |
|
192 |
* |
|
193 |
* @see #start() |
|
194 |
* @see #startAsync() |
|
195 |
*/ |
|
196 |
void awaitTermination(Duration timeout); |
|
197 |
||
198 |
/** |
|
199 |
* Blocks the current thread until the stream is finished or closed. |
|
200 |
* |
|
201 |
* @see #start() |
|
202 |
* @see #startAsync() |
|
203 |
*/ |
|
204 |
void awaitTermination(); |
|
205 |
} |