All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.cluster.callback.AutoCancelReconnectCallback Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.cluster.callback;

import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.endpoint.client.ResponseWait;

import java.util.concurrent.TimeUnit;

public class AutoCancelReconnectCallback implements ClientReadyCallback {
//    static Logger LOGGER = LogManager.getLogger(AutoCancelReconnectCallback.class);
    private final ClientReadyCallback originalCallback;
    Timeout timeout;

    public AutoCancelReconnectCallback(AuditDoipClient doipClient, ClientReadyCallback original, int numOfSeconds) {
        this.originalCallback = original;
        TimerTask tt = timeout -> {
            AutoCancelReconnectCallback.this.timeout = null;
            if (doipClient.rwLock.availablePermits() <= 0)
                doipClient.rwLock.release();
            if (originalCallback != null)
                originalCallback.onReady(doipClient);
        };
        timeout = ResponseWait.HASHED_WHEEL_TIMER.newTimeout(tt, numOfSeconds, TimeUnit.SECONDS);
    }

    @Override
    public void onReady(AuditDoipClient result) {
        // Already timeout is var timeout is null!
        if (timeout != null) {
            timeout.cancel();
            if (originalCallback != null)
                originalCallback.onReady(result);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy