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

com.qmetric.feed.consumer.EntryConsumerImpl Maven / Gradle / Ivy

Go to download

Java library for consuming HAL+JSON feeds (https://github.com/qmetric/hal-feed-server)

There is a newer version: 3.18
Show newest version
package com.qmetric.feed.consumer;

import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.RetryerBuilder;
import com.google.common.base.Optional;
import com.qmetric.feed.consumer.store.AlreadyConsumingException;
import com.qmetric.feed.consumer.store.FeedTracker;

import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import static com.github.rholder.retry.StopStrategies.stopAfterAttempt;
import static com.github.rholder.retry.WaitStrategies.fixedWait;
import static com.qmetric.feed.consumer.Result.*;
import static java.util.concurrent.TimeUnit.SECONDS;

public class EntryConsumerImpl implements EntryConsumer
{
    private static final RetryerBuilder RETRY_BUILDER = RetryerBuilder.newBuilder() //
            .retryIfException() //
            .withWaitStrategy(fixedWait(1, SECONDS)) //
            .withStopStrategy(stopAfterAttempt(60));

    private final FeedTracker feedTracker;

    private final ConsumeAction consumeAction;

    private final ResourceResolver resourceResolver;

    private final Collection listeners;

    private final Optional maxRetries;

    public EntryConsumerImpl(final FeedTracker feedTracker, final ConsumeAction consumeAction, final ResourceResolver resourceResolver,
                             final Collection listeners, final Optional maxRetries)
    {
        this.feedTracker = feedTracker;
        this.consumeAction = consumeAction;
        this.resourceResolver = resourceResolver;
        this.listeners = listeners;
        this.maxRetries = maxRetries;
    }

    @Override
    public boolean consume(final TrackedEntry trackedEntry) throws Exception
    {
        markAsConsuming(trackedEntry);

        final boolean success = process(trackedEntry);

        if (success)
        {
            markAsConsumed(trackedEntry);

            notifyAllListeners(trackedEntry);
        }

        return success;
    }

    private void markAsConsuming(final TrackedEntry trackedEntry) throws AlreadyConsumingException
    {
        feedTracker.markAsConsuming(trackedEntry.id);
    }

    private boolean process(final TrackedEntry trackedEntry) throws Exception
    {
        try
        {
            final Result result = consumeAction.consume(fetchFeedEntry(trackedEntry));
            if (result.failure())
            {
                feedTracker.fail(trackedEntry, result.state == State.RETRY_UNSUCCESSFUL);
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (final Throwable e)
        {
            fail(trackedEntry);
            throw new Exception(e);
        }
    }

    private void fail(final TrackedEntry trackedEntry)
    {
        final boolean scheduleRetry = !maxRetries.isPresent() || maxRetries.get() > trackedEntry.retries;

        feedTracker.fail(trackedEntry, scheduleRetry);
    }

    private FeedEntry fetchFeedEntry(final TrackedEntry trackedEntry)
    {
        return new FeedEntry(resourceResolver.resolve(trackedEntry.id), trackedEntry.retries);
    }

    private void markAsConsumed(final TrackedEntry trackedEntry) throws ExecutionException, RetryException
    {
        RETRY_BUILDER.build().call(new Callable()
        {
            @Override public Void call() throws Exception
            {
                feedTracker.markAsConsumed(trackedEntry.id);
                return null;
            }
        });
    }

    private void notifyAllListeners(final TrackedEntry trackingEntry)
    {
        for (final EntryConsumerListener listener : listeners)
        {
            listener.consumed(trackingEntry.id);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy