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