protected BioSender[] setupForSend(Member[] destination) throws ChannelException { ChannelException cx = null; BioSender[] result = new BioSender[destination.length]; for ( int i=0; i<destination.length; i++ ) { try { BioSender sender = (BioSender) bioSenders.get(destination[i]); if (sender == null) { sender = new BioSender(); sender.transferProperties(this,sender); sender.setDestination(destination[i]); bioSenders.put(destination[i], sender); } result[i] = sender; if (!result[i].isConnected() ) result[i].connect(); result[i].keepalive(); }catch (Exception x ) { if ( cx== null ) cx = new ChannelException(x); cx.addFaultyMember(destination[i],x); } } if ( cx!=null ) throw cx; else return result; }
/** * Send message. */ public void sendMessage(byte[] data, boolean waitForAck) throws IOException { IOException exception = null; setAttempt(0); try { // first try with existing connection pushMessage(data,false,waitForAck); } catch (IOException x) { SenderState.getSenderState(getDestination()).setSuspect(); exception = x; if (log.isTraceEnabled()) log.trace(sm.getString("IDataSender.send.again", getAddress().getHostAddress(),new Integer(getPort())),x); while ( getAttempt()<getMaxRetryAttempts() ) { try { setAttempt(getAttempt()+1); // second try with fresh connection pushMessage(data, true,waitForAck); exception = null; } catch (IOException xx) { exception = xx; closeSocket(); } } } finally { setRequestCount(getRequestCount()+1); keepalive(); if ( exception != null ) throw exception; } }
@Override public void remove(Member member) { //disconnect senders BioSender sender = bioSenders.remove(member); if ( sender != null ) sender.disconnect(); }
/** * Name of this SockerSender */ @Override public String toString() { StringBuilder buf = new StringBuilder("DataSender[("); buf.append(super.toString()).append(")"); buf.append(getAddress()).append(":").append(getPort()).append("]"); return buf.toString(); }
/** * disconnect and close socket * * @see IDataSender#disconnect() */ public void disconnect() { boolean connect = isConnected(); closeSocket(); if (connect) { if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.disconnect", getAddress().getHostAddress(), new Integer(getPort()), new Long(0))); } }
if(isConnected()) return ; try { socket = new Socket(); InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort()); socket.connect(sockaddr,(int)getTimeout()); socket.setSendBufferSize(getTxBufSize()); socket.setReceiveBufferSize(getRxBufSize()); socket.setSoTimeout( (int) getTimeout()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setReuseAddress(getSoReuseAddress()); socket.setOOBInline(getOoBInline()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setTrafficClass(getSoTrafficClass()); setConnected(true); soOut = socket.getOutputStream(); soIn = socket.getInputStream(); setRequestCount(0); setConnectTime(System.currentTimeMillis()); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket", getAddress().getHostAddress(), new Integer(getPort()), new Long(0))); } catch (IOException ex1) { SenderState.getSenderState(getDestination()).setSuspect(); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket.failure",getAddress().getHostAddress(), new Integer(getPort()),new Long(0)), ex1); throw (ex1);
/** * Push messages with only one socket at a time * Wait for ack is needed and make auto retry when write message is failed. * After sending error close and reopen socket again. * * After successful sending update stats * * WARNING: Subclasses must be very careful that only one thread call this pushMessage at once!!! * * @see #closeSocket() * @see #openSocket() * @see #sendMessage(byte[], boolean) * * @param data * data to send * @since 5.5.10 */ protected void pushMessage(byte[] data, boolean reconnect, boolean waitForAck) throws IOException { keepalive(); if ( reconnect ) closeSocket(); if (!isConnected()) openSocket(); soOut.write(data); soOut.flush(); if (waitForAck) waitForAck(); SenderState.getSenderState(getDestination()).setReady(); }
/** * Close socket. * * @see #disconnect() */ protected void closeSocket() { if(isConnected()) { if (socket != null) { try { socket.close(); } catch (IOException x) { // Ignore } finally { socket = null; soOut = null; soIn = null; } } setRequestCount(0); setConnected(false); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.closeSocket",getAddress().getHostAddress(), new Integer(getPort()),new Long(0))); } }
if (i == -1) throw new IOException(sm.getString("IDataSender.ack.eof",getAddress(), new Integer(socket.getLocalPort()))); else throw new IOException(sm.getString("IDataSender.ack.wrong",getAddress(), new Integer(socket.getLocalPort()))); } else if ( failAckReceived && getThrowOnFailedAck()) { throw new RemoteProcessException("Received a failed ack:org.apache.catalina.tribes.transport.Constants.FAIL_ACK_DATA"); String errmsg = sm.getString("IDataSender.ack.missing", getAddress(),new Integer(socket.getLocalPort()), new Long(getTimeout())); if ( SenderState.getSenderState(getDestination()).isReady() ) { SenderState.getSenderState(getDestination()).setSuspect(); if ( log.isWarnEnabled() ) log.warn(errmsg, x); } else {
if(isConnected()) return ; try { socket = new Socket(); InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort()); socket.connect(sockaddr,(int)getTimeout()); socket.setSendBufferSize(getTxBufSize()); socket.setReceiveBufferSize(getRxBufSize()); socket.setSoTimeout( (int) getTimeout()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setReuseAddress(getSoReuseAddress()); socket.setOOBInline(getOoBInline()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setTrafficClass(getSoTrafficClass()); setConnected(true); soOut = socket.getOutputStream(); soIn = socket.getInputStream(); setRequestCount(0); setConnectTime(System.currentTimeMillis()); if (log.isDebugEnabled()) log.debug(sm.getString("bioSender.openSocket", getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0))); } catch (IOException ex1) { SenderState.getSenderState(getDestination()).setSuspect(); if (log.isDebugEnabled()) log.debug(sm.getString("bioSender.openSocket.failure",getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0)), ex1); throw ex1;
/** * Push messages with only one socket at a time * Wait for ack is needed and make auto retry when write message is failed. * After sending error close and reopen socket again. * * After successful sending update stats * * WARNING: Subclasses must be very careful that only one thread call this pushMessage at once!!! * * @see #closeSocket() * @see #openSocket() * @see #sendMessage(byte[], boolean) * * @param data * data to send * @since 5.5.10 */ protected void pushMessage(byte[] data, boolean reconnect, boolean waitForAck) throws IOException { keepalive(); if ( reconnect ) closeSocket(); if (!isConnected()) openSocket(); soOut.write(data); soOut.flush(); if (waitForAck) waitForAck(); SenderState.getSenderState(getDestination()).setReady(); }
/** * Close socket. * * @see #disconnect() */ protected void closeSocket() { if(isConnected()) { if (socket != null) { try { socket.close(); } catch (IOException x) { // Ignore } finally { socket = null; soOut = null; soIn = null; } } setRequestCount(0); setConnected(false); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.closeSocket",getAddress().getHostAddress(), new Integer(getPort()),new Long(0))); } }
/** * disconnect and close socket * * @see org.apache.catalina.tribes.transport.DataSender#disconnect() */ @Override public void disconnect() { boolean connect = isConnected(); closeSocket(); if (connect) { if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.disconnect", getAddress().getHostAddress(), new Integer(getPort()), new Long(0))); } }
if (i == -1) throw new IOException(sm.getString("IDataSender.ack.eof",getAddress(), new Integer(socket.getLocalPort()))); else throw new IOException(sm.getString("IDataSender.ack.wrong",getAddress(), new Integer(socket.getLocalPort()))); } else if ( failAckReceived && getThrowOnFailedAck()) { throw new RemoteProcessException("Received a failed ack:org.apache.catalina.tribes.transport.Constants.FAIL_ACK_DATA"); String errmsg = sm.getString("IDataSender.ack.missing", getAddress(),new Integer(socket.getLocalPort()), new Long(getTimeout())); if ( SenderState.getSenderState(getDestination()).isReady() ) { SenderState.getSenderState(getDestination()).setSuspect(); if ( log.isWarnEnabled() ) log.warn(errmsg, x); } else {
/** * Name of this SockerSender */ @Override public String toString() { StringBuilder buf = new StringBuilder("DataSender[("); buf.append(super.toString()).append(")"); buf.append(getAddress()).append(":").append(getPort()).append("]"); return buf.toString(); }
if(isConnected()) return ; try { socket = new Socket(); InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort()); socket.connect(sockaddr,(int)getTimeout()); socket.setSendBufferSize(getTxBufSize()); socket.setReceiveBufferSize(getRxBufSize()); socket.setSoTimeout( (int) getTimeout()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setReuseAddress(getSoReuseAddress()); socket.setOOBInline(getOoBInline()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setTrafficClass(getSoTrafficClass()); setConnected(true); soOut = socket.getOutputStream(); soIn = socket.getInputStream(); setRequestCount(0); setConnectTime(System.currentTimeMillis()); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket", getAddress().getHostAddress(), new Integer(getPort()), new Long(0))); } catch (IOException ex1) { SenderState.getSenderState(getDestination()).setSuspect(); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket.failure",getAddress().getHostAddress(), new Integer(getPort()),new Long(0)), ex1); throw (ex1);
setAttempt(0); try { pushMessage(data,false,waitForAck); } catch (IOException x) { SenderState.getSenderState(getDestination()).setSuspect(); exception = x; if (log.isTraceEnabled()) log.trace(sm.getString("bioSender.send.again", getAddress().getHostAddress(),Integer.valueOf(getPort())),x); while ( getAttempt()<getMaxRetryAttempts() ) { try { setAttempt(getAttempt()+1); pushMessage(data, true,waitForAck); exception = null; } catch (IOException xx) { exception = xx; closeSocket(); setRequestCount(getRequestCount()+1); keepalive(); if ( exception != null ) throw exception;
/** * Push messages with only one socket at a time * Wait for ack is needed and make auto retry when write message is failed. * After sending error close and reopen socket again. * * After successfull sending update stats * * WARNING: Subclasses must be very carefull that only one thread call this pushMessage at once!!! * * @see #closeSocket() * @see #openSocket() * @see #writeData(ChannelMessage) * * @param data * data to send * @since 5.5.10 */ protected void pushMessage(byte[] data, boolean reconnect, boolean waitForAck) throws IOException { keepalive(); if ( reconnect ) closeSocket(); if (!isConnected()) openSocket(); soOut.write(data); soOut.flush(); if (waitForAck) waitForAck(); SenderState.getSenderState(getDestination()).setReady(); }
protected BioSender[] setupForSend(Member[] destination) throws ChannelException { ChannelException cx = null; BioSender[] result = new BioSender[destination.length]; for ( int i=0; i<destination.length; i++ ) { try { BioSender sender = bioSenders.get(destination[i]); if (sender == null) { sender = new BioSender(); AbstractSender.transferProperties(this,sender); sender.setDestination(destination[i]); bioSenders.put(destination[i], sender); } result[i] = sender; if (!result[i].isConnected() ) result[i].connect(); result[i].keepalive(); }catch (Exception x ) { if ( cx== null ) cx = new ChannelException(x); cx.addFaultyMember(destination[i],x); } } if ( cx!=null ) throw cx; else return result; }
/** * Close socket. * * @see #disconnect() */ protected void closeSocket() { if(isConnected()) { if (socket != null) { try { socket.close(); } catch (IOException x) { // Ignore } finally { socket = null; soOut = null; soIn = null; } } setRequestCount(0); setConnected(false); if (log.isDebugEnabled()) log.debug(sm.getString("bioSender.closeSocket",getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0))); } }