KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > nio > SelectorManager


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.nio;
19
20 import java.io.IOException JavaDoc;
21 import java.nio.channels.SocketChannel JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.concurrent.Executor JavaDoc;
24 import java.util.concurrent.Executors JavaDoc;
25 import java.util.concurrent.ThreadFactory JavaDoc;
26
27 /**
28  * The SelectorManager will manage one Selector and the thread that checks the
29  * selector.
30  *
31  * We may need to consider running more than one thread to check the selector if
32  * servicing the selector takes too long.
33  *
34  * @version $Rev: 46019 $ $Date: 2004-09-14 05:56:06 -0400 (Tue, 14 Sep 2004) $
35  */

36 final public class SelectorManager {
37
38     static final public SelectorManager singleton = new SelectorManager();
39     static SelectorManager getInstance() {
40         return singleton;
41     }
42     
43     public interface Listener {
44         public void onSelect(SelectorSelection selector);
45         public void onError(SelectorSelection selection, Throwable JavaDoc error);
46     }
47     
48     private Executor JavaDoc selectorExecutor = Executors.newCachedThreadPool(new ThreadFactory JavaDoc(){
49         public Thread JavaDoc newThread(Runnable JavaDoc r) {
50             Thread JavaDoc rc = new Thread JavaDoc(r);
51             rc.setName("NIO Transport Thread");
52             return rc;
53         }});
54     private Executor JavaDoc channelExecutor = selectorExecutor;
55     private LinkedList JavaDoc<SelectorWorker> freeWorkers = new LinkedList JavaDoc<SelectorWorker>();
56     private int maxChannelsPerWorker = 64;
57     
58     public synchronized SelectorSelection register(SocketChannel JavaDoc socketChannel, Listener listener)
59         throws IOException JavaDoc {
60
61         SelectorWorker worker = null;
62         if (freeWorkers.size() > 0) {
63             worker = freeWorkers.getFirst();
64         } else {
65             worker = new SelectorWorker(this);
66             freeWorkers.addFirst(worker);
67         }
68
69         SelectorSelection selection = new SelectorSelection(worker, socketChannel, listener);
70         return selection;
71     }
72
73     synchronized void onWorkerFullEvent(SelectorWorker worker) {
74         freeWorkers.remove(worker);
75     }
76
77     synchronized public void onWorkerEmptyEvent(SelectorWorker worker) {
78         freeWorkers.remove(worker);
79     }
80
81     synchronized public void onWorkerNotFullEvent(SelectorWorker worker) {
82         freeWorkers.add(worker);
83     }
84
85     public Executor JavaDoc getChannelExecutor() {
86         return channelExecutor;
87     }
88
89     public void setChannelExecutor(Executor JavaDoc channelExecutor) {
90         this.channelExecutor = channelExecutor;
91     }
92
93     public int getMaxChannelsPerWorker() {
94         return maxChannelsPerWorker;
95     }
96
97     public void setMaxChannelsPerWorker(int maxChannelsPerWorker) {
98         this.maxChannelsPerWorker = maxChannelsPerWorker;
99     }
100
101     public Executor JavaDoc getSelectorExecutor() {
102         return selectorExecutor;
103     }
104
105     public void setSelectorExecutor(Executor JavaDoc selectorExecutor) {
106         this.selectorExecutor = selectorExecutor;
107     }
108
109 }
110
Popular Tags