2013-04-26 18:53:48 +02:00
|
|
|
package com.commafeed.backend.feeds;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2013-05-22 00:07:13 +02:00
|
|
|
import java.util.List;
|
2013-05-22 21:57:53 +02:00
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-04-26 18:53:48 +02:00
|
|
|
|
2013-05-22 21:57:53 +02:00
|
|
|
import javax.annotation.PostConstruct;
|
2013-04-26 18:53:48 +02:00
|
|
|
import javax.inject.Inject;
|
2013-05-22 09:34:24 +02:00
|
|
|
import javax.inject.Singleton;
|
2013-04-26 18:53:48 +02:00
|
|
|
|
2013-05-20 14:06:09 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2013-04-26 18:53:48 +02:00
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
import com.commafeed.backend.LockMap;
|
2013-04-26 18:53:48 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
2013-05-22 00:07:13 +02:00
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2013-05-22 21:57:53 +02:00
|
|
|
import com.commafeed.backend.model.ApplicationSettings;
|
2013-04-26 18:53:48 +02:00
|
|
|
import com.commafeed.backend.model.Feed;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
2013-05-20 14:06:09 +02:00
|
|
|
import com.commafeed.backend.model.FeedPushInfo;
|
2013-05-22 00:07:13 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-05-20 14:06:09 +02:00
|
|
|
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
|
2013-05-21 07:09:48 +02:00
|
|
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
2013-04-26 18:53:48 +02:00
|
|
|
import com.commafeed.backend.services.FeedUpdateService;
|
|
|
|
|
|
2013-05-22 09:34:24 +02:00
|
|
|
@Singleton
|
2013-04-26 18:53:48 +02:00
|
|
|
public class FeedRefreshUpdater {
|
|
|
|
|
|
2013-05-20 14:06:09 +02:00
|
|
|
protected static Logger log = LoggerFactory
|
|
|
|
|
.getLogger(FeedRefreshUpdater.class);
|
|
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
private static LockMap<String> lockMap = new LockMap<String>();
|
|
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedUpdateService feedUpdateService;
|
|
|
|
|
|
2013-05-20 14:06:09 +02:00
|
|
|
@Inject
|
|
|
|
|
SubscriptionHandler handler;
|
|
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedRefreshTaskGiver taskGiver;
|
|
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedDAO feedDAO;
|
|
|
|
|
|
2013-05-21 07:09:48 +02:00
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
|
2013-05-22 21:57:53 +02:00
|
|
|
private ThreadPoolExecutor pool;
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
ApplicationSettings settings = applicationSettingsService.get();
|
2013-05-23 06:38:58 +02:00
|
|
|
int threads = Math.max(settings.getDatabaseUpdateThreads(), 1);
|
2013-05-22 21:57:53 +02:00
|
|
|
pool = new ThreadPoolExecutor(threads, threads, 0,
|
|
|
|
|
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
|
|
|
|
|
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy() {
|
|
|
|
|
@Override
|
|
|
|
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
|
|
|
|
|
log.info("Thread queue full, executing in own thread.");
|
2013-05-23 08:19:05 +02:00
|
|
|
super.rejectedExecution(r, e);
|
2013-05-21 17:00:37 +02:00
|
|
|
}
|
2013-05-22 21:57:53 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateFeed(Feed feed, Collection<FeedEntry> entries) {
|
|
|
|
|
pool.execute(new Task(feed, entries));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class Task implements Runnable {
|
|
|
|
|
|
|
|
|
|
private Feed feed;
|
|
|
|
|
private Collection<FeedEntry> entries;
|
|
|
|
|
|
|
|
|
|
public Task(Feed feed, Collection<FeedEntry> entries) {
|
|
|
|
|
this.feed = feed;
|
|
|
|
|
this.entries = entries;
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|
2013-05-22 00:07:13 +02:00
|
|
|
|
2013-05-22 21:57:53 +02:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
if (entries != null) {
|
|
|
|
|
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
|
|
|
|
.findByFeed(feed);
|
|
|
|
|
for (FeedEntry entry : entries) {
|
|
|
|
|
updateEntry(feed, entry, subscriptions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (applicationSettingsService.get().isPubsubhubbub()) {
|
|
|
|
|
handlePubSub(feed);
|
|
|
|
|
}
|
|
|
|
|
taskGiver.giveBack(feed);
|
2013-05-21 07:09:48 +02:00
|
|
|
}
|
2013-05-22 21:57:53 +02:00
|
|
|
|
2013-05-20 14:06:09 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
private void updateEntry(Feed feed, FeedEntry entry,
|
|
|
|
|
List<FeedSubscription> subscriptions) {
|
|
|
|
|
synchronized (lockMap.get(entry.getGuid())) {
|
|
|
|
|
feedUpdateService.updateEntry(feed, entry, subscriptions);
|
2013-05-21 17:00:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 12:41:57 +02:00
|
|
|
private void handlePubSub(final Feed feed) {
|
2013-05-20 14:06:09 +02:00
|
|
|
FeedPushInfo info = feed.getPushInfo();
|
|
|
|
|
if (info != null && info.isActive() == false) {
|
2013-05-21 12:41:57 +02:00
|
|
|
new Thread() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
handler.subscribe(feed);
|
|
|
|
|
}
|
|
|
|
|
}.start();
|
2013-05-20 14:06:09 +02:00
|
|
|
}
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|