Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to not flush stream buffers on every message send #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ messageSender.setDefaultSeverity(Severity.INFORMATIONAL);
messageSender.setSyslogServerHostname("127.0.0.1");
messageSender.setSyslogServerPort(1234);
messageSender.setMessageFormat(MessageFormat.RFC_3164); // optional, default is RFC 3164
// onSend is the default value, other value is onClose which has higher throughput but
// risks dataloss if the syslog server address dynamically changes
messageSender.setSocketFlush(TcpSyslogMessageSender.SocketFlush.onSend);
messageSender.setSsl(false);

// send a Syslog message
Expand All @@ -119,6 +122,9 @@ messageSender.setDefaultSeverity(Severity.INFORMATIONAL);
messageSender.setSyslogServerHostname("127.0.0.1");
messageSender.setSyslogServerPort(1234);
messageSender.setMessageFormat(MessageFormat.RFC_3164); // optional, default is RFC 3164
// onSend is the default value, other value is onClose which has higher throughput but
// risks dataloss if the syslog server address dynamically changes
messageSender.setSocketFlush(TcpSyslogMessageSender.SocketFlush.onSend);
messageSender.setSsl(true);

// send a Syslog message
Expand All @@ -145,6 +151,9 @@ messageSender.setSyslogServerHostname("127.0.0.1");
// syslog-tls usually uses port 6514 as per https://tools.ietf.org/html/rfc5425#page-11
messageSender.setSyslogServerPort(6514);
messageSender.setMessageFormat(MessageFormat.RFC_5425);
// onSend is the default value, other value is onClose which has higher throughput but
// risks dataloss if the syslog server address dynamically changes
messageSender.setSocketFlush(TcpSyslogMessageSender.SocketFlush.onSend);
messageSender.setSsl(true);

// send a Syslog message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@
*/
@ThreadSafe
public class TcpSyslogMessageSender extends AbstractSyslogMessageSender implements Closeable {
public enum SocketFlush {
onSend, // default,
onClose // higher throughput, but at a risk of data-loss if the syslog server address dynamically changes
};

public final static int SETTING_SOCKET_CONNECT_TIMEOUT_IN_MILLIS_DEFAULT_VALUE = 500;
public final static int SETTING_MAX_RETRY = 2;
public final static SocketFlush DEFAULT_SOCKET_FLUSH = SocketFlush.onSend;

/**
* {@link java.net.InetAddress InetAddress} of the remote Syslog Server.
Expand All @@ -74,6 +80,9 @@ public class TcpSyslogMessageSender extends AbstractSyslogMessageSender implemen
* Number of retries to send a message before throwing an exception.
*/
private int maxRetryCount = SETTING_MAX_RETRY;

private SocketFlush socketFlush = DEFAULT_SOCKET_FLUSH;

/**
* Number of exceptions trying to send message.
*/
Expand All @@ -97,7 +106,9 @@ public synchronized void sendMessage(@Nonnull SyslogMessage message) throws IOEx
ensureSyslogServerConnection();
message.toSyslogMessage(messageFormat, writer);
writer.write(postfix);
writer.flush();
if (socketFlush == SocketFlush.onSend) {
writer.flush();
}
return;
} catch (IOException e) {
lastException = e;
Expand Down Expand Up @@ -265,6 +276,14 @@ public synchronized void setPostfix(String postfix) {
this.postfix = postfix;
}

public SocketFlush getSocketFlush() {
return socketFlush;
}

public void setSocketFlush(SocketFlush socketFlush) {
this.socketFlush = socketFlush;
}

@Override
public String toString() {
return getClass().getName() + "{" +
Expand All @@ -282,11 +301,17 @@ public String toString() {
", sendDurationInNanosCounter=" + sendDurationInNanosCounter +
", sendErrorCounter=" + sendErrorCounter +
", trySendErrorCounter=" + trySendErrorCounter +
", socketFlush=" + socketFlush +
'}';
}

@Override
public void close() throws IOException {
this.socket.close();
if ((socketFlush == SocketFlush.onClose) && (writer != null)) {
// close will flush first, so if an exception ocurrs, data will still be passed to socket
writer.close();
}
socket.close();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static void main(String[] args) throws Exception {
// messageSender.setSyslogServerHostname("127.0.0.1");
messageSender.setSyslogServerPort(46022);
messageSender.setSsl(true);
messageSender.setSocketFlush(TcpSyslogMessageSender.SocketFlush.onSend);

final AtomicInteger count = new AtomicInteger();

Expand Down
Loading