From 7ee2cd1aa61041c22d1191b125c16c23efaa258c Mon Sep 17 00:00:00 2001 From: top Date: Mon, 2 Dec 2024 15:22:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8A=82=E7=82=B9=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=9F=A5=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/dao/ZkNodePack.java | 14 +++++++ src/main/java/ui/MainFrame.java | 68 +++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 src/main/java/dao/ZkNodePack.java diff --git a/src/main/java/dao/ZkNodePack.java b/src/main/java/dao/ZkNodePack.java new file mode 100644 index 0000000..01758a2 --- /dev/null +++ b/src/main/java/dao/ZkNodePack.java @@ -0,0 +1,14 @@ +package dao; + +import org.apache.zookeeper.data.Stat; + +public class ZkNodePack { + + public final Stat stat; + public final byte[] data; + + public ZkNodePack(Stat stat, byte[] data) { + this.stat = stat; + this.data = data; + } +} diff --git a/src/main/java/ui/MainFrame.java b/src/main/java/ui/MainFrame.java index 11eca4a..0d54f63 100644 --- a/src/main/java/ui/MainFrame.java +++ b/src/main/java/ui/MainFrame.java @@ -4,6 +4,7 @@ import cn.hutool.json.JSONConfig; import cn.hutool.json.JSONObject; import dao.TreeNode; +import dao.ZkNodePack; import manager.Application; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.imps.CuratorFrameworkState; @@ -20,6 +21,10 @@ import java.io.*; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Queue; import java.util.*; @@ -46,6 +51,7 @@ public class MainFrame extends JFrame { JTextField nodePathTextField; JTextArea nodeContent; + JTextArea nodeState; public ConnectLoseDialog connectLoseDialog; @@ -93,6 +99,7 @@ public MainFrame(Application application, String title) throws HeadlessException if (lastPath instanceof TreeNode) { final String selectPath = ((TreeNode) lastPath).getPathNotNull(); nodePathTextField.setText(selectPath); + nodeState.setText(""); logger.trace("jTree new Select Path: {}", selectPath); } else { logger.warn("jTree produce unexpected type tree path {}", lastPath.getClass().getCanonicalName()); @@ -167,13 +174,33 @@ public MainFrame(Application application, String title) throws HeadlessException nodeContent.setEditable(true); right.add(jScrollPane, gridBagConstraintsForTextFieldNodeContent); + GridBagConstraints gridBagConstraintsForTextFieldNodeStat = new GridBagConstraints( + 0, + 3, + GridBagConstraints.REMAINDER, + 2, + 0, + 0.1, + GridBagConstraints.SOUTHWEST, + GridBagConstraints.BOTH, + new Insets(1, 2, 1, 2), + 0, + 0 + ); + + nodeState = new JTextArea(); + JScrollPane jScrollPane2 = new JScrollPane(nodeState, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); + nodeState.setLineWrap(false); + nodeState.setEditable(false); + right.add(jScrollPane2, gridBagConstraintsForTextFieldNodeStat); + final List> keySet = new LinkedList<>(btnMap.entrySet()); final int keySetSize = keySet.size(); for (int i = 0; i < keySetSize; i++) { Map.Entry entry = keySet.get(i); GridBagConstraints gridBagConstraintsForBtn = new GridBagConstraints( i, - 3, + 5, 1, 1, 0.4, @@ -190,7 +217,7 @@ public MainFrame(Application application, String title) throws HeadlessException right.add( jProgressBar, new GridBagConstraints( - 0, 4, keySetSize, 1, 0, 0, GridBagConstraints.SOUTHWEST, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0 + 0, 6, keySetSize, 1, 0, 0, GridBagConstraints.SOUTHWEST, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0 ) ); @@ -344,26 +371,40 @@ protected void actForBtnView() { if (stat == null) { return null; } - return curatorFramework.getData().forPath(path); + return new ZkNodePack(stat, curatorFramework.getData().forPath(path)); } catch (Exception e) { throw new RuntimeException(e); } }, application.executorService).thenApplyAsync( - (data) -> { + (pack) -> { + byte[] data = pack.data; if (data == null) { EventQueue.invokeLater(() -> JOptionPane.showMessageDialog(this, "节点不存在", "ZkView", JOptionPane.WARNING_MESSAGE)); } - return Optional.ofNullable(data); + return pack; }, application.executorService - ).whenCompleteAsync((optionalBytes, exception) -> { - if (optionalBytes.isEmpty()) { - return; - } - byte[] bytes = optionalBytes.get(); - String string = new String(bytes, charset); + ).whenCompleteAsync((pack, exception) -> { + var optionalBytes = pack.data; + String nodeContentText = new String(optionalBytes, charset); + var stat = pack.stat; + var nodeStatText = String.format( + "节点创建事务Id: %d\n节点最后更新事务Id:%d\n节点创建时间:%s\n节点最后修改时间:%s\n节点数据长度:%d\n子节点数量:%d\n节点数据版本号:%d\n节点子节点版本号:%d\n" + + "节点子节点配置版本号:%d\n节点临时会话Id:%d", + stat.getCzxid(), + stat.getMzxid(), + DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(stat.getCtime()), ZoneId.systemDefault())), + DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(stat.getMtime()), ZoneId.systemDefault())), + stat.getDataLength(), + stat.getNumChildren(), + stat.getVersion(), + stat.getAversion(), + stat.getCversion(), + stat.getEphemeralOwner() + ); EventQueue.invokeLater(() -> { - nodeContent.setText(string); + nodeContent.setText(nodeContentText); + nodeState.setText(nodeStatText); jProgressBar.setValue(100); }); application.scheduledExecutorService.schedule(() -> EventQueue.invokeLater(() -> jProgressBar.setValue(0)), 500, TimeUnit.MILLISECONDS); @@ -595,6 +636,7 @@ protected void actForBtnReload() { jTree.clearSelection(); nodePathTextField.setText(""); nodeContent.setText(""); + nodeState.setText(""); }); if (exception != null) { logger.error("重载全部节点发生异常", exception); @@ -689,10 +731,10 @@ protected void actForBtnJsonExpand() { protected void actForBtnClear() { nodeContent.setText(""); + nodeState.setText(""); } void stop() { application.stop(); } } -