forked from Archives/Athou_commafeed
smarter way of getting the next feed to update
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.commafeed.backend.feeds;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
import javax.ejb.Lock;
|
||||
import javax.ejb.LockType;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@Singleton
|
||||
public class FeedRefreshTaskGiver {
|
||||
|
||||
@Inject
|
||||
FeedDAO feedDAO;
|
||||
|
||||
private Queue<Feed> queue = Lists.newLinkedList();
|
||||
|
||||
@Lock(LockType.WRITE)
|
||||
public Feed take() {
|
||||
if (queue.peek() == null) {
|
||||
List<Feed> feeds = feedDAO.findNextUpdatable(30);
|
||||
for (Feed feed : feeds) {
|
||||
queue.add(feed);
|
||||
}
|
||||
}
|
||||
return queue.poll();
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,9 @@ public class FeedRefreshWorker {
|
||||
@Inject
|
||||
FeedUpdateService feedUpdateService;
|
||||
|
||||
@Inject
|
||||
FeedRefreshTaskGiver taskGiver;
|
||||
|
||||
@Resource
|
||||
private UserTransaction transaction;
|
||||
|
||||
@@ -117,22 +120,7 @@ public class FeedRefreshWorker {
|
||||
|
||||
}
|
||||
|
||||
private Feed getNextFeed() throws NotSupportedException, SystemException,
|
||||
SecurityException, IllegalStateException, RollbackException,
|
||||
HeuristicMixedException, HeuristicRollbackException {
|
||||
|
||||
Feed feed = null;
|
||||
lock.lock();
|
||||
try {
|
||||
feed = Iterables.getFirst(feedDAO.findNextUpdatable(1), null);
|
||||
if (feed != null) {
|
||||
feed.setLastUpdated(Calendar.getInstance().getTime());
|
||||
feedDAO.update(feed);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
return feed;
|
||||
private Feed getNextFeed() {
|
||||
return taskGiver.take();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user