protected OutputStream socketOutputStream() { final ResilentSocketOutputStream outputStream = new ResilentSocketOutputStream(host, port, connectionTimeoutMs, sendBufferSize, socketFactory); outputStream.setContext(context); return outputStream; } }
/** * Creates a new stream based on the socket configuration. * * @param host The host or an IP address of the server. * @param port The port on the server which accepts TCP connections. * @param connectionTimeoutMs The timeout for establishing a new TCP connection. * @param sendBufferSize The size of the send buffer of the socket stream in bytes. * @param socketFactory The factory for customizing the client socket. */ public ResilentSocketOutputStream(String host, int port, int connectionTimeoutMs, int sendBufferSize, SocketFactory socketFactory) { this.host = host; this.port = port; this.connectionTimeoutMs = connectionTimeoutMs; this.sendBufferSize = sendBufferSize; this.socketFactory = socketFactory; try { this.os = openNewOutputStream(); } catch (IOException e) { throw new IllegalStateException("Unable to create a TCP connection to " + host + ":" + port, e); } this.presumedClean = true; }
public boolean isTooSoon() { long now = getCurrentTime(); if(now > next) { next = now + getBackoffCoefficient(); return false; } else { return true; } }
@Override public void write(int b) { if (isPresumedInError()) { if (!recoveryCoordinator.isTooSoon()) { attemptRecovery(); } return; // return regardless of the success of the recovery attempt } try { os.write(b); postSuccessfulWrite(); } catch (IOException e) { postIOFailure(e); } }
void postIOFailure(IOException e) { addStatusIfCountNotOverLimit(new ErrorStatus("IO failure while writing to " + getDescription(), this, e)); presumedClean = false; if (recoveryCoordinator == null) { recoveryCoordinator = new RecoveryCoordinator(); } }
void attemptRecovery() { try { close(); } catch (IOException e) { } addStatusIfCountNotOverLimit(new InfoStatus( "Attempting to recover from IO failure on " + getDescription(), this)); // subsequent writes must always be in append mode try { os = openNewOutputStream(); presumedClean = true; } catch (IOException e) { addStatusIfCountNotOverLimit(new ErrorStatus("Failed to open " + getDescription(), this, e)); } }
void addStatusIfCountNotOverLimit(Status s) { ++statusCount; if (statusCount < STATUS_COUNT_LIMIT) { addStatus(s); } if (statusCount == STATUS_COUNT_LIMIT) { addStatus(s); addStatus(new InfoStatus("Will supress future messages regarding " + getDescription(), this)); } }
ResilientFileOutputStream resilientFos = new ResilientFileOutputStream( file, append); resilientFos.setContext(context); setOutputStream(resilientFos); } finally {
@Override public void flush() { if (os != null) { try { os.flush(); postSuccessfulWrite(); } catch (IOException e) { postIOFailure(e); } } }
private void safeWrite(E event) throws IOException { ResilientFileOutputStream resilientFOS = (ResilientFileOutputStream) getOutputStream(); FileChannel fileChannel = resilientFOS.getChannel(); if (fileChannel == null) { return; } FileLock fileLock = null; try { fileLock = fileChannel.lock(); long position = fileChannel.position(); long size = fileChannel.size(); if (size != position) { fileChannel.position(size); } super.writeOut(event); } finally { if (fileLock != null) { fileLock.release(); } } }
public void write(byte b[], int off, int len) { if (isPresumedInError()) { if (!recoveryCoordinator.isTooSoon()) { attemptRecovery(); } return; // return regardless of the success of the recovery attempt } try { os.write(b, off, len); postSuccessfulWrite(); } catch (IOException e) { postIOFailure(e); } }
public boolean isTooSoon() { long now = getCurrentTime(); if(now > next) { next = now + getBackoffCoefficient(); return false; } else { return true; } }
public void postIOFailure(IOException e) { addStatusIfCountNotOverLimit(new ErrorStatus("IO failure while writing to " + getDescription(), this, e)); presumedClean = false; if (recoveryCoordinator == null) { recoveryCoordinator = new RecoveryCoordinator(); } }
private void postSuccessfulWrite() { if (recoveryCoordinator != null) { recoveryCoordinator = null; statusCount = 0; addStatus(new InfoStatus("Recovered from IO failure on " + getDescription(), this)); } }
protected OutputStream socketOutputStream() { final ResilentSocketOutputStream outputStream = new ResilentSocketOutputStream(host, port, connectionTimeoutMs, sendBufferSize, socketFactory); outputStream.setContext(context); return outputStream; } }
public boolean isTooSoon() { long now = getCurrentTime(); if(now > next) { next = now + getBackoffCoefficient(); return false; } else { return true; } }
public boolean isTooSoon() { long now = getCurrentTime(); if(now > next) { next = now + getBackoffCoefficient(); return false; } else { return true; } }
public boolean isTooSoon() { long now = getCurrentTime(); if (now > next) { next = now + getBackoffCoefficient(); return false; } else { return true; } }
public boolean isTooSoon() { long now = getCurrentTime(); if(now > next) { next = now + getBackoffCoefficient(); return false; } else { return true; } }
public boolean isTooSoon() { long now = getCurrentTime(); if (now > next) { next = now + getBackoffCoefficient(); return false; } else { return true; } }