forked from Archives/Athou_commafeed
make sure we have something to do before entering synchronized block
This commit is contained in:
@@ -15,6 +15,7 @@ import com.commafeed.backend.model.Feed;
|
|||||||
import com.commafeed.backend.model.FeedEntry;
|
import com.commafeed.backend.model.FeedEntry;
|
||||||
import com.commafeed.backend.model.FeedEntry_;
|
import com.commafeed.backend.model.FeedEntry_;
|
||||||
import com.commafeed.backend.model.Feed_;
|
import com.commafeed.backend.model.Feed_;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
@Stateless
|
@Stateless
|
||||||
public class FeedEntryDAO extends GenericDAO<FeedEntry> {
|
public class FeedEntryDAO extends GenericDAO<FeedEntry> {
|
||||||
@@ -30,6 +31,20 @@ public class FeedEntryDAO extends GenericDAO<FeedEntry> {
|
|||||||
return q.getResultList();
|
return q.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<FeedEntry> findByGuids(List<String> guids) {
|
||||||
|
List<String> hashes = Lists.newArrayList();
|
||||||
|
for (String guid : guids) {
|
||||||
|
hashes.add(DigestUtils.sha1Hex(guid));
|
||||||
|
}
|
||||||
|
|
||||||
|
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
|
||||||
|
Root<FeedEntry> root = query.from(getType());
|
||||||
|
query.where(root.get(FeedEntry_.guidHash).in(hashes));
|
||||||
|
root.fetch(FeedEntry_.feeds, JoinType.LEFT);
|
||||||
|
TypedQuery<FeedEntry> q = em.createQuery(query);
|
||||||
|
return q.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
public List<FeedEntry> findByFeed(Feed feed, int offset, int limit) {
|
public List<FeedEntry> findByFeed(Feed feed, int offset, int limit) {
|
||||||
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
|
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
|
||||||
Root<FeedEntry> root = query.from(getType());
|
Root<FeedEntry> root = query.from(getType());
|
||||||
|
|||||||
@@ -14,12 +14,14 @@ import javax.annotation.PreDestroy;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.commafeed.backend.MetricsBean;
|
import com.commafeed.backend.MetricsBean;
|
||||||
import com.commafeed.backend.dao.FeedDAO;
|
import com.commafeed.backend.dao.FeedDAO;
|
||||||
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||||
import com.commafeed.backend.model.ApplicationSettings;
|
import com.commafeed.backend.model.ApplicationSettings;
|
||||||
import com.commafeed.backend.model.Feed;
|
import com.commafeed.backend.model.Feed;
|
||||||
@@ -29,6 +31,7 @@ import com.commafeed.backend.model.FeedSubscription;
|
|||||||
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
|
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
|
||||||
import com.commafeed.backend.services.ApplicationSettingsService;
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
||||||
import com.commafeed.backend.services.FeedUpdateService;
|
import com.commafeed.backend.services.FeedUpdateService;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.util.concurrent.Striped;
|
import com.google.common.util.concurrent.Striped;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@@ -58,6 +61,9 @@ public class FeedRefreshUpdater {
|
|||||||
@Inject
|
@Inject
|
||||||
FeedSubscriptionDAO feedSubscriptionDAO;
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
FeedEntryDAO feedEntryDAO;
|
||||||
|
|
||||||
private ThreadPoolExecutor pool;
|
private ThreadPoolExecutor pool;
|
||||||
private BlockingQueue<Runnable> queue;
|
private BlockingQueue<Runnable> queue;
|
||||||
private Striped<Lock> locks;
|
private Striped<Lock> locks;
|
||||||
@@ -113,7 +119,7 @@ public class FeedRefreshUpdater {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
boolean ok = true;
|
boolean ok = true;
|
||||||
if (entries != null) {
|
if (CollectionUtils.isNotEmpty(entries) && hasWork()) {
|
||||||
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
||||||
.findByFeed(feed);
|
.findByFeed(feed);
|
||||||
for (FeedEntry entry : entries) {
|
for (FeedEntry entry : entries) {
|
||||||
@@ -131,6 +137,26 @@ public class FeedRefreshUpdater {
|
|||||||
taskGiver.giveBack(feed);
|
taskGiver.giveBack(feed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasWork() {
|
||||||
|
boolean hasWork = false;
|
||||||
|
|
||||||
|
List<String> guids = Lists.newArrayList();
|
||||||
|
for (FeedEntry entry : entries) {
|
||||||
|
guids.add(entry.getGuid());
|
||||||
|
}
|
||||||
|
|
||||||
|
List<FeedEntry> existingEntries = feedEntryDAO.findByGuids(guids);
|
||||||
|
for (FeedEntry entry : entries) {
|
||||||
|
FeedEntry foundEntry = FeedUtils.findEntry(existingEntries,
|
||||||
|
entry);
|
||||||
|
if (foundEntry == null
|
||||||
|
|| FeedUtils.findFeed(foundEntry.getFeeds(), feed) == null) {
|
||||||
|
hasWork = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hasWork;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean updateEntry(final Feed feed, final FeedEntry entry,
|
private boolean updateEntry(final Feed feed, final FeedEntry entry,
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ package com.commafeed.backend.feeds;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
|
import org.apache.commons.lang.ObjectUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.lang3.time.DateUtils;
|
import org.apache.commons.lang3.time.DateUtils;
|
||||||
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
|
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
|
||||||
@@ -19,6 +21,7 @@ import org.mozilla.universalchardet.UniversalDetector;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.commafeed.backend.model.Feed;
|
||||||
import com.commafeed.backend.model.FeedEntry;
|
import com.commafeed.backend.model.FeedEntry;
|
||||||
import com.google.api.client.util.Lists;
|
import com.google.api.client.util.Lists;
|
||||||
|
|
||||||
@@ -93,6 +96,30 @@ public class FeedUtils {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static FeedEntry findEntry(Collection<FeedEntry> list,
|
||||||
|
FeedEntry entry) {
|
||||||
|
FeedEntry found = null;
|
||||||
|
for (FeedEntry e : list) {
|
||||||
|
if (StringUtils.equals(entry.getGuid(), e.getGuid())
|
||||||
|
&& StringUtils.equals(entry.getUrl(), e.getUrl())) {
|
||||||
|
found = e;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Feed findFeed(Collection<Feed> list, Feed feed) {
|
||||||
|
Feed found = null;
|
||||||
|
for (Feed f : list) {
|
||||||
|
if (ObjectUtils.equals(feed.getId(), f.getId())) {
|
||||||
|
found = f;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
public static String trimInvalidXmlCharacters(String xml) {
|
public static String trimInvalidXmlCharacters(String xml) {
|
||||||
if (StringUtils.isBlank(xml)) {
|
if (StringUtils.isBlank(xml)) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -2,14 +2,10 @@ package com.commafeed.backend.services;
|
|||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import org.apache.commons.lang.ObjectUtils;
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
|
|
||||||
import com.commafeed.backend.MetricsBean;
|
import com.commafeed.backend.MetricsBean;
|
||||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||||
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
||||||
@@ -40,7 +36,7 @@ public class FeedUpdateService {
|
|||||||
public void updateEntry(Feed feed, FeedEntry entry,
|
public void updateEntry(Feed feed, FeedEntry entry,
|
||||||
List<FeedSubscription> subscriptions) {
|
List<FeedSubscription> subscriptions) {
|
||||||
|
|
||||||
FeedEntry foundEntry = findEntry(
|
FeedEntry foundEntry = FeedUtils.findEntry(
|
||||||
feedEntryDAO.findByGuid(entry.getGuid()), entry);
|
feedEntryDAO.findByGuid(entry.getGuid()), entry);
|
||||||
|
|
||||||
FeedEntry update = null;
|
FeedEntry update = null;
|
||||||
@@ -56,11 +52,9 @@ public class FeedUpdateService {
|
|||||||
entry.getFeeds().add(feed);
|
entry.getFeeds().add(feed);
|
||||||
|
|
||||||
update = entry;
|
update = entry;
|
||||||
} else {
|
} else if (FeedUtils.findFeed(foundEntry.getFeeds(), feed) == null) {
|
||||||
if (!findFeed(foundEntry.getFeeds(), feed)) {
|
foundEntry.getFeeds().add(feed);
|
||||||
foundEntry.getFeeds().add(feed);
|
update = foundEntry;
|
||||||
update = foundEntry;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (update != null) {
|
if (update != null) {
|
||||||
@@ -77,27 +71,4 @@ public class FeedUpdateService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private FeedEntry findEntry(List<FeedEntry> existingEntries, FeedEntry entry) {
|
|
||||||
FeedEntry found = null;
|
|
||||||
for (FeedEntry existing : existingEntries) {
|
|
||||||
if (StringUtils.equals(entry.getGuid(), existing.getGuid())
|
|
||||||
&& StringUtils.equals(entry.getUrl(), existing.getUrl())) {
|
|
||||||
found = existing;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean findFeed(Set<Feed> feeds, Feed feed) {
|
|
||||||
boolean found = false;
|
|
||||||
for (Feed existingFeed : feeds) {
|
|
||||||
if (ObjectUtils.equals(existingFeed.getId(), feed.getId())) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user