src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/Dispatcher.java
branchJEP-349-branch
changeset 58197 0ef79bd7fb5c
parent 58153 0f7562601338
child 58200 2d147d680311
equal deleted inserted replaced
58193:baf88aa4f5db 58197:0ef79bd7fb5c
    10 import jdk.jfr.internal.LongMap;
    10 import jdk.jfr.internal.LongMap;
    11 import jdk.jfr.internal.consumer.ChunkParser.ParserConfiguration;
    11 import jdk.jfr.internal.consumer.ChunkParser.ParserConfiguration;
    12 
    12 
    13 final class Dispatcher {
    13 final class Dispatcher {
    14 
    14 
    15     public final static class EventDispatcher {
    15     final static class EventDispatcher {
    16         final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0];
    16         private final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0];
    17         final String eventName;
       
    18         public final Consumer<RecordedEvent> action;
       
    19 
    17 
    20         public EventDispatcher(Consumer<RecordedEvent> action) {
    18         private final String eventName;
    21             this(null, action);
    19         private final Consumer<RecordedEvent> action;
    22         }
       
    23 
    20 
    24         public EventDispatcher(String eventName, Consumer<RecordedEvent> action) {
    21         public EventDispatcher(String eventName, Consumer<RecordedEvent> action) {
    25             this.eventName = eventName;
    22             this.eventName = eventName;
    26             this.action = action;
    23             this.action = action;
    27         }
    24         }
    28 
    25 
    29         public void offer(RecordedEvent event) {
    26         private void offer(RecordedEvent event) {
    30             action.accept(event);
    27             action.accept(event);
    31         }
    28         }
    32 
    29 
    33         public boolean accepts(EventType eventType) {
    30         private boolean accepts(EventType eventType) {
    34             return (eventName == null || eventType.getName().equals(eventName));
    31             return (eventName == null || eventType.getName().equals(eventName));
       
    32         }
       
    33 
       
    34         public Consumer<RecordedEvent> getAction() {
       
    35             return action;
    35         }
    36         }
    36     }
    37     }
    37 
    38 
    38     final Consumer<Throwable>[] errorActions;
    39     private final Consumer<Throwable>[] errorActions;
    39     final Runnable[] flushActions;
    40     private final Runnable[] flushActions;
    40     final Runnable[] closeActions;
    41     private final Runnable[] closeActions;
    41     final EventDispatcher[] dispatchers;
    42     private final EventDispatcher[] dispatchers;
    42     final LongMap<EventDispatcher[]> dispatcherLookup = new LongMap<>();
    43     private final LongMap<EventDispatcher[]> dispatcherLookup = new LongMap<>();
    43     final ParserConfiguration parserConfiguration;
    44     final ParserConfiguration parserConfiguration;
    44     final Instant startTime;
    45     final Instant startTime;
    45     final Instant endTime;
    46     final Instant endTime;
    46     final long startNanos;
    47     final long startNanos;
    47     final long endNanos;
    48     final long endNanos;
    61         this.endTime = c.endTime;
    62         this.endTime = c.endTime;
    62         this.startNanos = c.startNanos;
    63         this.startNanos = c.startNanos;
    63         this.endNanos = c.endNanos;
    64         this.endNanos = c.endNanos;
    64     }
    65     }
    65 
    66 
       
    67     public void runFlushActions() {
       
    68         Runnable[] flushActions = this.flushActions;
       
    69         for (int i = 0; i < flushActions.length; i++) {
       
    70             try {
       
    71                 flushActions[i].run();
       
    72             } catch (Exception e) {
       
    73                 handleError(e);
       
    74             }
       
    75         }
       
    76     }
       
    77 
       
    78     public void runCloseActions() {
       
    79         Runnable[] closeActions = this.closeActions;
       
    80         for (int i = 0; i < closeActions.length; i++) {
       
    81             try {
       
    82                 closeActions[i].run();
       
    83             } catch (Exception e) {
       
    84                 handleError(e);
       
    85             }
       
    86         }
       
    87     }
       
    88 
    66     private static ParserFilter buildFilter(EventDispatcher[] dispatchers) {
    89     private static ParserFilter buildFilter(EventDispatcher[] dispatchers) {
    67         ParserFilter ef = new ParserFilter();
    90         ParserFilter ef = new ParserFilter();
    68         for (EventDispatcher ed : dispatchers) {
    91         for (EventDispatcher ed : dispatchers) {
    69             String name = ed.eventName;
    92             String name = ed.eventName;
    70             if (name == null) {
    93             if (name == null) {
    73             ef.setThreshold(name, 0);
    96             ef.setThreshold(name, 0);
    74         }
    97         }
    75         return ef;
    98         return ef;
    76     }
    99     }
    77 
   100 
    78     protected final void dispatch(RecordedEvent event) {
   101     void dispatch(RecordedEvent event) {
    79         EventType type = event.getEventType();
   102         EventType type = event.getEventType();
    80         EventDispatcher[] dispatchers = null;
   103         EventDispatcher[] dispatchers = null;
    81         if (type == cacheEventType) {
   104         if (type == cacheEventType) {
    82             dispatchers = cacheDispatchers;
   105             dispatchers = cacheDispatchers;
    83         } else {
   106         } else {
   119                 handleError(e);
   142                 handleError(e);
   120             }
   143             }
   121         }
   144         }
   122     }
   145     }
   123 
   146 
   124     public void handleError(Throwable e) {
   147     private void handleError(Throwable e) {
   125         Consumer<?>[] consumers = this.errorActions;
   148         Consumer<?>[] consumers = this.errorActions;
   126         if (consumers.length == 0) {
   149         if (consumers.length == 0) {
   127             defaultErrorHandler(e);
   150             defaultErrorHandler(e);
   128             return;
   151             return;
   129         }
   152         }
   132             Consumer<Throwable> conusmer = (Consumer<Throwable>) consumers[i];
   155             Consumer<Throwable> conusmer = (Consumer<Throwable>) consumers[i];
   133             conusmer.accept(e);
   156             conusmer.accept(e);
   134         }
   157         }
   135     }
   158     }
   136 
   159 
   137     public void runFlushActions() {
   160     private void defaultErrorHandler(Throwable e) {
   138         Runnable[] flushActions = this.flushActions;
       
   139         for (int i = 0; i < flushActions.length; i++) {
       
   140             try {
       
   141                 flushActions[i].run();
       
   142             } catch (Exception e) {
       
   143                 handleError(e);
       
   144             }
       
   145         }
       
   146     }
       
   147 
       
   148     public void runCloseActions() {
       
   149         Runnable[] closeActions = this.closeActions;
       
   150         for (int i = 0; i < closeActions.length; i++) {
       
   151             try {
       
   152                 closeActions[i].run();
       
   153             } catch (Exception e) {
       
   154                 handleError(e);
       
   155             }
       
   156         }
       
   157     }
       
   158 
       
   159     void defaultErrorHandler(Throwable e) {
       
   160         e.printStackTrace();
   161         e.printStackTrace();
   161     }
   162     }
   162 }
   163 }