com.alachisoft.ncache.client.internal.communication.ConnectionKeepAlive Maven / Gradle / Ivy
package com.alachisoft.ncache.client.internal.communication;
import com.alachisoft.ncache.client.internal.command.CommandResponse;
import com.alachisoft.ncache.client.internal.command.PingCommand;
final class ConnectionKeepAlive {
boolean isThreadStop = false;
private int _interval;
private Broker _enclosure;
private java.util.HashSet _idleConnections;
private Thread _activityPerformer;
public ConnectionKeepAlive(Broker encloser, int interval) {
_enclosure = encloser;
_interval = interval;
_idleConnections = new java.util.HashSet();
}
public void Start() {
if (_activityPerformer != null) {
return;
}
_activityPerformer = new Thread(() -> Run());
_activityPerformer.setDaemon(true);
_activityPerformer.setName(String.join(_enclosure.getCache().getName(), ":KeepAlivePerformer"));
_activityPerformer.start();
}
public void Stop() {
if (_activityPerformer == null) {
return;
}
isThreadStop = true;
_activityPerformer.interrupt();
_activityPerformer = null;
}
private void Run() {
try {
while (!isThreadStop) {
MarkConnectionsIdle();
Thread.sleep(_interval * 1000);
PopulateIdleList();
PingIdleList();
}
} catch (InterruptedException e) {
if (_enclosure.getLogger() != null && _enclosure.getLogger().getIsDetailedLogsEnabled()) {
_enclosure.getLogger().getNCacheLog().Info("Broker.ConnectionKeepAlive.Run", " The activity performing thread is stopping...");
}
} catch (RuntimeException ex) {
if (_enclosure.getLogger() != null && _enclosure.getLogger().getIsErrorLogsEnabled()) {
_enclosure.getLogger().getNCacheLog().Error("Broker.ConnectionKeepAlive.Run", " The activity performing thread has been terminated due to the problem: " + ex);
}
}
}
private void MarkConnectionsIdle() {
if (_enclosure.getConnection() != null) {
_enclosure.getConnection().setIsIdle(true);
}
if (_enclosure.getPool() != null) {
_enclosure.getPool().MarkConnectionsIdle();
}
}
private void PopulateIdleList() {
if (_enclosure.getConnection() != null && _enclosure.getConnection().getIsIdle()) {
_idleConnections.add(_enclosure.getConnection());
}
if (_enclosure.getPool() != null) {
for (Connection connection : _enclosure.getPool().GetIdleConnections()) {
if (!_idleConnections.contains(connection)) {
_idleConnections.add(connection);
}
}
}
}
private void PingIdleList() {
for (Connection connection : _idleConnections) {
SendPing(connection, false, true, false);
}
_idleConnections.clear();
}
private void SendPing(Connection connection, boolean waitforReponse, boolean hasResponse, boolean isAsync) {
try {
if (_enclosure.getLogger() != null && _enclosure.getLogger().getIsDetailedLogsEnabled()) {
_enclosure.getLogger().getNCacheLog().Info("Broker.ConnectionKeepAlive.SendPing", "Sending PingCommand to Server: " + connection.getIpAddress());
}
PingCommand command = new PingCommand(hasResponse, isAsync);
Request request = new Request(false, _enclosure.getOperationTimeout());
request.addCommand(connection.getServerAddress(), command);
_enclosure.executeRequest(request, connection, true, waitforReponse);
if (waitforReponse) {
CommandResponse reponse = request.getResponse();
reponse.parseResponse();
}
} catch (Exception ex) {
if (_enclosure.getLogger() != null && _enclosure.getLogger().getIsErrorLogsEnabled()) {
_enclosure.getLogger().getNCacheLog().Error("Broker.ConnectionKeepAlive.SendPing", "Problem occured: " + ex);
}
}
}
}