com.qmetric.feed.consumer.FeedConsumerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hal-feed-consumer Show documentation
Show all versions of hal-feed-consumer Show documentation
Java library for consuming HAL+JSON feeds (https://github.com/qmetric/hal-feed-server)
package com.qmetric.feed.consumer;
import com.qmetric.feed.consumer.store.AlreadyConsumingException;
import com.qmetric.feed.consumer.store.FeedTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.List;
import static com.google.common.collect.FluentIterable.from;
public class FeedConsumerImpl implements FeedConsumer
{
private static final Logger LOG = LoggerFactory.getLogger(FeedConsumerImpl.class);
private final EntryConsumer entryConsumer;
private final FeedTracker feedTracker;
private final Collection listeners;
public FeedConsumerImpl(final EntryConsumer entryConsumer, final FeedTracker feedTracker, final Collection listeners)
{
this.entryConsumer = entryConsumer;
this.feedTracker = feedTracker;
this.listeners = listeners;
}
@Override
public List consume() throws Exception
{
return consume(unconsumed());
}
private List consume(final List entries) throws Exception
{
processEach(entries);
notifyAllListeners(entries);
return entries;
}
private void processEach(final List entries) throws Exception
{
for (final TrackedEntry trackedEntry : entries)
{
try
{
LOG.debug("Consuming entry {}", trackedEntry);
entryConsumer.consume(trackedEntry);
}
catch (AlreadyConsumingException e)
{
LOG.info("Entry {} already being consumed", trackedEntry, e);
}
catch (Exception e)
{
LOG.warn("Entry {} failed processing", trackedEntry, e);
}
catch (Throwable e)
{
LOG.error("Fatal error processing entry {}", trackedEntry, e);
}
}
}
private List unconsumed()
{
return from(feedTracker.getEntriesToBeConsumed()).toList();
}
private void notifyAllListeners(final List consumedEntries)
{
for (final FeedPollingListener listener : listeners)
{
listener.consumed(consumedEntries);
}
}
}