From 4a4b213bd4d0a55003fc286a6c70a5c3e7623364 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 27 Oct 2010 08:20:21 -0400 Subject: [PATCH] polishing --- .../feed/FeedEntryReaderMessageSource.java | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java b/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java index da858bd2d2..e3b22ad1e4 100644 --- a/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java +++ b/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java @@ -31,6 +31,7 @@ import org.springframework.integration.context.metadata.SimpleMetadataStore; import org.springframework.integration.core.MessageSource; import org.springframework.integration.support.MessageBuilder; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import com.sun.syndication.feed.synd.SyndEntry; @@ -111,34 +112,20 @@ public class FeedEntryReaderMessageSource extends IntegrationObjectSupport imple this.initialized = true; } - @SuppressWarnings("unchecked") private SyndEntry doReceive() { - SyndEntry nextUp = null; + SyndEntry nextEntry = null; synchronized (this.monitor) { - nextUp = pollAndCache(); - if (nextUp != null) { - return nextUp; + nextEntry = getNextEntry(); + if (nextEntry == null) { + // read feed and try again + this.populateEntryList(); + nextEntry = getNextEntry(); } - // otherwise, fill the backlog - SyndFeed syndFeed = this.feedReaderMessageSource.receiveSyndFeed(); - if (syndFeed != null) { - List feedEntries = (List) syndFeed.getEntries(); - if (null != feedEntries) { - Collections.sort(feedEntries, syndEntryComparator); - for (SyndEntry se : feedEntries) { - long publishedTime = se.getPublishedDate().getTime(); - if (publishedTime > this.lastTime) { - entries.add(se); - } - } - } - } - nextUp = pollAndCache(); } - return nextUp; + return nextEntry; } - private SyndEntry pollAndCache() { + private SyndEntry getNextEntry() { SyndEntry next = this.entries.poll(); if (next == null) { return null; @@ -148,6 +135,22 @@ public class FeedEntryReaderMessageSource extends IntegrationObjectSupport imple return next; } + @SuppressWarnings("unchecked") + private void populateEntryList() { + SyndFeed syndFeed = this.feedReaderMessageSource.receiveSyndFeed(); + if (syndFeed != null) { + List retrievedEntries = (List) syndFeed.getEntries(); + if (!CollectionUtils.isEmpty(retrievedEntries)) { + Collections.sort(retrievedEntries, this.syndEntryComparator); + for (SyndEntry entry : retrievedEntries) { + if (entry.getPublishedDate().getTime() > this.lastTime) { + this.entries.add(entry); + } + } + } + } + } + private static class SyndEntryComparator implements Comparator {