org.bdware.doip.cluster.entity.ReconnectReqPack Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
package org.bdware.doip.cluster.entity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.cluster.util.AuditDoipClientCacheUtil;
import org.bdware.doip.cluster.callback.ClientReadyCallback;
import org.bdware.doip.cluster.util.ConnectionUtil;
public class ReconnectReqPack extends RequestPackIntf {
static Logger LOGGER = LogManager.getLogger(ReconnectReqPack.class);
private final String address;
private final String version;
private final ClientReadyCallback callback;
public ReconnectReqPack(String address, String version, ClientReadyCallback callback) {
this.address = address;
this.version = version;
this.callback = callback;
}
public long getPriority() {
return 100;
}
@Override
public void run() {
if (address == null && version == null) callback.onReady(null);
AuditDoipClient doipClientImpl = AuditDoipClientCacheUtil.clientCache.get(address);
if (doipClientImpl == null) {
// Create the client only if it doesn't exist in the map
doipClientImpl = AuditDoipClientCacheUtil.createWithoutConnect(address);
// Use putIfAbsent() to ensure thread-safety
AuditDoipClient existingClient = AuditDoipClientCacheUtil.clientCache.putIfAbsent(address, doipClientImpl);
if (existingClient != null) {
// If another thread added a client while we were creating ours, use that instead
doipClientImpl = existingClient;
}
}
if (doipClientImpl.isConnected()) {
callback.onReady(doipClientImpl);
return;
}
try {
// Always try to reconnect, regardless of whether the client was just created or already existed
ConnectionUtil.tryReconnect(doipClientImpl, 2000, callback);
} catch (Exception e) {
e.printStackTrace();
}
}
}