summaryrefslogtreecommitdiff
path: root/java/common/src/main/java/org/apache/qpid/pool/ReadWriteThreadModel.java
blob: 8cea70e597d78c108bd9aa47592e59c401522516 (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.qpid.pool;

import org.apache.mina.common.IoFilterChain;
import org.apache.mina.common.ThreadModel;
import org.apache.mina.filter.ReferenceCountingIoFilter;

/**
 * ReadWriteThreadModel is a Mina i/o filter chain factory, which creates a filter chain with seperate filters to
 * handle read and write events. The seperate filters are {@link PoolingFilter}s, which have thread pools to handle
 * these events. The effect of this is that reading and writing may happen concurrently.
 *
 * <p/>Socket i/o will only happen with concurrent reads and writes if Mina has seperate selector threads for each.
 *
 * <p/><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Create a filter chain with seperate read and write thread pools for read/write Mina events.
 *     <td> {@link PoolingFilter}
 * </table>
 */
public class ReadWriteThreadModel implements ThreadModel
{
    /** Holds the singleton instance of this factory. */
    private static final ReadWriteThreadModel _instance = new ReadWriteThreadModel();

    /** Holds the thread pooling filter for reads. */
    private final PoolingFilter _asynchronousReadFilter;

    /** Holds the thread pooloing filter for writes. */
    private final PoolingFilter _asynchronousWriteFilter;

    /**
     * Creates a new factory for concurrent i/o, thread pooling filter chain construction. This is private, so that
     * only a singleton instance of the factory is ever created.
     */
    private ReadWriteThreadModel()
    {
        final ReferenceCountingExecutorService executor = ReferenceCountingExecutorService.getInstance();
        _asynchronousReadFilter = PoolingFilter.createAynschReadPoolingFilter(executor, "AsynchronousReadFilter");
        _asynchronousWriteFilter = PoolingFilter.createAynschWritePoolingFilter(executor, "AsynchronousWriteFilter");
    }

    /**
     * Gets the singleton instance of this filter chain factory.
     *
     * @return The singleton instance of this filter chain factory.
     */
    public static ReadWriteThreadModel getInstance()
    {
        return _instance;
    }

    /**
     * Gets the read filter.
     *
     * @return The read filter.
     */
    public PoolingFilter getAsynchronousReadFilter()
    {
        return _asynchronousReadFilter;
    }

    /**
     * Gets the write filter.
     *
     * @return The write filter.
     */
    public PoolingFilter getAsynchronousWriteFilter()
    {
        return _asynchronousWriteFilter;
    }

    /**
     * Adds the concurrent read and write filters to a filter chain.
     *
     * @param chain The Mina filter chain to add to.
     */
    public void buildFilterChain(IoFilterChain chain)
    {
        chain.addFirst("AsynchronousReadFilter", new ReferenceCountingIoFilter(_asynchronousReadFilter));
        chain.addLast("AsynchronousWriteFilter", new ReferenceCountingIoFilter(_asynchronousWriteFilter));
    }
}