From 88bb4e8a9b0cf34c481f7f41ae4a8d0bbf10e82a Mon Sep 17 00:00:00 2001 From: patschuh <46817726+patschuh@users.noreply.github.com> Date: Fri, 7 Jun 2024 21:30:33 +0200 Subject: [PATCH] Improve adminClient handling in Controller * Closing previous adminClient if present and clear TopicList, before creating a new one * Display Exceptions thrown during AdminClient creation * Clear cluster selection if AdminClient creation failed --- src/main/java/at/esque/kafka/Controller.java | 21 ++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/at/esque/kafka/Controller.java b/src/main/java/at/esque/kafka/Controller.java index 18199bf..24ea0aa 100644 --- a/src/main/java/at/esque/kafka/Controller.java +++ b/src/main/java/at/esque/kafka/Controller.java @@ -289,8 +289,17 @@ public void setup(Stage controlledStage) { }); clusterComboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { - adminClient = new KafkaesqueAdminClient(newValue.getBootStrapServers(), configHandler.getSslProperties(selectedCluster()), configHandler.getSaslProperties(selectedCluster())); - refreshTopicList(); + try { + closeAdminClientIfPresentAndClearTopicList(); + if (newValue == null) { + return; + } + adminClient = new KafkaesqueAdminClient(newValue.getBootStrapServers(), configHandler.getSslProperties(selectedCluster()), configHandler.getSaslProperties(selectedCluster())); + refreshTopicList(); + } catch (Exception e) { + ErrorAlert.show(e, controlledStage); + clusterComboBox.getSelectionModel().clearSelection(); + } }); partitionCombobox.getItems().add(-1); @@ -345,6 +354,14 @@ public void setup(Stage controlledStage) { }); } + private void closeAdminClientIfPresentAndClearTopicList() { + if (adminClient != null) { + adminClient.close(); + adminClient = null; + topicListView.getBaseList().clear(); + } + } + private void showTextInStageTitle(String text) { if (Strings.isNullOrEmpty(text)) { controlledStage.setTitle("Kafkaesque");