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.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.services.FeedUpdateService;
import com.sun.syndication.io.FeedException;
public class FeedRefreshWorker {
@@ -29,7 +28,7 @@ public class FeedRefreshWorker {
.getLogger(FeedRefreshWorker.class);
@Inject
FeedUpdateService feedUpdateService;
FeedRefreshUpdater feedRefreshUpdater;
@Inject
FeedFetcher fetcher;
@@ -127,7 +126,7 @@ public class FeedRefreshWorker {
feed.setEtagHeader(fetchedFeed.getFeed().getEtagHeader());
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.List;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.inject.Inject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedEntryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO;
@@ -25,12 +24,9 @@ import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.google.common.collect.Lists;
@Stateless
@Singleton
public class FeedUpdateService {
@Inject
FeedDAO feedDAO;
@Inject
FeedSubscriptionDAO feedSubscriptionDAO;
@@ -40,10 +36,9 @@ public class FeedUpdateService {
@Inject
FeedEntryStatusDAO feedEntryStatusDAO;
@Asynchronous
@Lock(LockType.WRITE)
public void updateEntries(Feed feed, Collection<FeedEntry> entries) {
if (CollectionUtils.isNotEmpty(entries)) {
List<FeedEntry> existingEntries = getExistingEntries(entries);
List<FeedSubscription> subscriptions = feedSubscriptionDAO
.findByFeed(feed);
@@ -57,8 +52,7 @@ public class FeedUpdateService {
if (foundEntry == null) {
FeedEntryContent content = entry.getContent();
content.setContent(FeedUtils.handleContent(content
.getContent()));
content.setContent(FeedUtils.handleContent(content.getContent()));
String title = FeedUtils.handleContent(content.getTitle());
if (title != null) {
content.setTitle(title.substring(0,
@@ -71,8 +65,7 @@ public class FeedUpdateService {
} else {
boolean foundFeed = false;
for (Feed existingFeed : foundEntry.getFeeds()) {
if (ObjectUtils.equals(existingFeed.getId(),
feed.getId())) {
if (ObjectUtils.equals(existingFeed.getId(), feed.getId())) {
foundFeed = true;
break;
}
@@ -96,8 +89,6 @@ public class FeedUpdateService {
feedEntryDAO.saveOrUpdate(entryUpdateList);
feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
}
feedDAO.update(feed);
}
private FeedEntry findEntry(List<FeedEntry> existingEntries, FeedEntry entry) {
FeedEntry foundEntry = null;