FileDocCategorySizeDatePackage
SelectorFactory.javaAPI DocExample4266Tue May 29 16:57:14 BST 2007com.sun.xml.ws.transport.tcp.util

SelectorFactory

public final class SelectorFactory extends Object
Class was copied from GlassFish Grizzly sources to be available also for client side and don't require GlassFish to be installed Factory used to dispatch/share Selector.
author
Scott Oaks
author
Jean-Francois Arcand

Fields Summary
static long
timeout
The timeout before we exit.
static int
maxSelectors
The number of Selector to create.
private static final Stack
selectors
Cache of Selector
Constructors Summary
Methods Summary
public static java.nio.channels.SelectorgetSelector()
Get a exclusive Selector

    
    
            
     
        try{
            for (int i = 0; i < maxSelectors; i++) 
                selectors.add(Selector.open());
        } catch (IOException ex){
        }
    
        synchronized(selectors) {
            Selector s = null;
            try {
                if ( selectors.size() != 0 )
                    s = selectors.pop();
            } catch (EmptyStackException ex){}
                       
            int attempts = 0;
            try{
                while (s == null && attempts < 2) {
                    selectors.wait(timeout);
                    try {
                        if ( selectors.size() != 0 )
                            s = selectors.pop();
                    } catch (EmptyStackException ex){
                        break;
                    }
                    attempts++;
                }
            } catch (InterruptedException ex){};
            return s;
        }
    
public static voidreturnSelector(java.nio.channels.Selector s)
Return the Selector to the cache

        synchronized(selectors) {
            selectors.push(s);
            if (selectors.size() == 1)
                selectors.notify();
        }