forked from Archives/Athou_commafeed
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:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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,63 +36,58 @@ 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);
|
||||
List<FeedEntry> existingEntries = getExistingEntries(entries);
|
||||
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
||||
.findByFeed(feed);
|
||||
|
||||
List<FeedEntry> entryUpdateList = Lists.newArrayList();
|
||||
List<FeedEntryStatus> statusUpdateList = Lists.newArrayList();
|
||||
for (FeedEntry entry : entries) {
|
||||
List<FeedEntry> entryUpdateList = Lists.newArrayList();
|
||||
List<FeedEntryStatus> statusUpdateList = Lists.newArrayList();
|
||||
for (FeedEntry entry : entries) {
|
||||
|
||||
FeedEntry foundEntry = findEntry(existingEntries, entry);
|
||||
FeedEntry foundEntry = findEntry(existingEntries, entry);
|
||||
|
||||
if (foundEntry == null) {
|
||||
FeedEntryContent content = entry.getContent();
|
||||
if (foundEntry == null) {
|
||||
FeedEntryContent content = entry.getContent();
|
||||
|
||||
content.setContent(FeedUtils.handleContent(content
|
||||
.getContent()));
|
||||
String title = FeedUtils.handleContent(content.getTitle());
|
||||
if (title != null) {
|
||||
content.setTitle(title.substring(0,
|
||||
Math.min(2048, title.length())));
|
||||
}
|
||||
content.setContent(FeedUtils.handleContent(content.getContent()));
|
||||
String title = FeedUtils.handleContent(content.getTitle());
|
||||
if (title != null) {
|
||||
content.setTitle(title.substring(0,
|
||||
Math.min(2048, title.length())));
|
||||
}
|
||||
|
||||
entry.setInserted(Calendar.getInstance().getTime());
|
||||
entry.getFeeds().add(feed);
|
||||
entryUpdateList.add(entry);
|
||||
} else {
|
||||
boolean foundFeed = false;
|
||||
for (Feed existingFeed : foundEntry.getFeeds()) {
|
||||
if (ObjectUtils.equals(existingFeed.getId(),
|
||||
feed.getId())) {
|
||||
foundFeed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundFeed) {
|
||||
foundEntry.getFeeds().add(feed);
|
||||
entryUpdateList.add(foundEntry);
|
||||
entry.setInserted(Calendar.getInstance().getTime());
|
||||
entry.getFeeds().add(feed);
|
||||
entryUpdateList.add(entry);
|
||||
} else {
|
||||
boolean foundFeed = false;
|
||||
for (Feed existingFeed : foundEntry.getFeeds()) {
|
||||
if (ObjectUtils.equals(existingFeed.getId(), feed.getId())) {
|
||||
foundFeed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (FeedEntry entry : entryUpdateList) {
|
||||
for (FeedSubscription sub : subscriptions) {
|
||||
FeedEntryStatus status = new FeedEntryStatus();
|
||||
status.setEntry(entry);
|
||||
status.setSubscription(sub);
|
||||
statusUpdateList.add(status);
|
||||
|
||||
if (!foundFeed) {
|
||||
foundEntry.getFeeds().add(feed);
|
||||
entryUpdateList.add(foundEntry);
|
||||
}
|
||||
}
|
||||
|
||||
feedEntryDAO.saveOrUpdate(entryUpdateList);
|
||||
feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
|
||||
}
|
||||
feedDAO.update(feed);
|
||||
for (FeedEntry entry : entryUpdateList) {
|
||||
for (FeedSubscription sub : subscriptions) {
|
||||
FeedEntryStatus status = new FeedEntryStatus();
|
||||
status.setEntry(entry);
|
||||
status.setSubscription(sub);
|
||||
statusUpdateList.add(status);
|
||||
}
|
||||
}
|
||||
|
||||
feedEntryDAO.saveOrUpdate(entryUpdateList);
|
||||
feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
|
||||
}
|
||||
|
||||
private FeedEntry findEntry(List<FeedEntry> existingEntries, FeedEntry entry) {
|
||||
|
||||
Reference in New Issue
Block a user