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.filter.executor;
21  
22  import org.apache.mina.core.filterchain.IoFilterAdapter;
23  import org.apache.mina.core.future.IoFutureListener;
24  import org.apache.mina.core.future.WriteFuture;
25  import org.apache.mina.core.service.IoProcessor;
26  import org.apache.mina.core.session.IoEvent;
27  import org.apache.mina.core.session.IoEventType;
28  import org.apache.mina.core.session.IoSession;
29  import org.apache.mina.core.write.WriteRequest;
30  
31  /**
32   * Attaches an {@link IoEventQueueHandler} to an {@link IoSession}'s
33   * {@link WriteRequest} queue to provide accurate write queue status tracking.
34   * <p>
35   * The biggest difference from {@link OrderedThreadPoolExecutor} and
36   * {@link UnorderedThreadPoolExecutor} is that {@link IoEventQueueHandler#polled(Object, IoEvent)}
37   * is invoked when the write operation is completed by an {@link IoProcessor},
38   * consequently providing the accurate tracking of the write request queue
39   * status to the {@link IoEventQueueHandler}.
40   * <p>
41   * Most common usage of this filter could be detecting an {@link IoSession}
42   * which writes too fast which will cause {@link OutOfMemoryError} soon:
43   * <pre>
44   *     session.getFilterChain().addLast(
45   *             "writeThrottle",
46   *             new WriteRequestFilter(new IoEventQueueThrottle()));
47   * </pre>
48   *
49   * <h3>Known issues</h3>
50   *
51   * You can run into a dead lock if you run this filter with the blocking
52   * {@link IoEventQueueHandler} implementation such as {@link IoEventQueueThrottle}
53   * in the {@link IoProcessor} thread.  It's because an {@link IoProcessor}
54   * thread is what processes the {@link WriteRequest}s and notifies related
55   * {@link WriteFuture}s; the {@link IoEventQueueHandler} implementation that
56   * waits for the size of the write request queue to decrease will never wake
57   * up.  To use such an handler, you have to insert an {@link ExecutorFilter}
58   * before this filter or call {@link IoSession#write(Object)} method always
59   * from a different thread.
60   *
61   * @author The Apache MINA Project (dev@mina.apache.org)
62   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
63   */
64  public class WriteRequestFilter extends IoFilterAdapter {
65  
66      private final IoEventQueueHandler queueHandler;
67  
68      /**
69       * Creates a new instance with a new default {@link IoEventQueueThrottle}.
70       */
71      public WriteRequestFilter() {
72          this(new IoEventQueueThrottle());
73      }
74  
75      /**
76       * Creates a new instance with the specified {@link IoEventQueueHandler}.
77       */
78      public WriteRequestFilter(IoEventQueueHandler queueHandler) {
79          if (queueHandler == null) {
80              throw new NullPointerException("queueHandler");
81          }
82          this.queueHandler = queueHandler;
83      }
84  
85      /**
86       * Returns the {@link IoEventQueueHandler} which is attached to this
87       * filter.
88       */
89      public IoEventQueueHandler getQueueHandler() {
90          return queueHandler;
91      }
92  
93      @Override
94      public void filterWrite(
95              NextFilter nextFilter,
96              IoSession session, WriteRequest writeRequest) throws Exception {
97  
98          final IoEvent e = new IoEvent(IoEventType.WRITE, session, writeRequest);
99  
100         if (queueHandler.accept(this, e)) {
101             nextFilter.filterWrite(session, writeRequest);
102             WriteFuture writeFuture = writeRequest.getFuture();
103             if (writeFuture == null) {
104                 return;
105             }
106 
107             // We can track the write request only when it has a future.
108             queueHandler.offered(this, e);
109             writeFuture.addListener(new IoFutureListener<WriteFuture>() {
110                 public void operationComplete(WriteFuture future) {
111                     queueHandler.polled(WriteRequestFilter.this, e);
112                 }
113             });
114         }
115     }
116 }