Register the DatabaseUiExecutor for shutdown by the LifecycleManager.

See issue #3612607.
This commit is contained in:
akwizgran
2013-05-16 15:39:41 +01:00
parent 1692e5a695
commit 37e68d5e9e
3 changed files with 19 additions and 16 deletions

View File

@@ -16,6 +16,7 @@ import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.android.DatabaseUiExecutor; import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.android.ReferenceManager; import net.sf.briar.api.android.ReferenceManager;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.lifecycle.ShutdownManager; import net.sf.briar.api.lifecycle.ShutdownManager;
import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.duplex.DuplexPluginConfig; import net.sf.briar.api.plugins.duplex.DuplexPluginConfig;
@@ -34,22 +35,29 @@ import com.google.inject.Singleton;
public class AndroidModule extends AbstractModule { public class AndroidModule extends AbstractModule {
protected void configure() { private final ExecutorService databaseUiExecutor;
bind(AndroidExecutor.class).to(AndroidExecutorImpl.class);
bind(ReferenceManager.class).to(ReferenceManagerImpl.class).in( public AndroidModule() {
Singleton.class);
// The queue is unbounded, so tasks can be dependent // The queue is unbounded, so tasks can be dependent
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
// Discard tasks that are submitted during shutdown // Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy = RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy(); new ThreadPoolExecutor.DiscardPolicy();
// Use a single thread so DB accesses from the UI don't overlap // Use a single thread so DB accesses from the UI don't overlap
ExecutorService e = new ThreadPoolExecutor(1, 1, 60, SECONDS, queue, databaseUiExecutor = new ThreadPoolExecutor(1, 1, 60, SECONDS, queue,
policy); policy);
bind(Executor.class).annotatedWith( }
DatabaseUiExecutor.class).toInstance(e);
bind(ExecutorService.class).annotatedWith( protected void configure() {
DatabaseUiExecutor.class).toInstance(e); bind(AndroidExecutor.class).to(AndroidExecutorImpl.class);
bind(ReferenceManager.class).to(ReferenceManagerImpl.class).in(
Singleton.class);
}
@Provides @Singleton @DatabaseUiExecutor
Executor getDatabaseUiExecutor(LifecycleManager lifecycleManager) {
lifecycleManager.registerForShutdown(databaseUiExecutor);
return databaseUiExecutor;
} }
@Provides @Provides

View File

@@ -5,12 +5,10 @@ import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.sf.briar.R; import net.sf.briar.R;
import net.sf.briar.api.android.AndroidExecutor; import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.lifecycle.LifecycleManager; import net.sf.briar.api.lifecycle.LifecycleManager;
import roboguice.service.RoboService; import roboguice.service.RoboService;
import android.app.PendingIntent; import android.app.PendingIntent;
@@ -33,7 +31,6 @@ public class BriarService extends RoboService {
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject private volatile LifecycleManager lifecycleManager; @Inject private volatile LifecycleManager lifecycleManager;
@Inject private volatile AndroidExecutor androidExecutor; @Inject private volatile AndroidExecutor androidExecutor;
@Inject @DatabaseUiExecutor private volatile ExecutorService dbUiExecutor;
@Override @Override
public void onCreate() { public void onCreate() {
@@ -81,10 +78,7 @@ public class BriarService extends RoboService {
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
// FIXME: This is ugly - executors should register themselves
// with the lifecycle manager
androidExecutor.shutdown(); androidExecutor.shutdown();
dbUiExecutor.shutdown();
lifecycleManager.stopServices(); lifecycleManager.stopServices();
} }
}.start(); }.start();

View File

@@ -1,6 +1,7 @@
package net.sf.briar.api.android; package net.sf.briar.api.android;
import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@@ -13,6 +14,6 @@ import com.google.inject.BindingAnnotation;
* Annotation for injecting the executor for accessing the database from the UI. * Annotation for injecting the executor for accessing the database from the UI.
*/ */
@BindingAnnotation @BindingAnnotation
@Target({ FIELD, PARAMETER }) @Target({ FIELD, METHOD, PARAMETER })
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface DatabaseUiExecutor {} public @interface DatabaseUiExecutor {}