ReadWriteThreadModel.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.pool;
022 
023 import org.apache.mina.common.IoFilterChain;
024 import org.apache.mina.common.ThreadModel;
025 import org.apache.mina.filter.ReferenceCountingIoFilter;
026 
027 /**
028  * ReadWriteThreadModel is a Mina i/o filter chain factory, which creates a filter chain with seperate filters to
029  * handle read and write events. The seperate filters are {@link PoolingFilter}s, which have thread pools to handle
030  * these events. The effect of this is that reading and writing may happen concurrently.
031  *
032  <p/>Socket i/o will only happen with concurrent reads and writes if Mina has seperate selector threads for each.
033  *
034  <p/><table id="crc"><caption>CRC Card</caption>
035  <tr><th> Responsibilities <th> Collaborations
036  <tr><td> Create a filter chain with seperate read and write thread pools for read/write Mina events.
037  *     <td> {@link PoolingFilter}
038  </table>
039  */
040 public class ReadWriteThreadModel implements ThreadModel
041 {
042     /** Holds the singleton instance of this factory. */
043     private static final ReadWriteThreadModel _instance = new ReadWriteThreadModel();
044 
045     /** Holds the thread pooling filter for reads. */
046     private final PoolingFilter _asynchronousReadFilter;
047 
048     /** Holds the thread pooloing filter for writes. */
049     private final PoolingFilter _asynchronousWriteFilter;
050 
051     /**
052      * Creates a new factory for concurrent i/o, thread pooling filter chain construction. This is private, so that
053      * only a singleton instance of the factory is ever created.
054      */
055     private ReadWriteThreadModel()
056     {
057         final ReferenceCountingExecutorService executor = ReferenceCountingExecutorService.getInstance();
058         _asynchronousReadFilter = PoolingFilter.createAynschReadPoolingFilter(executor, "AsynchronousReadFilter");
059         _asynchronousWriteFilter = PoolingFilter.createAynschWritePoolingFilter(executor, "AsynchronousWriteFilter");
060     }
061 
062     /**
063      * Gets the singleton instance of this filter chain factory.
064      *
065      @return The singleton instance of this filter chain factory.
066      */
067     public static ReadWriteThreadModel getInstance()
068     {
069         return _instance;
070     }
071 
072     /**
073      * Gets the read filter.
074      *
075      @return The read filter.
076      */
077     public PoolingFilter getAsynchronousReadFilter()
078     {
079         return _asynchronousReadFilter;
080     }
081 
082     /**
083      * Gets the write filter.
084      *
085      @return The write filter.
086      */
087     public PoolingFilter getAsynchronousWriteFilter()
088     {
089         return _asynchronousWriteFilter;
090     }
091 
092     /**
093      * Adds the concurrent read and write filters to a filter chain.
094      *
095      @param chain The Mina filter chain to add to.
096      */
097     public void buildFilterChain(IoFilterChain chain)
098     {
099         chain.addFirst("AsynchronousReadFilter"new ReferenceCountingIoFilter(_asynchronousReadFilter));
100         chain.addLast("AsynchronousWriteFilter"new ReferenceCountingIoFilter(_asynchronousWriteFilter));
101     }
102 }