Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.mirth.connect.util.TcpUtil;

import jdk.net.ExtendedSocketOptions;

public class SocketUtil {

private static final Logger logger = LogManager.getLogger(SocketUtil.class);

public static Socket createSocket(TcpConfiguration configuration) throws UnknownHostException, IOException {
return createSocket(configuration, null);
}
Expand Down Expand Up @@ -67,6 +74,20 @@ public static void connectSocket(Socket socket, String host, int port, int timeo
socket.connect(new InetSocketAddress(InetAddress.getByName(TcpUtil.getFixedHost(host)), port), timeout);
}

/** Set TCP keepalive options on a socket */
public static void setKeepAlive(Socket socket) throws SocketException {
socket.setKeepAlive(true);

try {
int keepAliveIdle = Integer.parseInt(System.getProperty("mirth.tcp.keepalive.idle", "120").trim());
if (keepAliveIdle > 0) {
socket.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, keepAliveIdle);
}
} catch (Exception e) {
logger.warn("Failed to set TCP keepalive options on socket.", e);
}
}

public static void closeSocket(Socket socket) throws IOException {
if (socket != null) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ public Response send(ConnectorProperties connectorProperties, ConnectorMessage m
socket.setReceiveBufferSize(bufferSize);
socket.setSendBufferSize(bufferSize);
socket.setSoTimeout(responseTimeout);
socket.setKeepAlive(tcpDispatcherProperties.isKeepConnectionOpen());
if (tcpDispatcherProperties.isKeepConnectionOpen()) {
SocketUtil.setKeepAlive(socket);
}

eventController.dispatchEvent(new ConnectorCountEvent(getChannelId(), getMetaDataId(), getDestinationName(), ConnectionStatusEventType.CONNECTED, SocketUtil.getLocalAddress(socket) + " -> " + SocketUtil.getInetAddress(socket), true));
}
Expand Down Expand Up @@ -810,7 +812,7 @@ private void initSocket(Socket socket) throws SocketException {
socket.setReceiveBufferSize(bufferSize);
socket.setSendBufferSize(bufferSize);
socket.setSoTimeout(responseTimeout);
socket.setKeepAlive(true);
SocketUtil.setKeepAlive(socket);
socket.setReuseAddress(true);
socket.setTcpNoDelay(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,9 @@ private void initSocket(Socket socket) throws SocketException {
socket.setReceiveBufferSize(bufferSize);
socket.setSendBufferSize(bufferSize);
socket.setSoTimeout(timeout);
socket.setKeepAlive(connectorProperties.isKeepConnectionOpen());
if (connectorProperties.isKeepConnectionOpen()) {
SocketUtil.setKeepAlive(socket);
}
socket.setReuseAddress(true);
socket.setTcpNoDelay(true);
}
Expand Down
Loading