Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   * 
   * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   * 
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * 
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  * 
  * Contributor(s):
  * 
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 package com.sun.enterprise.web.connector.grizzly;
 
Factory used to dispatch/share java.nio.channels.Selector.

Author(s):
Scott Oaks
Jean-Francois Arcand
gustav trede
 
 public class SelectorFactory {
 
     public static final int DEFAULT_MAX_SELECTORS = 20;
    
The number of java.nio.channels.Selector to create.
 
     private static volatile int maxSelectors = ;
    
 
     private final static ConcurrentQueue<Selectorselectors =
             new ConcurrentQueue<Selector>("temporary-selectors-queue");
    
have we created the Selector instances.
 
     private static volatile boolean initialized = false;
 
     // selector poll timeout
     private static volatile long timeout = .;
    
    
Set max selector pool size.

Parameters:
size max pool size
 
     public static void setMaxSelectors(int sizethrows IOException {
         synchronized () {
             if (size < 0) {
                 SelectorThread.logger().log(.,
                         " tried to remove too many selectors " +
                         size +">=" + new Exception());
                 return;
             }
             int toAdd =  ? size -  : size;
             if (toAdd > 0) {
                 while (toAdd-- > 0) {
                     .add(createSelector());
                 }
             } else {
                 reduce(-toAdd);
             }
              = size;
              = true;
         }
     }

    
Changes the Selector cache size

Parameters:
delta
Throws:
java.io.IOException
    public static void changeSelectorsBy(int deltathrows IOException {
        synchronized () {
            setMaxSelectors( + delta);
        }
    }

    
Returns max selector pool size

Returns:
max pool size
    public static int getMaxSelectors() {
        return ;
    }

    
Please ensure to use try finally around get and return of selector so avoid leaks. Get a exclusive java.nio.channels.Selector

    public static Selector getSelector() {
        if (!) {
            try {
                setMaxSelectors();
            } catch (IOException ex) {
                SelectorThread.logger().log(.,
                        "static init of SelectorFactory failed"ex);
            }
        }
        Selector selector = null;
        try {
            selector = .poll(.);
        } catch (InterruptedException e) {
             SelectorThread.logger().log(."Interrupted during selector polling"e);
        }
        
        if (selector == null) {
            SelectorThread.logger().warning(
                    "No Selector available. Increase default: " + );
        }
        return selector;
    }

    
Please ensure to use try finally around get and return of selector so avoid leaks. Return the java.nio.channels.Selector to the cache

    public static void returnSelector(Selector s) {
        .offer(s);
    }

    
Executes Selector.selectNow() and returns the java.nio.channels.Selector to the cache
    public static void selectNowAndReturnSelector(Selector s) {
        try {
            s.selectNow();
            returnSelector(s);
        } catch (IOException e) {
            final Logger logger = SelectorThread.logger();
            logger.log(.,
                    "Unexpected problem when releasing temporary Selector"e);
            try {
                s.close();
            } catch (IOException ee) {
                // We are not interested
            }
            try {
                reimburseSelector();
            } catch (IOException ee) {
                logger.log(.,
                        "Problematic Selector could not be reimbursed!"ee);
            }
        }
    }

    
Add Selector to the cache. This method could be called to reimberse a lost or problematic Selector.

    public static void reimburseSelector() throws IOException {
        returnSelector(createSelector());
    }

    
Decrease java.nio.channels.Selector pool size
    private static void reduce(int tokill) {
        while (tokill-- > 0) {
            try {
                Selector selector = .poll();
                if (selector != null) {
                    selector.close();
                } else {
                    // can happen in concurrent usage, if selectors are in use and hence not in cache.
                    SelectorThread.logger().warning("SelectorFactory cache could " +
                            "not remove the desired number, too few selectors in cache.");
                    return;
                }
            } catch (IOException e) {
                final Logger logger = SelectorThread.logger();
                if (logger.isLoggable(.)) {
                    logger.log(."SelectorFactory.reduce"e);
                }
            }
        }
    }

    
Creeate Selector

Returns:
Selector
Throws:
java.io.IOException
    protected static Selector createSelector() throws IOException {
        return Selector.open();
    }
    public static long getTimeout(TimeUnit timeUnit) {
        return timeUnit.convert(.);
    }
    public static void setTimeout(long timeoutTimeUnit timeUnit) {
        . = ..convert(timeouttimeUnit);
    }
New to GrepCode? Check out our FAQ X