smarter way of stopping things

This commit is contained in:
Athou
2013-04-16 09:29:33 +02:00
parent 2fbd7c3837
commit b6d8072090
3 changed files with 22 additions and 12 deletions

View File

@@ -13,6 +13,7 @@ import javax.ejb.Singleton;
import javax.ejb.Startup; import javax.ejb.Startup;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.commons.lang.mutable.MutableBoolean;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -60,6 +61,8 @@ public class StartupBean {
private long startupTime; private long startupTime;
private MutableBoolean running = new MutableBoolean(true);
@PostConstruct @PostConstruct
private void init() { private void init() {
startupTime = Calendar.getInstance().getTimeInMillis(); startupTime = Calendar.getInstance().getTimeInMillis();
@@ -71,7 +74,7 @@ public class StartupBean {
log.info("Starting {} background threads", log.info("Starting {} background threads",
settings.getBackgroundThreads()); settings.getBackgroundThreads());
for (int i = 0; i < settings.getBackgroundThreads(); i++) { for (int i = 0; i < settings.getBackgroundThreads(); i++) {
Future<Void> thread = worker.start("Thread " + i); Future<Void> thread = worker.start(running, "Thread " + i);
threads.add(thread); threads.add(thread);
} }
@@ -89,10 +92,8 @@ public class StartupBean {
} }
@PreDestroy @PreDestroy
private void shutdown() { public void shutdown() {
for (Future<Void> future : threads) { running.setValue(false);
future.cancel(true);
}
} }
} }

View File

@@ -4,11 +4,13 @@ import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import javax.annotation.PreDestroy;
import javax.ejb.Lock; import javax.ejb.Lock;
import javax.ejb.LockType; import javax.ejb.LockType;
import javax.ejb.Singleton; import javax.ejb.Singleton;
import javax.inject.Inject; import javax.inject.Inject;
import com.commafeed.backend.StartupBean;
import com.commafeed.backend.dao.FeedDAO; import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.model.Feed; import com.commafeed.backend.model.Feed;
import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.backend.services.ApplicationSettingsService;
@@ -23,6 +25,9 @@ public class FeedRefreshTaskGiver {
@Inject @Inject
ApplicationSettingsService applicationSettingsService; ApplicationSettingsService applicationSettingsService;
@Inject
StartupBean startupBean;
private Queue<Feed> queue = Lists.newLinkedList(); private Queue<Feed> queue = Lists.newLinkedList();
@Lock(LockType.WRITE) @Lock(LockType.WRITE)
@@ -39,4 +44,9 @@ public class FeedRefreshTaskGiver {
} }
return queue.poll(); return queue.poll();
} }
@PreDestroy
public void shutdown() {
startupBean.shutdown();
}
} }

View File

@@ -18,6 +18,7 @@ import javax.transaction.RollbackException;
import javax.transaction.SystemException; import javax.transaction.SystemException;
import javax.transaction.UserTransaction; import javax.transaction.UserTransaction;
import org.apache.commons.lang.mutable.MutableBoolean;
import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -48,12 +49,10 @@ public class FeedRefreshWorker {
@Resource @Resource
private UserTransaction transaction; private UserTransaction transaction;
private boolean running = true;
@Asynchronous @Asynchronous
public Future<Void> start(String threadName) { public Future<Void> start(MutableBoolean running, String threadName) {
log.info("{} starting", threadName); log.info("{} starting", threadName);
while (running) { while (running.isTrue()) {
try { try {
Feed feed = getNextFeed(); Feed feed = getNextFeed();
if (feed != null) { if (feed != null) {
@@ -71,7 +70,7 @@ public class FeedRefreshWorker {
} }
@Asynchronous @Asynchronous
public void updateAsync(Feed feed){ public void updateAsync(Feed feed) {
try { try {
update(feed); update(feed);
} catch (Exception e) { } catch (Exception e) {