NioSender[] senders = setupForSend(destination);
connect(senders);
setData(senders,data);
int remaining = senders.length;
ChannelException cx = null;
try {
//loop until complete, an error happens, or we timeout
long delta = System.currentTimeMillis() - start;
boolean waitForAck = (Channel.SEND_OPTIONS_USE_ACK &
msg.getOptions()) == Channel.SEND_OPTIONS_USE_ACK;
while ( (remaining>0) && (delta<getTimeout()) ) {
try {
remaining -= doLoop(selectTimeout, getMaxRetryAttempts(),waitForAck,msg);
} catch (Exception x ) {
if (log.isTraceEnabled()) log.trace("Error sending message", x);
int faulty = (cx == null)?0:cx.getFaultyMembers().length;
if ( cx == null ) {
if ( x instanceof ChannelException ) cx = (ChannelException)x;
else cx = new ChannelException("Parallel NIO send failed.", x);
} else {
if (x instanceof ChannelException) {
cx.addFaultyMember(((ChannelException) x).getFaultyMembers());
}
}
//count down the remaining on an error
if (faulty < cx.getFaultyMembers().length) {
remaining -= (cx.getFaultyMembers().length - faulty);
}
}
//bail out if all remaining senders are failing
if ( cx != null && cx.getFaultyMembers().length == remaining ) throw cx;
delta = System.currentTimeMillis() - start;
}
if ( remaining > 0 ) {
//timeout has occurred
ChannelException cxtimeout = new ChannelException(
"Operation has timed out(" + getTimeout() + " ms.).");
if ( cx==null ) cx = new ChannelException(
"Operation has timed out(" + getTimeout() + " ms.).");
for (int i=0; i<senders.length; i++ ) {
if (!senders[i].isComplete()) {
cx.addFaultyMember(senders[i].getDestination(),cxtimeout);
}
}
throw cx;
} else if ( cx != null ) {
//there was an error
throw cx;
}
} catch (Exception x ) {
try { this.disconnect(); } catch (Exception e) {/*Ignore*/}
if ( x instanceof ChannelException ) throw (ChannelException)x;
else throw new ChannelException(x);
}
}