Search in sources :

Example 1 with SenderState

use of org.apache.catalina.tribes.transport.SenderState in project tomcat by apache.

the class ParallelNioSender method doLoop.

private int doLoop(long selectTimeOut, int maxAttempts, boolean waitForAck, ChannelMessage msg) throws IOException, ChannelException {
    int completed = 0;
    int selectedKeys = selector.select(selectTimeOut);
    if (selectedKeys == 0) {
        return 0;
    }
    Iterator<SelectionKey> it = selector.selectedKeys().iterator();
    while (it.hasNext()) {
        SelectionKey sk = it.next();
        it.remove();
        int readyOps = sk.readyOps();
        sk.interestOps(sk.interestOps() & ~readyOps);
        NioSender sender = (NioSender) sk.attachment();
        try {
            if (sender.process(sk, waitForAck)) {
                completed++;
                sender.setComplete(true);
                if (Logs.MESSAGES.isTraceEnabled()) {
                    Logs.MESSAGES.trace("ParallelNioSender - Sent msg:" + new UniqueId(msg.getUniqueId()) + " at " + new java.sql.Timestamp(System.currentTimeMillis()) + " to " + sender.getDestination().getName());
                }
                SenderState.getSenderState(sender.getDestination()).setReady();
            }
        //end if
        } catch (Exception x) {
            if (log.isTraceEnabled()) {
                log.trace("Error while processing send to " + sender.getDestination().getName(), x);
            }
            SenderState state = SenderState.getSenderState(sender.getDestination());
            int attempt = sender.getAttempt() + 1;
            boolean retry = (sender.getAttempt() <= maxAttempts && maxAttempts > 0);
            synchronized (state) {
                //sk.cancel();
                if (state.isSuspect())
                    state.setFailing();
                if (state.isReady()) {
                    state.setSuspect();
                    if (retry)
                        log.warn(sm.getString("parallelNioSender.send.fail.retrying", sender.getDestination().getName()));
                    else
                        log.warn(sm.getString("parallelNioSender.send.fail", sender.getDestination().getName()), x);
                }
            }
            if (!isConnected()) {
                log.warn(sm.getString("parallelNioSender.sender.disconnected.notRetry", sender.getDestination().getName()));
                ChannelException cx = new ChannelException(sm.getString("parallelNioSender.sender.disconnected.sendFailed"), x);
                cx.addFaultyMember(sender.getDestination(), x);
                throw cx;
            }
            byte[] data = sender.getMessage();
            if (retry) {
                try {
                    sender.disconnect();
                    sender.connect();
                    sender.setAttempt(attempt);
                    sender.setMessage(data);
                } catch (Exception ignore) {
                    state.setFailing();
                }
            } else {
                ChannelException cx = new ChannelException(sm.getString("parallelNioSender.sendFailed.attempt", Integer.toString(sender.getAttempt()), Integer.toString(maxAttempts)), x);
                cx.addFaultyMember(sender.getDestination(), x);
                throw cx;
            }
        //end if
        }
    }
    return completed;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) UniqueId(org.apache.catalina.tribes.UniqueId) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ChannelException(org.apache.catalina.tribes.ChannelException) SenderState(org.apache.catalina.tribes.transport.SenderState) ChannelException(org.apache.catalina.tribes.ChannelException)

Aggregations

IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 SelectionKey (java.nio.channels.SelectionKey)1 ChannelException (org.apache.catalina.tribes.ChannelException)1 UniqueId (org.apache.catalina.tribes.UniqueId)1 SenderState (org.apache.catalina.tribes.transport.SenderState)1