cancel threads when application stops/restarts

This commit is contained in:
Athou
2013-04-13 11:41:33 +02:00
parent c41c4fe8b9
commit b3465f33c1
2 changed files with 25 additions and 4 deletions

View File

@@ -2,8 +2,11 @@ package com.commafeed.backend;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Future;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton; import javax.ejb.Singleton;
import javax.ejb.Startup; import javax.ejb.Startup;
import javax.inject.Inject; import javax.inject.Inject;
@@ -24,6 +27,7 @@ import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserRole.Role; import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.UserService; import com.commafeed.backend.services.UserService;
import com.google.api.client.util.Lists;
@Startup @Startup
@Singleton @Singleton
@@ -53,6 +57,8 @@ public class StartupBean {
@Inject @Inject
FeedRefreshWorker worker; FeedRefreshWorker worker;
private List<Future<Void>> threads = Lists.newArrayList();
private long startupTime; private long startupTime;
@PostConstruct @PostConstruct
@@ -64,7 +70,8 @@ public class StartupBean {
ApplicationSettings settings = applicationSettingsService.get(); ApplicationSettings settings = applicationSettingsService.get();
for (int i = 0; i < settings.getBackgroundThreads(); i++) { for (int i = 0; i < settings.getBackgroundThreads(); i++) {
worker.start(); Future<Void> thread = worker.start();
threads.add(thread);
} }
} }
@@ -129,4 +136,11 @@ public class StartupBean {
return startupTime; return startupTime;
} }
@PreDestroy
private void shutdown() {
for (Future<Void> future : threads) {
future.cancel(true);
}
}
} }

View File

@@ -2,9 +2,12 @@ package com.commafeed.backend.feeds;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.concurrent.Future;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous; import javax.ejb.Asynchronous;
import javax.ejb.EJBException;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType; import javax.ejb.TransactionManagementType;
@@ -46,9 +49,11 @@ public class FeedRefreshWorker {
@Resource @Resource
private UserTransaction transaction; private UserTransaction transaction;
private boolean running = true;
@Asynchronous @Asynchronous
public void start() { public Future<Void> start() {
while (true) { while (running) {
try { try {
Feed feed = getNextFeed(); Feed feed = getNextFeed();
if (feed != null) { if (feed != null) {
@@ -59,9 +64,10 @@ public class FeedRefreshWorker {
Thread.sleep(15000); Thread.sleep(15000);
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); throw new EJBException(e.getMessage(), e);
} }
} }
return new AsyncResult<Void>(null);
} }
private void update(Feed feed) throws NotSupportedException, private void update(Feed feed) throws NotSupportedException,
@@ -115,4 +121,5 @@ public class FeedRefreshWorker {
private Feed getNextFeed() { private Feed getNextFeed() {
return taskGiver.take(); return taskGiver.take();
} }
} }