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

io.vlingo.common.completes.operations.TimeoutGateway Maven / Gradle / Ivy

There is a newer version: 1.7.5
Show newest version
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.common.completes.operations;

import io.vlingo.common.Cancellable;
import io.vlingo.common.Scheduled;
import io.vlingo.common.Scheduler;
import io.vlingo.common.completes.Operation;

import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

public class TimeoutGateway extends Operation implements Scheduled {
    private final Scheduler scheduler;
    private final long timeout;
    private Cancellable cancellable;
    private AtomicBoolean didTimeout;

    public TimeoutGateway(Scheduler scheduler, long timeout) {
        this.scheduler = scheduler;
        this.timeout = timeout;
        this.didTimeout = new AtomicBoolean(false);
        startTimer();
    }

    @Override
    public void onOutcome(Receives receives) {
        if (!didTimeout.get()) {
            this.cancellable.cancel();
            emitOutcome(receives);
            startTimer();
        }
    }

    @Override
    public void intervalSignal(Scheduled scheduled, Void data) {
        emitError(new TimeoutException());
        didTimeout.set(true);
    }

    private void startTimer() {
        this.cancellable = scheduler.scheduleOnce(this, null, 0, timeout);
        this.didTimeout.set(false);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy