Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
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;
import java.time.Instant;
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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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<Void> shutdownAsync() {
final CompletableFuture<Void> 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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
{
Expand All @@ -116,12 +116,16 @@ private void changeConfig(final String new_config_name)
}
}

private void dispose()
private CompletableFuture<Void> dispose()
{
if (client != null)
{
client.shutdown();
client = null;
}
if (client == null)
return CompletableFuture.completedFuture(null);

client.removeListener(area_view);
area_view = null;

final CompletableFuture<Void> future = client.shutdownAsync();
client = null;
return future;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -141,17 +140,16 @@ public void save(final Memento memento)
table.save(memento);
}

private void dispose()
private CompletableFuture<Void> 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<Void> future = client.shutdownAsync();
client = null;
return future;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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<Void> dispose()
{
if (client != null)
{
client.shutdown();
client = null;
}
if (client == null)
return CompletableFuture.completedFuture(null);

client.removeListener(tree_view);
tree_view = null;

final CompletableFuture<Void> future = client.shutdownAsync();
client = null;
return future;
}
}
Loading