only allow one thread to update entries for a feed, avoids duplicates if two feeds containing the same entry are updating at the same time

This commit is contained in:
Athou
2013-04-26 18:53:48 +02:00
parent aa3baf5007
commit 56ba907ec5
3 changed files with 79 additions and 56 deletions

View File

@@ -0,0 +1,33 @@
package com.commafeed.backend.feeds;
import java.util.Collection;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.apache.commons.collections.CollectionUtils;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.services.FeedUpdateService;
@Stateless
public class FeedRefreshUpdater {
@Inject
FeedUpdateService feedUpdateService;
@Inject
FeedDAO feedDAO;
@Asynchronous
public void updateEntries(Feed feed, Collection<FeedEntry> entries) {
if (CollectionUtils.isNotEmpty(entries)) {
feedUpdateService.updateEntries(feed, entries);
}
feedDAO.update(feed);
}
}

View File

@@ -20,7 +20,6 @@ import org.slf4j.LoggerFactory;
import com.commafeed.backend.HttpGetter.NotModifiedException; import com.commafeed.backend.HttpGetter.NotModifiedException;
import com.commafeed.backend.model.Feed; import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry; import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.services.FeedUpdateService;
import com.sun.syndication.io.FeedException; import com.sun.syndication.io.FeedException;
public class FeedRefreshWorker { public class FeedRefreshWorker {
@@ -29,7 +28,7 @@ public class FeedRefreshWorker {
.getLogger(FeedRefreshWorker.class); .getLogger(FeedRefreshWorker.class);
@Inject @Inject
FeedUpdateService feedUpdateService; FeedRefreshUpdater feedRefreshUpdater;
@Inject @Inject
FeedFetcher fetcher; FeedFetcher fetcher;
@@ -127,7 +126,7 @@ public class FeedRefreshWorker {
feed.setEtagHeader(fetchedFeed.getFeed().getEtagHeader()); feed.setEtagHeader(fetchedFeed.getFeed().getEtagHeader());
entries = fetchedFeed.getEntries(); entries = fetchedFeed.getEntries();
} }
feedUpdateService.updateEntries(feed, entries); feedRefreshUpdater.updateEntries(feed, entries);
} }

View File

@@ -5,15 +5,14 @@ import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import javax.ejb.Asynchronous; import javax.ejb.Lock;
import javax.ejb.Stateless; import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedEntryDAO; import com.commafeed.backend.dao.FeedEntryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO; import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO; import com.commafeed.backend.dao.FeedSubscriptionDAO;
@@ -25,12 +24,9 @@ import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription; import com.commafeed.backend.model.FeedSubscription;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@Stateless @Singleton
public class FeedUpdateService { public class FeedUpdateService {
@Inject
FeedDAO feedDAO;
@Inject @Inject
FeedSubscriptionDAO feedSubscriptionDAO; FeedSubscriptionDAO feedSubscriptionDAO;
@@ -40,10 +36,9 @@ public class FeedUpdateService {
@Inject @Inject
FeedEntryStatusDAO feedEntryStatusDAO; FeedEntryStatusDAO feedEntryStatusDAO;
@Asynchronous @Lock(LockType.WRITE)
public void updateEntries(Feed feed, Collection<FeedEntry> entries) { public void updateEntries(Feed feed, Collection<FeedEntry> entries) {
if (CollectionUtils.isNotEmpty(entries)) {
List<FeedEntry> existingEntries = getExistingEntries(entries); List<FeedEntry> existingEntries = getExistingEntries(entries);
List<FeedSubscription> subscriptions = feedSubscriptionDAO List<FeedSubscription> subscriptions = feedSubscriptionDAO
.findByFeed(feed); .findByFeed(feed);
@@ -57,8 +52,7 @@ public class FeedUpdateService {
if (foundEntry == null) { if (foundEntry == null) {
FeedEntryContent content = entry.getContent(); FeedEntryContent content = entry.getContent();
content.setContent(FeedUtils.handleContent(content content.setContent(FeedUtils.handleContent(content.getContent()));
.getContent()));
String title = FeedUtils.handleContent(content.getTitle()); String title = FeedUtils.handleContent(content.getTitle());
if (title != null) { if (title != null) {
content.setTitle(title.substring(0, content.setTitle(title.substring(0,
@@ -71,8 +65,7 @@ public class FeedUpdateService {
} else { } else {
boolean foundFeed = false; boolean foundFeed = false;
for (Feed existingFeed : foundEntry.getFeeds()) { for (Feed existingFeed : foundEntry.getFeeds()) {
if (ObjectUtils.equals(existingFeed.getId(), if (ObjectUtils.equals(existingFeed.getId(), feed.getId())) {
feed.getId())) {
foundFeed = true; foundFeed = true;
break; break;
} }
@@ -96,8 +89,6 @@ public class FeedUpdateService {
feedEntryDAO.saveOrUpdate(entryUpdateList); feedEntryDAO.saveOrUpdate(entryUpdateList);
feedEntryStatusDAO.saveOrUpdate(statusUpdateList); feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
} }
feedDAO.update(feed);
}
private FeedEntry findEntry(List<FeedEntry> existingEntries, FeedEntry entry) { private FeedEntry findEntry(List<FeedEntry> existingEntries, FeedEntry entry) {
FeedEntry foundEntry = null; FeedEntry foundEntry = null;