View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.service.executor;
    21
    22  import java.util.concurrent.BlockingQueue;
    23  import java.util.concurrent.LinkedBlockingQueue;
    24
    25  import org.apache.mina.api.IoHandler;
    26  import org.slf4j.Logger;
    27  import org.slf4j.LoggerFactory;
    28
    29  /**
    30   * Use this executor if you want the {@link IoHandler} events of a session to be executed in order and on the same
    31   * thread. In your {@link IoHandler} code you don't need to care about session level concurrency.
    32   *
    33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    34   */
    35  public final class OrderedHandlerExecutor implements IoHandlerExecutor {
    36
    37      private static final Logger LOG = LoggerFactory.getLogger(OrderedHandlerExecutor.class);
    38
    39      private Worker[] workers;
    40
    41      /**
    42       * Create an {@link OrderedHandlerExecutor} with a given number of thread and a given queue size.
    43       *
    44       * @param workerThreadCount the worker thread count
    45       * @param queueSize the size of the queue for each worker thread
    46       */
    47      public OrderedHandlerExecutor(int workerThreadCount, int queueSize) {
    48          LOG.debug("creating OrderedHandlerExecutor workerThreadCount = {} queueSize = {}", workerThreadCount, queueSize);
    49          workers = new Worker[workerThreadCount];
    50
    51          for (int i = 0; i < workerThreadCount; i++) {
    52              workers[i] = new Worker(i, queueSize);
    53          }
    54          for (int i = 0; i < workerThreadCount; i++) {
    55              workers[i].start();
    56          }
    57          LOG.debug("workers started");
    58      }
    59
    60      /**
    61       * {@inheritDoc}
    62       */
    63      @Override
    64      public void execute(Event event) {
    65          try {
    66              int workerIndex = (int) (event.getSession().getId() % workers.length);
    67              LOG.debug("executing event {} in worker {}", event, workerIndex);
    68              workers[workerIndex].enqueue(event);
    69          } catch (InterruptedException e) {
    70              // interrupt the world
    71              return;
    72          }
    73      }
    74
    75      /** thread in charge of gathering events from a queue and running them */
    76      private static class Worker extends Thread {
    77
    78          private static HandlerCaller caller = new HandlerCaller();
    79
    80          private final BlockingQueue<Event> queue;
    81
    82          public Worker(int index, int queueSize) {
    83              super("IoHandlerWorker " + index);
    84              queue = new LinkedBlockingQueue<Event>(queueSize);
    85          }
    86
    87          public void enqueue(Event event) throws InterruptedException {
    88              LOG.debug("enqueing event : {}", event);
    89              queue.put(event);
    90          }
    91
    92          /**
    93           * {@inheritDoc}
    94           */
    95          @Override
    96          public void run() {
    97              for (;;) {
    98                  try {
    99
    100                     Event e = queue.take();
    101                     LOG.debug("dequeing event {}", e);
    102                     e.visit(caller);
    103
    104                 } catch (InterruptedException e) {
    105                     // end this thread
    106                     return;
    107                 }
    108             }
    109         }
    110     }
    111 }