summaryrefslogtreecommitdiffstats
path: root/simple/simple-transport/src/main/java/org/simpleframework/transport/reactor/ActionDistributor.java
blob: 65dc8d2117ec951826305b61880ff056ad4897bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
 * ActionDistributor.java February 2007
 *
 * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */
 
package org.simpleframework.transport.reactor;
 
import static java.nio.channels.SelectionKey.OP_READ;
import static java.nio.channels.SelectionKey.OP_WRITE;
import static org.simpleframework.transport.reactor.ReactorEvent.CHANNEL_CLOSED;
import static org.simpleframework.transport.reactor.ReactorEvent.CLOSE_SELECTOR;
import static org.simpleframework.transport.reactor.ReactorEvent.ERROR;
import static org.simpleframework.transport.reactor.ReactorEvent.EXECUTE_ACTION;
import static org.simpleframework.transport.reactor.ReactorEvent.INVALID_KEY;
import static org.simpleframework.transport.reactor.ReactorEvent.READ_INTEREST_READY;
import static org.simpleframework.transport.reactor.ReactorEvent.REGISTER_INTEREST;
import static org.simpleframework.transport.reactor.ReactorEvent.REGISTER_READ_INTEREST;
import static org.simpleframework.transport.reactor.ReactorEvent.REGISTER_WRITE_INTEREST;
import static org.simpleframework.transport.reactor.ReactorEvent.SELECT;
import static org.simpleframework.transport.reactor.ReactorEvent.SELECT_CANCEL;
import static org.simpleframework.transport.reactor.ReactorEvent.SELECT_EXPIRED;
import static org.simpleframework.transport.reactor.ReactorEvent.UPDATE_INTEREST;
import static org.simpleframework.transport.reactor.ReactorEvent.UPDATE_READ_INTEREST;
import static org.simpleframework.transport.reactor.ReactorEvent.UPDATE_WRITE_INTEREST;
import static org.simpleframework.transport.reactor.ReactorEvent.WRITE_INTEREST_READY;

import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;

import org.simpleframework.common.thread.Daemon;
import org.simpleframework.transport.trace.Trace;
 
 /**
  * The <code>ActionDistributor</code> is used to execute operations
  * that have an interested I/O event ready. This acts much like a
  * scheduler would in that it delays the execution of the operations
  * until such time as the associated <code>SelectableChannel</code>
  * has an interested I/O event ready.
  * <p>
  * This distributor has two modes, one mode is used to cancel the
  * channel once an I/O event has occurred. This means that the channel
  * is removed from the <code>Selector</code> so that the selector 
  * does not break when asked to select again. cancelling the channel
  * is useful when the operation execution may not fully read the 
  * payload or when the operation takes a significant amount of time.
  *
  * @see org.simpleframework.transport.reactor.ExecutorReactor
  */ 
class ActionDistributor extends Daemon implements OperationDistributor {
   
   /**
    * This is used to determine the operations that need cancelling.
    */ 
   private Map<Channel, ActionSet> executing;
   
   /**
    * This is used to keep track of actions currently in selection.
    */
   private Map<Channel, ActionSet> selecting;   
   
   /**
    * This is the queue that is used to invalidate channels.
    */ 
   private Queue<Channel> invalid;       
   
   /**
    * This is the queue that is used to provide the operations.
    */ 
   private Queue<Action> pending;  
   
  /**
   * This is the selector used to select for interested events.
   */ 
   private ActionSelector selector;   
 
   /**
    * This is used to execute the operations that are ready.
    */ 
   private Executor executor;        
   
   /**
    * This is used to signal when the distributor has closed.
    */
   private Latch latch;
   
   /**
    * This is the duration in milliseconds the operation expires in.
    */
   private long expiry;
 
   /**
    * This is time in milliseconds when the next expiry will occur.
    */
   private long update;
 
   /**
    * This is used to determine the mode the distributor uses.
    */ 
   private boolean cancel;
   
   /**
    * Constructor for the <code>ActionDistributor</code> object. This 
    * will create a distributor that distributes operations when those
    * operations show that they are ready for a given I/O event. The
    * interested I/O events are provided as a bitmask taken from the
    * actions of the <code>SelectionKey</code>. Distribution of the
    * operations is passed to the provided executor object.
    *
    * @param executor this is the executor used to execute operations
    */   
   public ActionDistributor(Executor executor) throws IOException {
      this(executor, true);
   } 
 
   /**
    * Constructor for the <code>ActionDistributor</code> object. This 
    * will create a distributor that distributes operations when those
    * operations show that they are ready for a given I/O event. The
    * interested I/O events are provided as a bitmask taken from the
    * actions of the <code>SelectionKey</code>. Distribution of the
    * operations is passed to the provided executor object.
    *
    * @param executor this is the executor used to execute operations
    * @param cancel should the channel be removed from selection
    */   
   public ActionDistributor(Executor executor, boolean cancel) throws IOException {
      this(executor, cancel, 120000);
   }
   
   /**
    * Constructor for the <code>ActionDistributor</code> object. This 
    * will create a distributor that distributes operations when those
    * operations show that they are ready for a given I/O event. The
    * interested I/O events are provided as a bitmask taken from the
    * actions of the <code>SelectionKey</code>. Distribution of the
    * operations is passed to the provided executor object.
    *
    * @param executor this is the executor used to execute operations
    * @param cancel should the channel be removed from selection
    * @param expiry this the maximum idle time for an operation
    */   
   public ActionDistributor(Executor executor, boolean cancel, long expiry) throws IOException {
      this.selecting = new LinkedHashMap<Channel, ActionSet>();
      this.executing = new LinkedHashMap<Channel, ActionSet>();
      this.pending = new ConcurrentLinkedQueue<Action>();
      this.invalid = new ConcurrentLinkedQueue<Channel>();      
      this.selector = new ActionSelector();  
      this.latch = new Latch();
      this.executor = executor;    
      this.cancel = cancel;
      this.expiry = expiry;
      this.start(); 
   }   
   
   /**
    * This is used to process the <code>Operation</code> object. This
    * will wake up the selector if it is currently blocked selecting
    * and register the operations associated channel. Once the 
    * selector is awake it will acquire the operation from the queue
    * and register the associated <code>SelectableChannel</code> for
    * selection. The operation will then be executed when the channel
    * is ready for the interested I/O events.
    * 
    * @param task this is the task that is scheduled for distribution   
    * @param require this is the bit-mask value for interested events
    */ 
   public void process(Operation task, int require) throws IOException {
      Action action = new ExecuteAction(task, require, expiry);
      
      if(!isActive())  {
         throw new IOException("Distributor is closed");
      }
      pending.offer(action);
      selector.wake();
   }
   
   /**
    * This is used to close the distributor such that it cancels all
    * of the registered channels and closes down the selector. This
    * is used when the distributor is no longer required, after the
    * close further attempts to process operations will fail.
    */ 
   public void close() throws IOException {  
      stop();  
      selector.wake();
      latch.close();
   }   
   
   /**
    * This returns the number of channels that are currently selecting
    * with this distributor. When busy this can get quite high, however
    * it must return to zero as soon as all tasks have completed.
    * 
    * @return return the number of channels currently selecting
    */
   public int size() {
      return selecting.size();
   }
   
   /**
    * Performs the execution of the distributor. Each distributor runs 
    * on an asynchronous thread to the <code>Reactor</code> which is
    * used to perform the selection on a set of channels. Each  time 
    * there is a new operation to be processed this will take the
    * operation from the ready queue, cancel all outstanding channels,
    * and register the operations associated channel for selection.   
    */ 
   public void run() {
      try {
         execute();
      } finally {
         purge();
      }
   }
   
   /**
    * Performs the execution of the distributor. Each distributor runs 
    * on an asynchronous thread to the <code>Reactor</code> which is
    * used to perform the selection on a set of channels. Each  time 
    * there is a new operation to be processed this will take the
    * operation from the ready queue, cancel all outstanding channels,
    * and register the operations associated channel for selection.   
    */ 
   private void execute() {
      while(isActive()) {
         try {
            register();
            cancel(); 
            expire();
            distribute(); 
            validate();
         } catch(Exception cause) {
            report(cause);           
         }            
      }    
   }
   
   /**
    * This will purge all the actions from the distributor when the
    * distributor ends. If there are any threads waiting on the close
    * to finish they are signalled when all operations are purged.
    * This will allow them to return ensuring no operations linger.
    */
   private void purge() {
      try {
         register();
         cancel();
         clear();
      } catch(Exception cause) {
         report(cause);
      }
   }
   
   /**
    * This method is called to ensure that if there is a global 
    * error that each action will know about it. Such an issue could
    * be file handle exhaustion or an out of memory error. It is
    * also possible that a poorly behaving action could cause an
    * issue which should be know the the entire system.
    * 
    * @param cause this is the exception to report
    */
   private void report(Exception cause) {
      Set<Channel> channels = selecting.keySet();
      
      for(Channel channel : channels) {
         ActionSet set = selecting.get(channel);
         Action[] list = set.list();
            
         for(Action action : list) {         
            Operation operation = action.getOperation();
            Trace trace = operation.getTrace();
            
            try {
               trace.trace(ERROR, cause);
            } catch(Exception e) {
               invalid.offer(channel);
            }
         }
      }
      invalid.clear();
   }   
   
   /**   
    * Here we perform an expire which will take all of the registered
    * sockets and expire it. This ensures that the operations can be
    * executed within the executor and the cancellation of the sockets
    * can be performed. Once this method has finished then all of 
    * the operations will have been scheduled for execution.
    */
   private void clear() throws IOException {
      List<ActionSet> sets = selector.registeredSets();
      
      for(ActionSet set : sets) {
         Action[] list = set.list();
         
         for(Action action : list) {
            Operation task = action.getOperation();
            Trace trace = task.getTrace();
            
            try {
               trace.trace(CLOSE_SELECTOR);         
               expire(set, Long.MAX_VALUE);   
            } catch(Exception cause) {
               trace.trace(ERROR, cause);
            } 
         }
      }
      selector.close();
      latch.signal();
   }
   
   /**
    * This method is used to expire registered operations that remain
    * idle within the selector. Operations specify a time at which 
    * point they wish to be cancelled if the I/O event they wait on
    * has not arisen. This will enables the cancelled operation to be
    * cancelled so that the resources it occupies can be released. 
    */
   private void expire() throws IOException {
      List<ActionSet> sets = selector.registeredSets();
      
      if(cancel) {
         long time = System.currentTimeMillis();
         
         if(update <= time) {
            for(ActionSet set : sets) {          
               expire(set, time);           
            }
            update = time +10000;
         }
      }
   }
   
   /**
    * This method is used to expire registered operations that remain
    * idle within the selector. Operations specify a time at which 
    * point they wish to be  if the I/O event they wait on
    * has not arisen. This will enables the cancelled operation to be
    * cancelled so that the resources it occupies can be released.
    * 
    * @param set this is the selection set check for expired actions
    * @param time this is the time to check the expiry against
    */
   private void expire(ActionSet set, long time) throws IOException {
      Action[] actions = set.list();
      SelectionKey key = set.key();
      
      if(key.isValid()) {
         int mask = key.interestOps();
         
         for(Action action : actions) {
            int interest = action.getInterest();
            long expiry = action.getExpiry();
            
            if(expiry < time) {
               expire(set, action);         
               mask &= ~interest;
            }
         }
         update(set, mask);
      }
   }
   
   /**
    * This is used to update the interested operations of a set of
    * actions. If there are no interested operations the set will be
    * cancelled, otherwise the selection key will be updated with the
    * new operations provided by the bitmask.
    * 
    * @param set this is the action set that is to be updated
    * @param interest this is the bitmask containing the operations
    */
   private void update(ActionSet set, int interest) throws IOException {
      SelectionKey key = set.key();
      
      if(interest == 0) {
         Channel channel = key.channel();
         
         selecting.remove(channel);
         key.cancel();
      } else {
         key.interestOps(interest);
      }
   }
   
   /**
    * This method is used to expire registered operations that remain
    * idle within the selector. Operations specify a time at which 
    * point they wish to be cancelled if the I/O event they wait on
    * has not arisen. This will enables the cancelled operation to be
    * cancelled so that the resources it occupies can be released. 
    * 
    * @param set this is the action set containing the actions
    * @param action this is the actual action to be cancelled        
    */
   private void expire(ActionSet set, Action action) throws IOException {
      Action cancel = new CancelAction(action);
      
      if(set != null) {
         Operation task = action.getOperation();
         Trace trace = task.getTrace();
         int interest = action.getInterest();
         
         try {
            trace.trace(SELECT_EXPIRED, interest);         
            set.remove(interest);
            execute(cancel);
         } catch(Exception cause) {
            trace.trace(ERROR, cause);
         }
      }
   }
   
   /**
    * This method is used to perform simple validation. It ensures 
    * that directly after the processing loop any channels that
    * are registered that have been cancelled or are closed will
    * be removed from the selecting map and rejected.
    */
   private void validate() throws IOException {
      Set<Channel> channels = selecting.keySet();
      
      for(Channel channel : channels) {
         ActionSet set = selecting.get(channel);
         SelectionKey key = set.key();
         
         if(!key.isValid()) {
            invalid.offer(channel);
         }
      }
      for(Channel channel : invalid) {
         invalidate(channel);
      }
      invalid.clear();
   }
   
   /**
    * This method is used to remove the channel from the selecting
    * registry. It is rare that this will every happen, however it
    * is important that tasks are cleared out in this manner as it
    * could lead to a memory leak if left for a long time.
    * 
    * @param channel this is the channel being validated
    */
   private void invalidate(Channel channel) throws IOException {
      ActionSet set = selecting.remove(channel);
      Action[] list = set.list();
         
      for(Action action : list) {
         Operation task = action.getOperation();
         Trace trace = task.getTrace();
               
         try {
            trace.trace(INVALID_KEY);               
            execute(action); // reject
         } catch(Exception cause) {            
            trace.trace(ERROR, cause);            
         }
      }      
   }
 
   /**
    * This is used to cancel any selection keys that have previously
    * been selected with an interested I/O event. Performing a cancel
    * here ensures that on a the next select the associated channel
    * is not considered, this ensures the select does not break.
    */ 
   private void cancel() throws IOException {    
      Collection<ActionSet> list = executing.values();
         
      for(ActionSet set : list) {
         Action[] actions = set.list();
         
         for(Action action : actions) {
            Operation task = action.getOperation();
            Trace trace = task.getTrace();
            
            trace.trace(SELECT_CANCEL);
         }
         set.cancel();
         set.clear();
      }     
      executing.clear();
   }
 
   /**
    * Here all the enqueued <code>Operation</code> objects will be 
    * registered for selection. Each operations channel is used for
    * selection on the interested I/O events. Once the I/O event
    * occurs for the channel the operation is scheduled for execution.   
    */ 
   private void register() throws IOException {
      while(!pending.isEmpty()) {
         Action action = pending.poll();
         
         if(action != null) {
            SelectableChannel channel = action.getChannel();    
            ActionSet set = executing.remove(channel);
            
            if(set == null) {
               set = selecting.get(channel); 
            }  
            if(set != null) {
               update(action, set);
            } else {
               register(action);
            }
         }
      }
   }

   /**
    * Here the specified <code>Operation</code> object is registered
    * with the selector. If the associated channel had previously 
    * been cancelled it is removed from the cancel map to ensure it
    * is not removed from the selector when cancellation is done.
    *
    * @param action this is the operation that is to be registered   
    */
   private void register(Action action) throws IOException {
      SelectableChannel channel = action.getChannel();
      Operation task = action.getOperation();
      Trace trace = task.getTrace();
      
      try {
         if(channel.isOpen()) {
            trace.trace(SELECT);              
            select(action);               
         } else {
            trace.trace(CHANNEL_CLOSED);            
            selecting.remove(channel);
            execute(action); // reject
         }
      }catch(Exception cause) {
         trace.trace(ERROR, cause);
      }
   }   
   
   /**
    * Here the specified <code>Operation</code> object is registered
    * with the selector. If the associated channel had previously 
    * been cancelled it is removed from the cancel map to ensure it
    * is not removed from the selector when cancellation is done.
    *
    * @param action this is the operation that is to be registered
    * @param set this is the action set to register the action with     
    */
   private void update(Action action, ActionSet set) throws IOException {
      Operation task = action.getOperation();
      Trace trace = task.getTrace();
      SelectionKey key = set.key();
      int interest = action.getInterest();
      int current = key.interestOps();
      int updated = current | interest;
     
      try {
         if(OP_READ == (interest & OP_READ)) {
            trace.trace(UPDATE_READ_INTEREST);
         } 
         if(OP_WRITE == (interest & OP_WRITE)) {
            trace.trace(UPDATE_WRITE_INTEREST);
         }       
         trace.trace(UPDATE_INTEREST, updated);
         key.interestOps(updated);
         set.attach(action);
      } catch(Exception cause) {      
         trace.trace(ERROR, cause);      
      }
   }
   
   /**
    * This method is used to perform an actual select on a channel. It
    * will register the channel with the internal selector using the
    * required I/O event bit mask. In order to ensure that selection 
    * is performed correctly the provided channel must be connected.
    * 
    * @param action this is the operation that is to be registered 
    * 
    * @return this returns the selection key used for selection
    */
   private void select(Action action) throws IOException {
      SelectableChannel channel = action.getChannel();
      Operation task = action.getOperation();
      Trace trace = task.getTrace();
      int interest = action.getInterest();
      
      if(interest > 0) {
         ActionSet set = selector.register(channel, interest);  
         
         if(OP_READ == (interest & OP_READ)) {
            trace.trace(REGISTER_READ_INTEREST);
         } 
         if(OP_WRITE == (interest & OP_WRITE)) {
            trace.trace(REGISTER_WRITE_INTEREST);
         }
         trace.trace(REGISTER_INTEREST, interest);         
         set.attach(action);
         selecting.put(channel, set);         
      }
   }
 
   /**
    * This method is used to perform the select and if required queue
    * the operations that are ready for execution. If the selector 
    * is woken up without any ready channels then this will return
    * quietly. If however there are a number of channels ready to be
    * processed then they are handed to the executor object and 
    * marked as ready for cancellation.
    */ 
   private void distribute() throws IOException {      
      if(selector.select(5000) > 0) {
         if(isActive()) {           
            process();            
         }
      }  
   }  
 
   /**
    * This will iterate over the set of selection keys and process each
    * of them. The <code>Operation</code> associated with the selection
    * key is handed to the executor to perform the channel operation.
    * Also, if configured to cancel, this method will add the channel
    * and the associated selection key to the cancellation map.
    */ 
   private void process() throws IOException{  
      List<ActionSet> ready = selector.selectedSets();
      
      for(ActionSet set : ready) {
         process(set);
         remove(set);
      }
   }
 
   /**
    * This will use the specified action set to acquire the channel
    * and <code>Operation</code> associated with it to hand to the
    * executor to perform the channel operation.
    *
    * @param set this is the set of actions that are to be processed
    */ 
   private void process(ActionSet set) throws IOException {
      Action[] actions = set.ready();
      
      for(Action action : actions) {
         Operation task = action.getOperation();
         Trace trace = task.getTrace();
         int interest = action.getInterest();
         
         try {
            if(OP_READ == (interest & OP_READ)) {
               trace.trace(READ_INTEREST_READY, interest);
            } 
            if(OP_WRITE == (interest & OP_WRITE)) {
               trace.trace(WRITE_INTEREST_READY, interest);
            } 
            execute(action);
         } catch(Exception cause) {
            trace.trace(ERROR, cause);
         }
      } 
   }
   
   /**
    * This method ensures that references to the actions and channel
    * are cleared from this instance. To ensure there are no memory
    * leaks it is important to clear out all actions and channels.
    * Also, if configured to cancel executing actions this will
    * register the channel and actions to cancel on the next loop.
    *
    * @param set this is the set of actions that are to be removed
    */ 
   private void remove(ActionSet set) throws IOException {
      Channel channel = set.channel();
      SelectionKey key = set.key();
      
      if(key.isValid()) {
         int interest = set.interest();
         int ready = key.readyOps();
    
         if(cancel) {
            int remaining = interest & ~ready; 
   
            if(remaining == 0) {               
               executing.put(channel, set); 
            } else {       
               key.interestOps(remaining);       
            }
            set.remove(ready);   
         }
      } else {
         selecting.remove(channel);
      }
   }
   
   /**
    * This is where the action is handed off to the executor. Before
    * the action is executed a trace event is generated, this will
    * ensure that the entry and exit points can be tracked. It is
    * also useful in debugging performance issues and memory leaks.
    * 
    * @param action this is the action to execute
    */
   private void execute(Action action) {
      Operation task = action.getOperation();
      Trace trace = task.getTrace();
      int interest = action.getInterest();
      
      try {
         trace.trace(EXECUTE_ACTION, interest);
         executor.execute(action);
      } catch(Exception cause) {
         trace.trace(ERROR, cause);
      }
   }
}