author | egahlin |
Wed, 31 Jul 2019 14:07:44 +0200 | |
branch | JEP-349-branch |
changeset 57604 | 838f9a7635b6 |
parent 57452 | 6fabe73e5d9a |
child 57627 | b38c7a822244 |
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; |
|
57434 | 30 |
import java.security.AccessController; |
57361 | 31 |
import java.time.Duration; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
32 |
import java.time.Instant; |
57361 | 33 |
import java.util.function.Consumer; |
34 |
||
35 |
/** |
|
36 |
* Represents a stream of event that actions can be performed up on. |
|
37 |
*/ |
|
38 |
public interface EventStream extends AutoCloseable { |
|
39 |
||
40 |
/** |
|
57449 | 41 |
* Creates a stream from a disk repository. |
42 |
* <p> |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57449
diff
changeset
|
43 |
* By default, the stream starts with the next event flushed by Flight Recorder. |
57361 | 44 |
* |
45 |
* @param directory location of the disk repository, not {@code null} |
|
57449 | 46 |
* |
57361 | 47 |
* @return an event stream, not {@code null} |
57449 | 48 |
* |
49 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
|
50 |
* during reading |
|
57361 | 51 |
*/ |
52 |
public static EventStream openRepository(Path directory) throws IOException { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
53 |
return new EventDirectoryStream(AccessController.getContext(), directory, AbstractEventStream.NEXT_EVENT); |
57361 | 54 |
} |
55 |
||
56 |
/** |
|
57449 | 57 |
* Creates an event stream from a file. |
58 |
* <p> |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57449
diff
changeset
|
59 |
* By default, the stream starts with the first event in the file. |
57361 | 60 |
* |
61 |
* @param file location of the file, not {@code null} |
|
57449 | 62 |
* |
57361 | 63 |
* @return an event stream, not {@code null} |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
64 |
* |
57449 | 65 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
57376 | 66 |
* during reading |
57361 | 67 |
*/ |
68 |
public static EventStream openFile(Path file) throws IOException { |
|
57449 | 69 |
return new EventFileStream(file, null, null); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
70 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
71 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
72 |
/** |
57361 | 73 |
* Performs an action on all events in the stream. |
74 |
* |
|
75 |
* @param action an action to be performed on each {@code RecordedEvent}, |
|
76 |
* not {@code null} |
|
77 |
*/ |
|
78 |
void onEvent(Consumer<RecordedEvent> action); |
|
79 |
||
80 |
/** |
|
81 |
* Performs an action on all events in the stream with a specified name. |
|
82 |
* |
|
83 |
* @param eventName the name of the event, not {@code null} |
|
57449 | 84 |
* |
57361 | 85 |
* @param action an action to be performed on each {@code RecordedEvent} |
86 |
* that matches the event name, not {@code null} |
|
87 |
*/ |
|
88 |
void onEvent(String eventName, Consumer<RecordedEvent> action); |
|
89 |
||
90 |
/** |
|
91 |
* Performs an action when the event stream has been flushed. |
|
92 |
* |
|
93 |
* @param action an action to be performed after stream has been flushed, |
|
94 |
* not {@code null} |
|
95 |
*/ |
|
96 |
void onFlush(Runnable action); |
|
97 |
||
98 |
/** |
|
99 |
* Performs an action when the event stream is closed. |
|
57449 | 100 |
* <p> |
101 |
* If the stream is already closed, the action will be executed immediately |
|
102 |
* in the current thread. |
|
57361 | 103 |
* |
104 |
* @param action an action to be performed after the stream has been closed, |
|
105 |
* not {@code null} |
|
106 |
*/ |
|
107 |
void onClose(Runnable action); |
|
108 |
||
109 |
/** |
|
110 |
* Releases all resources associated with this event stream. |
|
111 |
*/ |
|
112 |
void close(); |
|
113 |
||
114 |
/** |
|
115 |
* Removes an action from the stream. |
|
116 |
* <p> |
|
117 |
* If the action has been added multiple times, all instance of it will be |
|
118 |
* removed. |
|
119 |
* |
|
120 |
* @param action the action to remove, not {@code null} |
|
57449 | 121 |
* |
57361 | 122 |
* @return {@code true} if the action was removed, {@code false} otherwise |
123 |
* |
|
124 |
* @see #onClose(Runnable) |
|
125 |
* @see #onFlush(Runnable) |
|
126 |
* @see #onEvent(Consumer) |
|
127 |
* @see #onEvent(String, Consumer) |
|
128 |
*/ |
|
129 |
boolean remove(Object action); |
|
130 |
||
131 |
/** |
|
57449 | 132 |
* Specifies that the event object in an {@link #onEvent(Consumer)} action |
133 |
* is to be reused. |
|
57376 | 134 |
* <p> |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
135 |
* 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
|
136 |
* to the event object after the callback from {@code onEvent} has returned. |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
137 |
* |
57449 | 138 |
* @param resuse if event objects can be reused between calls to |
139 |
* {@code #onEvent(Consumer)} |
|
57376 | 140 |
* |
141 |
*/ |
|
142 |
public void setReuse(boolean reuse); |
|
143 |
||
144 |
/** |
|
57449 | 145 |
* Specifies that events arrives in chronological order, sorted by the time |
146 |
* they were committed to the event stream. |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
147 |
* |
57449 | 148 |
* @param ordered if event objects arrive in chronological order to |
149 |
* {@code #onEvent(Consumer)} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
150 |
*/ |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
151 |
public void setOrdered(boolean ordered); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
152 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
153 |
/** |
57449 | 154 |
* Specifies start time of the event stream. |
155 |
* |
|
156 |
* @param startTime the start time, not {@code null} |
|
157 |
* |
|
158 |
* @throws IllegalStateException if the stream has already been started |
|
159 |
*/ |
|
160 |
public void setStartTime(Instant startTime); |
|
161 |
||
162 |
/** |
|
163 |
* Start processing events in the stream. |
|
57361 | 164 |
* <p> |
57449 | 165 |
* All actions performed on this stream will happen in the current thread. |
57361 | 166 |
* |
167 |
* @throws IllegalStateException if the stream is already started or if it |
|
168 |
* has been closed |
|
169 |
*/ |
|
170 |
void start(); |
|
171 |
||
172 |
/** |
|
57449 | 173 |
* Start processing events in the stream asynchronously. |
57361 | 174 |
* <p> |
175 |
* All actions on this stream will be performed in a separate thread. |
|
176 |
* |
|
57449 | 177 |
* @throws IllegalStateException if the stream is already started, or if it |
57361 | 178 |
* has been closed |
179 |
*/ |
|
180 |
void startAsync(); |
|
181 |
||
182 |
/** |
|
183 |
* Blocks the current thread until the stream is finished, closed, or it |
|
184 |
* times out. |
|
185 |
* |
|
186 |
* @param timeout the maximum time to wait, not {@code null} |
|
187 |
* |
|
188 |
* @see #start() |
|
189 |
* @see #startAsync() |
|
190 |
*/ |
|
191 |
void awaitTermination(Duration timeout); |
|
192 |
||
193 |
/** |
|
194 |
* Blocks the current thread until the stream is finished or closed. |
|
195 |
* |
|
196 |
* @see #start() |
|
197 |
* @see #startAsync() |
|
198 |
*/ |
|
199 |
void awaitTermination(); |
|
200 |
} |