Search in sources :

Example 1 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat by apache.

the class BioReceiver method listen.

public void listen() throws Exception {
    if (doListen()) {
        log.warn(sm.getString("bioReceiver.already.started"));
        return;
    }
    setListen(true);
    while (doListen()) {
        Socket socket = null;
        if (getTaskPool().available() < 1) {
            if (log.isWarnEnabled())
                log.warn(sm.getString("bioReceiver.threads.busy"));
        }
        BioReplicationTask task = (BioReplicationTask) getTaskPool().getRxTask();
        //should never happen
        if (task == null)
            continue;
        try {
            socket = serverSocket.accept();
        } catch (Exception x) {
            if (doListen())
                throw x;
        }
        if (!doListen()) {
            task.setDoRun(false);
            task.serviceSocket(null, null);
            getExecutor().execute(task);
            //regular shutdown
            break;
        }
        if (socket == null)
            continue;
        socket.setReceiveBufferSize(getRxBufSize());
        socket.setSendBufferSize(getTxBufSize());
        socket.setTcpNoDelay(getTcpNoDelay());
        socket.setKeepAlive(getSoKeepAlive());
        socket.setOOBInline(getOoBInline());
        socket.setReuseAddress(getSoReuseAddress());
        socket.setSoLinger(getSoLingerOn(), getSoLingerTime());
        socket.setSoTimeout(getTimeout());
        ObjectReader reader = new ObjectReader(socket);
        task.serviceSocket(socket, reader);
        getExecutor().execute(task);
    }
//while
}
Also used : ObjectReader(org.apache.catalina.tribes.io.ObjectReader) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) IOException(java.io.IOException)

Example 2 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat70 by apache.

the class NioReceiver method listen.

/**
 * get data from channel and store in byte array
 * send it to cluster
 * @throws IOException
 * @throws java.nio.channels.ClosedChannelException
 */
protected void listen() throws Exception {
    if (doListen()) {
        log.warn("ServerSocketChannel already started");
        return;
    }
    setListen(true);
    // Avoid NPEs if selector is set to null on stop.
    Selector selector = this.selector.get();
    if (selector != null && datagramChannel != null) {
        // max size for a datagram packet
        ObjectReader oreader = new ObjectReader(MAX_UDP_SIZE);
        registerChannel(selector, datagramChannel, SelectionKey.OP_READ, oreader);
    }
    while (doListen() && selector != null) {
        // selected set contains keys of the ready channels
        try {
            events();
            socketTimeouts();
            int n = selector.select(getSelectorTimeout());
            if (n == 0) {
                // nothing to do
                continue;
            }
            // get an iterator over the set of selected keys
            Iterator<SelectionKey> it = selector.selectedKeys().iterator();
            // look at each key in the selected set
            while (it != null && it.hasNext()) {
                SelectionKey key = it.next();
                // Is a new connection coming in?
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.channel();
                    SocketChannel channel = server.accept();
                    channel.socket().setReceiveBufferSize(getTxBufSize());
                    channel.socket().setSendBufferSize(getTxBufSize());
                    channel.socket().setTcpNoDelay(getTcpNoDelay());
                    channel.socket().setKeepAlive(getSoKeepAlive());
                    channel.socket().setOOBInline(getOoBInline());
                    channel.socket().setReuseAddress(getSoReuseAddress());
                    channel.socket().setSoLinger(getSoLingerOn(), getSoLingerTime());
                    channel.socket().setSoTimeout(getTimeout());
                    Object attach = new ObjectReader(channel);
                    registerChannel(selector, channel, SelectionKey.OP_READ, attach);
                }
                // is there data to read on this channel?
                if (key.isReadable()) {
                    readDataFromSocket(key);
                } else {
                    key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
                }
                // remove key from selected set, it's been handled
                it.remove();
            }
        } catch (java.nio.channels.ClosedSelectorException cse) {
        // ignore is normal at shutdown or stop listen socket
        } catch (java.nio.channels.CancelledKeyException nx) {
            log.warn("Replication client disconnected, error when polling key. Ignoring client.");
        } catch (Throwable t) {
            if (t instanceof ThreadDeath) {
                throw (ThreadDeath) t;
            }
            if (t instanceof VirtualMachineError) {
                throw (VirtualMachineError) t;
            }
            log.error("Unable to process request in NioReceiver", t);
        }
    }
    serverChannel.close();
    if (datagramChannel != null) {
        try {
            datagramChannel.close();
        } catch (Exception iox) {
            if (log.isDebugEnabled())
                log.debug("Unable to close datagram channel.", iox);
        }
        datagramChannel = null;
    }
    closeSelector();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) CancelledKeyException(java.nio.channels.CancelledKeyException) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ObjectReader(org.apache.catalina.tribes.io.ObjectReader) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 3 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat70 by apache.

the class NioReceiver method socketTimeouts.

protected void socketTimeouts() {
    long now = System.currentTimeMillis();
    if ((now - lastCheck) < getSelectorTimeout())
        return;
    // timeout
    Selector tmpsel = this.selector.get();
    Set<SelectionKey> keys = (isListening() && tmpsel != null) ? tmpsel.keys() : null;
    if (keys == null)
        return;
    for (Iterator<SelectionKey> iter = keys.iterator(); iter.hasNext(); ) {
        SelectionKey key = iter.next();
        try {
            // else
            if (key.interestOps() == 0) {
                // check for keys that didn't make it in.
                ObjectReader ka = (ObjectReader) key.attachment();
                if (ka != null) {
                    long delta = now - ka.getLastAccess();
                    if (delta > getTimeout() && (!ka.isAccessed())) {
                        if (log.isWarnEnabled())
                            log.warn("Channel key is registered, but has had no interest ops for the last " + getTimeout() + " ms. (cancelled:" + ka.isCancelled() + "):" + key + " last access:" + new java.sql.Timestamp(ka.getLastAccess()) + " Possible cause: all threads used, perform thread dump");
                        ka.setLastAccess(now);
                    // key.interestOps(SelectionKey.OP_READ);
                    }
                // end if
                } else {
                    cancelledKey(key);
                }
            // end if
            }
        // end if
        } catch (CancelledKeyException ckx) {
            cancelledKey(key);
        }
    }
    lastCheck = System.currentTimeMillis();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) CancelledKeyException(java.nio.channels.CancelledKeyException) ObjectReader(org.apache.catalina.tribes.io.ObjectReader) Selector(java.nio.channels.Selector)

Example 4 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat70 by apache.

the class NioReplicationTask method run.

// loop forever waiting for work to do
@Override
public synchronized void run() {
    if (buffer == null) {
        int size = getRxBufSize();
        if (key.channel() instanceof DatagramChannel) {
            size = ChannelReceiver.MAX_UDP_SIZE;
        }
        if ((getOptions() & OPTION_DIRECT_BUFFER) == OPTION_DIRECT_BUFFER) {
            buffer = ByteBuffer.allocateDirect(size);
        } else {
            buffer = ByteBuffer.allocate(size);
        }
    } else {
        buffer.clear();
    }
    if (key == null) {
        // just in case
        return;
    }
    if (log.isTraceEnabled())
        log.trace("Servicing key:" + key);
    try {
        ObjectReader reader = (ObjectReader) key.attachment();
        if (reader == null) {
            if (log.isTraceEnabled())
                log.trace("No object reader, cancelling:" + key);
            cancelKey(key);
        } else {
            if (log.isTraceEnabled())
                log.trace("Draining channel:" + key);
            drainChannel(key, reader);
        }
    } catch (Exception e) {
        // end expire after a certain time.
        if (e instanceof CancelledKeyException) {
        // do nothing
        } else if (e instanceof IOException) {
            // dont spew out stack traces for IO exceptions unless debug is enabled.
            if (log.isDebugEnabled())
                log.debug("IOException in replication worker, unable to drain channel. Probable cause: Keep alive socket closed[" + e.getMessage() + "].", e);
            else
                log.warn("IOException in replication worker, unable to drain channel. Probable cause: Keep alive socket closed[" + e.getMessage() + "].");
        } else if (log.isErrorEnabled()) {
            // this is a real error, log it.
            log.error("Exception caught in TcpReplicationThread.drainChannel.", e);
        }
        cancelKey(key);
    } finally {
    }
    key = null;
    // done, ready for more, return to pool
    getTaskPool().returnWorker(this);
}
Also used : CancelledKeyException(java.nio.channels.CancelledKeyException) DatagramChannel(java.nio.channels.DatagramChannel) ObjectReader(org.apache.catalina.tribes.io.ObjectReader) IOException(java.io.IOException) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException)

Example 5 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat70 by apache.

the class BioReceiver method listen.

public void listen() throws Exception {
    if (doListen()) {
        log.warn("ServerSocket already started");
        return;
    }
    setListen(true);
    while (doListen()) {
        Socket socket = null;
        if (getTaskPool().available() < 1) {
            if (log.isWarnEnabled())
                log.warn("All BIO server replication threads are busy, unable to handle more requests until a thread is freed up.");
        }
        BioReplicationTask task = (BioReplicationTask) getTaskPool().getRxTask();
        // should never happen
        if (task == null)
            continue;
        try {
            socket = serverSocket.accept();
        } catch (Exception x) {
            if (doListen())
                throw x;
        }
        if (!doListen()) {
            task.setDoRun(false);
            task.serviceSocket(null, null);
            getExecutor().execute(task);
            // regular shutdown
            break;
        }
        if (socket == null)
            continue;
        socket.setReceiveBufferSize(getRxBufSize());
        socket.setSendBufferSize(getTxBufSize());
        socket.setTcpNoDelay(getTcpNoDelay());
        socket.setKeepAlive(getSoKeepAlive());
        socket.setOOBInline(getOoBInline());
        socket.setReuseAddress(getSoReuseAddress());
        socket.setSoLinger(getSoLingerOn(), getSoLingerTime());
        socket.setSoTimeout(getTimeout());
        ObjectReader reader = new ObjectReader(socket);
        task.serviceSocket(socket, reader);
        getExecutor().execute(task);
    }
// while
}
Also used : ObjectReader(org.apache.catalina.tribes.io.ObjectReader) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) IOException(java.io.IOException)

Aggregations

ObjectReader (org.apache.catalina.tribes.io.ObjectReader)14 IOException (java.io.IOException)8 CancelledKeyException (java.nio.channels.CancelledKeyException)8 ClosedSelectorException (java.nio.channels.ClosedSelectorException)4 DatagramChannel (java.nio.channels.DatagramChannel)4 SelectionKey (java.nio.channels.SelectionKey)4 Selector (java.nio.channels.Selector)4 ServerSocketChannel (java.nio.channels.ServerSocketChannel)4 SocketChannel (java.nio.channels.SocketChannel)4 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 RemoteProcessException (org.apache.catalina.tribes.RemoteProcessException)2