package com.commafeed.backend.feeds;

import java.util.Calendar;
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);
				feed.setLastUpdated(Calendar.getInstance().getTime());
			}
			feedDAO.update(feeds);
		}
		return queue.poll();
	}
}

@@ -309,16 +309,83 @@
 .Feed;%0D%0A
+import com.commafeed.backend.services.ApplicationSettingsService;%0D%0A
 import c
@@ -417,16 +417,16 @@
 Lists;%0D%0A
-
 %0D%0A@Singl
@@ -501,16 +501,85 @@
 DAO;%0D%0A%0D%0A
+%09@Inject%0D%0A%09ApplicationSettingsService applicationSettingsService;%0D%0A%0D%0A
 %09private
@@ -732,16 +732,23 @@
  feedDAO
+%0D%0A%09%09%09%09%09
 .findNex
@@ -760,16 +760,83 @@
 table(30
+ * applicationSettingsService.get()%0D%0A%09%09%09%09%09%09%09.getBackgroundThreads()
 );%0D%0A%09%09%09f

scale based on number of background threads