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

com.sportradar.livedata.sdk.proto.livescout.LiveScoutClientAliveProducer Maven / Gradle / Ivy

Go to download

Livedata SDK is a client library that enables easier integration with the Livedata XML feed. SDK exposes XML feed service interface in a more user-friendly way and isolates the client from having to do XML feed parsing, proper connection handling, error recovery, event queuing and dispatching. It also makes a client solution more stable and robust when it comes to feed handling, especially with the release of new and updated XML feed versions.

There is a newer version: 2.0.10
Show newest version
/***************************************************************
 * Copyright (c) 2013, Sportradar AG                           *
 ***************************************************************/

package com.sportradar.livedata.sdk.proto.livescout;

import com.sportradar.livedata.sdk.proto.common.RequestProducer;
import com.sportradar.livedata.sdk.proto.common.RequestProducerBase;
import com.sportradar.livedata.sdk.proto.dto.OutgoingMessage;
import org.joda.time.Duration;

import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkArgument;
import static com.sportradar.livedata.sdk.common.classes.Nulls.checkNotNull;

/**
 * A {@link RequestProducer} for LiveScout that produces alive messages.
 */
public class LiveScoutClientAliveProducer extends RequestProducerBase {

    private final ScheduledExecutorService executor;
    private final Duration interval;
    private final LiveScoutStatusFactory entityFactory;

    private Future timerCancel;

    public LiveScoutClientAliveProducer(
            final LiveScoutStatusFactory entityFactory,
            final ScheduledExecutorService executor,
            final Duration interval) {

        checkNotNull(entityFactory, "entityFactory cannot be a null reference");
        checkNotNull(executor, "executor cannot be a null reference");
        checkNotNull(interval, "interval cannot be a null reference");
        checkArgument(interval.getMillis() > 0, "interval cannot be negative");

        this.entityFactory = entityFactory;
        this.executor = executor;
        this.interval = interval;
    }

    private void produceAlive() {
        notifyOnRequestReady(entityFactory.buildAlive());
    }

    /**
     * Start invoking ClientAliveProducerListener events
     */
    @Override
    protected void onStarting() {
        timerCancel = executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                produceAlive();
            }
        }, interval.getMillis(), interval.getMillis(), TimeUnit.MILLISECONDS);
    }

    /**
     * Stop invoking ClientAliveProducerListener events
     */
    @Override
    protected void onStopping() {
        if (timerCancel != null) {
            timerCancel.cancel(false);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy