diff --git a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClient.java b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClient.java index 34f5969cd6..3fdd0cf368 100644 --- a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClient.java +++ b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClient.java @@ -19,6 +19,7 @@ import org.phoebus.applications.alarm.model.json.JsonModelReader; import org.phoebus.applications.alarm.model.json.JsonModelWriter; import org.phoebus.applications.alarm.model.json.JsonTags; +import org.phoebus.framework.jobs.JobManager; import org.phoebus.util.time.TimestampFormats; import java.time.Duration; @@ -26,6 +27,7 @@ import java.util.List; import java.util.Objects; import java.util.Set; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.TimeUnit; @@ -149,11 +151,11 @@ public void addListener(final AlarmClientListener listener) { } /** + * Does nothing if the listener was not registered * @param listener Listener to remove */ public void removeListener(final AlarmClientListener listener) { - if (!listeners.remove(listener)) - throw new IllegalStateException("Unknown listener"); + listeners.remove(listener); } /** @@ -594,6 +596,24 @@ private void checkServerState() { } } + /** + * Stop client in the background + * + * @return A future which is completed when the client has shut down + */ + public CompletableFuture shutdownAsync() { + final CompletableFuture future = new CompletableFuture<>(); + JobManager.schedule("Dispose alarm client for config " + config_topic, _ -> { + try { + shutdown(); + future.complete(null); + } catch (final Exception ex) { + future.completeExceptionally(ex); + } + }); + return future; + } + /** * Stop client */ diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/area/AlarmAreaInstance.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/area/AlarmAreaInstance.java index 817156bbbf..96ca04f93e 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/area/AlarmAreaInstance.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/area/AlarmAreaInstance.java @@ -36,6 +36,7 @@ public class AlarmAreaInstance implements AppInstance private final AlarmAreaApplication app; private String server = null, config_name = null; + private AlarmAreaView area_view = null; private AlarmClient client = null; private final DockItemWithInput tab; @@ -51,7 +52,7 @@ public AlarmAreaInstance(final AlarmAreaApplication app, final URI input) throws Platform.runLater(() -> tab.setLabel(config_name + " " + app.getDisplayName())); tab.addCloseCheck(() -> { - dispose(); + dispose().join(); return CompletableFuture.completedFuture(true); }); DockPane.getActiveDockPane().addTab(tab); @@ -72,7 +73,7 @@ private Node create(final URI input) throws Exception try { client = new AlarmClient(server, config_name, AlarmSystem.kafka_properties); - final AlarmAreaView area_view = new AlarmAreaView(client); + area_view = new AlarmAreaView(client); client.start(); if (AlarmSystem.config_names.length > 0) @@ -99,8 +100,7 @@ private Node create(final URI input) throws Exception private void changeConfig(final String new_config_name) { - // Dispose existing setup - dispose(); + dispose(); // Don't wait on future; shut down in background try { @@ -116,12 +116,16 @@ private void changeConfig(final String new_config_name) } } - private void dispose() + private CompletableFuture dispose() { - if (client != null) - { - client.shutdown(); - client = null; - } + if (client == null) + return CompletableFuture.completedFuture(null); + + client.removeListener(area_view); + area_view = null; + + final CompletableFuture future = client.shutdownAsync(); + client = null; + return future; } } diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableInstance.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableInstance.java index cfb6cea7d4..3dfdb786b1 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableInstance.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableInstance.java @@ -54,7 +54,7 @@ public AlarmTableInstance(final AlarmTableApplication app, final URI input) thro Platform.runLater(() -> tab.setLabel(config_name + " " + app.getDisplayName())); tab.addCloseCheck(() -> { - dispose(); + dispose().join(); return CompletableFuture.completedFuture(true); }); DockPane.getActiveDockPane().addTab(tab); @@ -112,8 +112,7 @@ private Node create(final URI input) throws Exception private void changeConfig(final String new_config_name) { - // Dispose existing setup - dispose(); + dispose(); // Don't wait on future; shut down in background try { @@ -141,17 +140,16 @@ public void save(final Memento memento) table.save(memento); } - private void dispose() + private CompletableFuture dispose() { - if (mediator != null) - { - client.removeListener(mediator); - mediator = null; - } - if (client != null) - { - client.shutdown(); - client = null; - } + if (client == null) + return CompletableFuture.completedFuture(null); + + client.removeListener(mediator); + mediator = null; + + final CompletableFuture future = client.shutdownAsync(); + client = null; + return future; } } diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeInstance.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeInstance.java index f9254c349f..b3035dfb0f 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeInstance.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeInstance.java @@ -37,6 +37,7 @@ class AlarmTreeInstance implements AppInstance private final AlarmTreeApplication app; private String server = null, config_name = null; + private AlarmTreeView tree_view; private AlarmClient client = null; private final DockItemWithInput tab; @@ -59,7 +60,7 @@ public AlarmTreeInstance(final AlarmTreeApplication app, final URI input) throws Platform.runLater(() -> tab.setLabel(config_name + " " + app.getDisplayName())); tab.addCloseCheck(() -> { - dispose(); + dispose().join(); return CompletableFuture.completedFuture(true); }); DockPane.getActiveDockPane().addTab(tab); @@ -87,7 +88,7 @@ private Node create(final URI input, String itemName) throws Exception try { client = new AlarmClient(server, config_name, AlarmSystem.kafka_properties); - final AlarmTreeView tree_view = new AlarmTreeView(client, itemName); + tree_view = new AlarmTreeView(client, itemName); client.start(); if (AlarmSystem.config_names.length > 0) @@ -105,32 +106,31 @@ private Node create(final URI input, String itemName) throws Exception } } - private void changeConfig(final String new_config_name) - { - // Dispose existing setup - dispose(); + private void changeConfig(final String new_config_name) { + dispose(); // Don't wait on future; shut down in background - try - { + try { // Use same server name, but new config_name final URI new_input = AlarmURI.createURI(server, new_config_name); // no need for initial item name tab.setContent(create(new_input, null)); tab.setInput(new_input); Platform.runLater(() -> tab.setLabel(config_name + " " + app.getDisplayName())); - } - catch (Exception ex) - { + } catch (Exception ex) { logger.log(Level.WARNING, "Cannot switch alarm tree to " + config_name, ex); } } - private void dispose() + private CompletableFuture dispose() { - if (client != null) - { - client.shutdown(); - client = null; - } + if (client == null) + return CompletableFuture.completedFuture(null); + + client.removeListener(tree_view); + tree_view = null; + + final CompletableFuture future = client.shutdownAsync(); + client = null; + return future; } }