src/jdk.jfr/share/classes/jdk/jfr/consumer/EventStream.java
branchJEP-349-branch
changeset 57449 099789ceff7d
parent 57434 216bf2e3b542
child 57452 6fabe73e5d9a
equal deleted inserted replaced
57434:216bf2e3b542 57449:099789ceff7d
    36  * Represents a stream of event that actions can be performed up on.
    36  * Represents a stream of event that actions can be performed up on.
    37  */
    37  */
    38 public interface EventStream extends AutoCloseable {
    38 public interface EventStream extends AutoCloseable {
    39 
    39 
    40     /**
    40     /**
    41      * Creates a stream starting from the next written event in a disk
    41      * Creates a stream from a disk repository.
    42      * repository.
    42      * <p>
       
    43      * By default, the stream will start with the next event that is flushed by
       
    44      * Flight Recorder.
    43      *
    45      *
    44      * @param directory location of the disk repository, not {@code null}
    46      * @param directory location of the disk repository, not {@code null}
       
    47      *
    45      * @return an event stream, not {@code null}
    48      * @return an event stream, not {@code null}
       
    49      *
       
    50      * @throws IOException if a stream can't be opened, or an I/O error occurs
       
    51      *         during reading
    46      */
    52      */
    47     public static EventStream openRepository(Path directory) throws IOException {
    53     public static EventStream openRepository(Path directory) throws IOException {
    48         return new EventDirectoryStream(AccessController.getContext(), directory);
    54         return new EventDirectoryStream(AccessController.getContext(), directory, EventConsumer.NEXT_EVENT);
    49     }
    55     }
    50 
    56 
    51     /**
    57     /**
    52      * Creates an event stream starting from the first event in a file.
    58      * Creates an event stream from a file.
       
    59      * <p>
       
    60      * By default, the stream will start with the first event in the file.
    53      *
    61      *
    54      * @param file location of the file, not {@code null}
    62      * @param file location of the file, not {@code null}
       
    63      *
    55      * @return an event stream, not {@code null}
    64      * @return an event stream, not {@code null}
    56      *
    65      *
    57      * @throws IOException if a stream can't be opened,or an I/O error occurs
    66      * @throws IOException if a stream can't be opened, or an I/O error occurs
    58      *         during reading
    67      *         during reading
    59      */
    68      */
    60     public static EventStream openFile(Path file) throws IOException {
    69     public static EventStream openFile(Path file) throws IOException {
    61         return new EventFileStream(file);
    70         return new EventFileStream(file, null, null);
    62     }
       
    63 
       
    64     /**
       
    65      * Creates an event stream starting start time and end time in a file.
       
    66      *
       
    67      * @param file location of the file, not {@code null}
       
    68      *
       
    69      * @param the start start time for the stream, or {@code null} to get data
       
    70      *        from the beginning of the
       
    71      *
       
    72      * @param the end end time for the stream, or {@code null} to get data until
       
    73      *        the end.
       
    74      *
       
    75      * @throws IllegalArgumentException if {@code end} happens before
       
    76      *         {@code start}
       
    77      *
       
    78      * @throws IOException if a stream can't be opened,or an I/O error occurs
       
    79      *         during reading
       
    80      */
       
    81     public static EventStream openFile(Path file, Instant from, Instant to) throws IOException {
       
    82         throw new UnsupportedOperationException("Not yet implemented");
       
    83         // return new EventFileStream(file);
       
    84     }
    71     }
    85 
    72 
    86     /**
    73     /**
    87      * Performs an action on all events in the stream.
    74      * Performs an action on all events in the stream.
    88      *
    75      *
    93 
    80 
    94     /**
    81     /**
    95      * Performs an action on all events in the stream with a specified name.
    82      * Performs an action on all events in the stream with a specified name.
    96      *
    83      *
    97      * @param eventName the name of the event, not {@code null}
    84      * @param eventName the name of the event, not {@code null}
       
    85      *
    98      * @param action an action to be performed on each {@code RecordedEvent}
    86      * @param action an action to be performed on each {@code RecordedEvent}
    99      *        that matches the event name, not {@code null}
    87      *        that matches the event name, not {@code null}
   100      */
    88      */
   101     void onEvent(String eventName, Consumer<RecordedEvent> action);
    89     void onEvent(String eventName, Consumer<RecordedEvent> action);
   102 
    90 
   108      */
    96      */
   109     void onFlush(Runnable action);
    97     void onFlush(Runnable action);
   110 
    98 
   111     /**
    99     /**
   112      * Performs an action when the event stream is closed.
   100      * Performs an action when the event stream is closed.
       
   101      * <p>
       
   102      * If the stream is already closed, the action will be executed immediately
       
   103      * in the current thread.
   113      *
   104      *
   114      * @param action an action to be performed after the stream has been closed,
   105      * @param action an action to be performed after the stream has been closed,
   115      *        not {@code null}
   106      *        not {@code null}
   116      */
   107      */
   117     void onClose(Runnable action);
   108     void onClose(Runnable action);
   126      * <p>
   117      * <p>
   127      * If the action has been added multiple times, all instance of it will be
   118      * If the action has been added multiple times, all instance of it will be
   128      * removed.
   119      * removed.
   129      *
   120      *
   130      * @param action the action to remove, not {@code null}
   121      * @param action the action to remove, not {@code null}
       
   122      *
   131      * @return {@code true} if the action was removed, {@code false} otherwise
   123      * @return {@code true} if the action was removed, {@code false} otherwise
   132      *
   124      *
   133      * @see #onClose(Runnable)
   125      * @see #onClose(Runnable)
   134      * @see #onFlush(Runnable)
   126      * @see #onFlush(Runnable)
   135      * @see #onEvent(Consumer)
   127      * @see #onEvent(Consumer)
   136      * @see #onEvent(String, Consumer)
   128      * @see #onEvent(String, Consumer)
   137      */
   129      */
   138     boolean remove(Object action);
   130     boolean remove(Object action);
   139 
   131 
   140     /**
   132     /**
   141      * Hint that the event object in an {@link #onEvent(Consumer)} action
   133      * Specifies that the event object in an {@link #onEvent(Consumer)} action
   142      * may be reused.
   134      * is to be reused.
   143      * <p>
   135      * <p>
   144      * If reuse is set to {@code true), a callback should not keep a reference
   136      * If reuse is set to {@code true), a callback should not keep a reference
   145      * to the event object after the callback from {@code onEvent} has returned.
   137      * to the event object after the callback from {@code onEvent} has returned.
   146      * <p>
   138      *
   147      * By default reuse is set to {@code true}
   139      * @param resuse if event objects can be reused between calls to
   148      *
   140      * {@code #onEvent(Consumer)}
   149      * @param resuse if event objects can be reused between calls to {@code #onEvent(Consumer)}
       
   150      *
   141      *
   151      */
   142      */
   152     public void setReuse(boolean reuse);
   143     public void setReuse(boolean reuse);
   153 
   144 
   154     /**
   145     /**
   155      * Orders events in chronological order aft the end timestamp
   146      * Specifies that events arrives in chronological order, sorted by the time
   156      *
   147      * they were committed to the event stream.
   157      * TODO: WHAT ABOUT EVENTS THAT OCCUR WAY OUT OF ORDER
   148      *
   158      * <p>
   149      * @param ordered if event objects arrive in chronological order to
   159      * By default ordered is set to {@code true}
   150      *        {@code #onEvent(Consumer)}
   160      *
       
   161      * @param ordered if event objects arrive in chronological order to {@code #onEvent(Consumer)}
       
   162      */
   151      */
   163     public void setOrdered(boolean ordered);
   152     public void setOrdered(boolean ordered);
   164 
   153 
   165     /**
   154     /**
   166      * Starts processing events in the stream.
   155      * Specifies start time of the event stream.
   167      * <p>
   156      *
   168      * All actions will performed on this stream will happen in the current
   157      * @param startTime the start time, not {@code null}
   169      * thread.
   158      *
       
   159      * @throws IllegalStateException if the stream has already been started
       
   160      */
       
   161     public void setStartTime(Instant startTime);
       
   162 
       
   163     /**
       
   164      * Start processing events in the stream.
       
   165      * <p>
       
   166      * All actions performed on this stream will happen in the current thread.
   170      *
   167      *
   171      * @throws IllegalStateException if the stream is already started or if it
   168      * @throws IllegalStateException if the stream is already started or if it
   172      *         has been closed
   169      *         has been closed
   173      */
   170      */
   174     void start();
   171     void start();
   175 
   172 
   176     /**
   173     /**
   177      * Starts processing events in the stream asynchronously.
   174      * Start processing events in the stream asynchronously.
   178      * <p>
   175      * <p>
   179      * All actions on this stream will be performed in a separate thread.
   176      * All actions on this stream will be performed in a separate thread.
   180      *
   177      *
   181      * @throws IllegalStateException if the stream is already started or if it
   178      * @throws IllegalStateException if the stream is already started, or if it
   182      *         has been closed
   179      *         has been closed
   183      */
   180      */
   184     void startAsync();
   181     void startAsync();
   185 
   182 
   186     /**
   183     /**