Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2007-2010 Oracle and/or its affiliates. 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_1_1.html
  * or packager/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 packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [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.comet;
 
 import java.util.Set;
NIO java.nio.channels.Selector allowing CometHandler to receive non-blocking requests bytes during request polling.

Author(s):
Jeanfrancois Arcand
 
 public class CometSelector {
 
     // Workaround for Issue 555
     private long lastSpinTimestamp;
     private int emptySpinCounter;
    
The threshold for detecting selector.select spin on linux, used for enabling workaround to prevent server from hanging.
 
     private final static int spinRateTreshold = 2000;

    
Enable workaround Linux spinning Selector
 
     static final boolean isLinux =
             System.getProperty("os.name").equalsIgnoreCase("linux") &&
                 !System.getProperty("java.version").startsWith("1.7");

    
The CometEngine singleton
 
     protected CometEngine cometEngine;


    
 
     private Selector selector;


    
Logger.
 
     private Logger logger = SelectorThread.logger();


    
        = new ConcurrentHashMap<SelectionKey,CometTask>();


    
New CometSelector

Parameters:
cometEngine The CometEngine singleton
    public CometSelector(CometEngine cometEngine) {
        this. = cometEngine;
    }


    
Start the java.nio.channels.Selector running on its Thread.
    public void start() throws InterruptedException{
        final CountDownLatch isStartedLatch = new CountDownLatch(1);
        new Thread("CometSelector"){
            {
                setDaemon(true);
            }
            @Override
                public void run(){
                    try{
                         = Selector.open();
                    } catch(IOException ex){
                        // Most probably a fd leak.
                        .log(.,"CometSelector.open()",ex);
                        return;
                    }
                    isStartedLatch.countDown();
                    while (true){
                        SelectionKey key = null;
                        Set readyKeys;
                        Iterator<SelectionKeyiterator;
                        int selectorState = 0;
                        try{
                            selectorState = 0;
                            try{
                                selectorState = .select(1000);
                            } catch (CancelledKeyException ex){
                                if (.isLoggable(.)){
                                    .log(.,"CometSelector.open()",ex);
                                }
                            }
                            readyKeys = .selectedKeys();
                            // JDK issue.
                            if (readyKeys.size() != 0 && ) {
                                resetSpinCounter();
                            } else if (){
                                long sr = getSpinRate();
                                if (sr > ) {
                                    workaroundSelectorSpin();
                                }
                            }
                            iterator = readyKeys.iterator();
                            CometTask cometTask;
                            while (iterator.hasNext()) {
                                key = iterator.next();
                                iterator.remove();
                                try {
                                    if (key.isValid()) {
                                        cometTask = (CometTaskkey.attachment();
                                        if (key.isReadable()) {
                                            key.interestOps(key.interestOps() &
                                                    (~.));
                                            cometTask.upcoming_op =
                                                    ..;
                                        }
                                        if (key.isWritable()) {
                                            key.interestOps(key.interestOps() &
                                                    (~.));
                                            cometTask.upcoming_op =
                                                    ..;
                                        }
                                        if (cometTask != null &&
                                                cometTask.getSelectionKey() != null &&
                                                cometTask.getSelectionKey().attachment() == null) {
                                            cometTask.execute();
                                        } else {
                                            key.cancel();
                                        }
                                    } else {
                                        cancelKey(key);
                                    }
                                } catch (Exception e) {
                                    try {
                                        cancelKey(key);
                                    } catch (Exception ee) {
                                        .log(."CometSelector"ee);
                                    }
                                }
                            }
                            Iterator<Entry<SelectionKeyCometTask>> entries =
                                .entrySet().iterator();
                            
The mainKey is the SelectionKey returned by the Selector used in the SelectorThread class.
                            SelectionKey mainKey;
                            SocketChannel channel;
                            while (entries.hasNext()) {
                                final Entry<SelectionKeyCometTaskentry = entries.next();
                                entries.remove();
                                mainKey = entry.getKey();
                                cometTask = entry.getValue();
                                try {
                                    channel = (SocketChannelmainKey.channel();
                                    if (mainKey.isValid() && channel.isOpen()) {
                                        key = channel.register(,
                                                .);
                                        cometTask.setCometKey(key);
                                        key.attach(cometTask);
                                    } else {
                                        cancelCometTask(cometTask);
                                    }
                                } catch (Exception e) {
                                    cancelCometTask(cometTask);
                                }
                            }
                            expireIdleKeys();
                            if (selectorState <= 0){
                                .selectedKeys().clear();
                            }
                        } catch (Throwable t){
                            if (key != null){
                                try{
                                    cancelKey(key);
                                } catch (Throwable t2){
                                    .log(.,"CometSelector",t2);
                                }
                            }
                            if (selectorState <= 0){
                                .selectedKeys().clear();
                            }
                            if (.isLoggable(.)){
                                .log(.,"CometSelector",t);
                            }
                        }
                    }
                }
        }.start();
        isStartedLatch.await();
    }


    
Expires registered java.nio.channels.SelectionKey. If a java.nio.channels.SelectionKey is expired, the request will be resumed and the HTTP request will complete,
    protected void expireIdleKeys(){
        Set<SelectionKeyreadyKeys = .keys();
        if (readyKeys.isEmpty()){
            return;
        }
        long current = System.currentTimeMillis();
        Iterator<SelectionKeyiterator = readyKeys.iterator();
        SelectionKey key;
        while (iterator.hasNext()) {
            key = iterator.next();
            try {
                final CometTask cometTask = (CometTaskkey.attachment();
                if (cometTask == null) {
                    continue;
                }
                if (cometTask.getExpirationDelay() == -1) {
                    continue;
                }
                final long expire = cometTask.getExpireTime();
                if (current - expire >= cometTask.getExpirationDelay()) {
                    cancelKey(key);
                }

                
The connection has been resumed since the timeout is re-attached to the SelectionKey so cancel the Comet key.
                if (cometTask.getSelectionKey() != null &&
                        cometTask.getSelectionKey().attachment() instanceof Long) {
                    key.cancel();
                    .interrupt(key);
                }
            } catch (Exception e) {
                cancelKey(key);
            }
        }
    }


    
Cancel a java.nio.channels.SelectionKey, and delegate the request polling interruption to the CometEngine

Parameters:
key the expired java.nio.channels.SelectionKey
    protected synchronized void cancelKey(SelectionKey key){
        if (key == nullreturn;
        try{
            CometTask cometTask = (CometTask)key.attachment();
            if (cometTask != null){
                cancelCometTask(cometTask);
            } else {
                .interrupt(key);
            }
        } catch (Throwable t) {
            .log(.,"CometSelector",t);
        } finally {
            key.attach(null);
        }
        key.cancel();
    }
    protected synchronized void cancelCometTask(CometTask cometTask){
        if (cometTask == nullreturn;
        try{
            SelectorThread st = cometTask.getSelectorThread();
            SelectionKey mainKey = cometTask.getSelectionKey();
            if (cometTask.getCometContext() != null) {
                cometTask.getCometContext().interrupt(cometTask);
            }
            .interrupt(cometTask);
            st.cancelKey(mainKey);
        } catch (Throwable t) {
            .log(.,"CometSelector",t);
        }
    }

    
Register the java.nio.channels.SelectionKey to the java.nio.channels.Selector. We cannot register the java.nio.channels.SelectionKey directy on the java.nio.channels.Selector because there is a deadlock in the VM (see bug XXX).
    public void registerKey(SelectionKey keyCometTask cometTask){
        if (key == null || cometTask == null || !key.isValid() ||  == nullreturn;
        cometTask.setExpireTime(System.currentTimeMillis());
        .put(keycometTask);
        .wakeup();
    }


    
    public void wakeup(){
        .wakeup();
    }

    
Return the SelectionKey associated with this channel.
    public SelectionKey cometKeyFor(SelectableChannel channel){
        NioProvider nioP = NioProvider.getProvider();
        if (nioP == null) {
            return channel.keyFor();
        } else {
            return nioP.keyFor(channel);
        }
    }
    public void resetSpinCounter(){
          = 0;
    }
    public int getSpinRate(){
        if (++ == 0){
             = System.nanoTime();
        } else if ( == 1000) {
            long deltatime = System.nanoTime() - ;
            int contspinspersec = (int) (1000 * 1000000000L / deltatime);
              = 0;
            return contspinspersec;
        }
        return 0;
    }
    private void workaroundSelectorSpin() throws IOException {
        Selector newSelector = Selector.open();
        Set<SelectionKeykeys = .keys();
        for (SelectionKey key : keys) {
            try {
                key.channel().register(newSelectorkey.interestOps(), key.attachment());
            } catch (Exception e) {
            }
        }
        try {
            .close();
        } catch (Exception e) {
        }
         = newSelector;
    }
New to GrepCode? Check out our FAQ X