forked from Archives/Athou_commafeed
cancel threads when application stops/restarts
This commit is contained in:
@@ -2,8 +2,11 @@ package com.commafeed.backend;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
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.services.ApplicationSettingsService;
|
||||
import com.commafeed.backend.services.UserService;
|
||||
import com.google.api.client.util.Lists;
|
||||
|
||||
@Startup
|
||||
@Singleton
|
||||
@@ -53,6 +57,8 @@ public class StartupBean {
|
||||
@Inject
|
||||
FeedRefreshWorker worker;
|
||||
|
||||
private List<Future<Void>> threads = Lists.newArrayList();
|
||||
|
||||
private long startupTime;
|
||||
|
||||
@PostConstruct
|
||||
@@ -64,7 +70,8 @@ public class StartupBean {
|
||||
|
||||
ApplicationSettings settings = applicationSettingsService.get();
|
||||
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;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void shutdown() {
|
||||
for (Future<Void> future : threads) {
|
||||
future.cancel(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ package com.commafeed.backend.feeds;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.ejb.AsyncResult;
|
||||
import javax.ejb.Asynchronous;
|
||||
import javax.ejb.EJBException;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.ejb.TransactionManagement;
|
||||
import javax.ejb.TransactionManagementType;
|
||||
@@ -46,9 +49,11 @@ public class FeedRefreshWorker {
|
||||
@Resource
|
||||
private UserTransaction transaction;
|
||||
|
||||
private boolean running = true;
|
||||
|
||||
@Asynchronous
|
||||
public void start() {
|
||||
while (true) {
|
||||
public Future<Void> start() {
|
||||
while (running) {
|
||||
try {
|
||||
Feed feed = getNextFeed();
|
||||
if (feed != null) {
|
||||
@@ -59,9 +64,10 @@ public class FeedRefreshWorker {
|
||||
Thread.sleep(15000);
|
||||
}
|
||||
} 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,
|
||||
@@ -115,4 +121,5 @@ public class FeedRefreshWorker {
|
||||
private Feed getNextFeed() {
|
||||
return taskGiver.take();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user