diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml index c90834f..f54a89f 100644 --- a/CasicTimeGuard.iml +++ b/CasicTimeGuard.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml index c90834f..f54a89f 100644 --- a/CasicTimeGuard.iml +++ b/CasicTimeGuard.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/asserts/update.log b/asserts/update.log new file mode 100644 index 0000000..0b93cb0 --- /dev/null +++ b/asserts/update.log @@ -0,0 +1 @@ +2021-12-09 10:37:28 \ No newline at end of file diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml index c90834f..f54a89f 100644 --- a/CasicTimeGuard.iml +++ b/CasicTimeGuard.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/asserts/update.log b/asserts/update.log new file mode 100644 index 0000000..0b93cb0 --- /dev/null +++ b/asserts/update.log @@ -0,0 +1 @@ +2021-12-09 10:37:28 \ No newline at end of file diff --git a/lib/guava-18.0.jar b/lib/guava-18.0.jar new file mode 100644 index 0000000..8f89e49 --- /dev/null +++ b/lib/guava-18.0.jar Binary files differ diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml index c90834f..f54a89f 100644 --- a/CasicTimeGuard.iml +++ b/CasicTimeGuard.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/asserts/update.log b/asserts/update.log new file mode 100644 index 0000000..0b93cb0 --- /dev/null +++ b/asserts/update.log @@ -0,0 +1 @@ +2021-12-09 10:37:28 \ No newline at end of file diff --git a/lib/guava-18.0.jar b/lib/guava-18.0.jar new file mode 100644 index 0000000..8f89e49 --- /dev/null +++ b/lib/guava-18.0.jar Binary files differ diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java index 5235034..7140412 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.java +++ b/src/com/casic/swing/ui/TimeGuardNtp.java @@ -1,7 +1,9 @@ package com.casic.swing.ui; import com.casic.swing.utils.CommandUtil; +import com.casic.swing.utils.StringHelper; import com.casic.swing.utils.TimeOrDateUtil; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import javax.swing.*; import javax.swing.border.EmptyBorder; @@ -12,6 +14,9 @@ import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * @author a203 @@ -34,6 +39,8 @@ private static final Integer[] PERIOD = {1, 6, 12, 24}; private boolean hasNtp = false; private String host = HOST_IP[0]; + private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor( + 1, new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build()); public static void main(String[] args) { TimeGuardNtp timeGuard = new TimeGuardNtp(); @@ -63,6 +70,13 @@ } }).start(); + String assertsData = StringHelper.getAssertsData(); + if (!"".equals(assertsData)) { + timeGuard.timeValueLabel.setText(assertsData); + } else { + timeGuard.timeValueLabel.setText("无法确定最近同步时间"); + } + //初始化JComboBox initComBox(timeGuard); timeGuard.hostComboBox.addItemListener(new ItemListener() { @@ -123,7 +137,20 @@ public void itemStateChanged(ItemEvent e) { int period = (Integer) e.getItem(); if (e.getStateChange() == ItemEvent.SELECTED) { - System.out.println(period + "h"); + /** + * 开启同步,只能开启一个同步线程 + * + * scheduleAtFixedRate + * 是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕 + * 如果上一个任务执行完毕,则当前任务立即执行 + * 如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行 + * */ + timeGuard.executorService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + updateView(timeGuard); + } + }, 0, period, TimeUnit.HOURS); } } }); @@ -132,22 +159,29 @@ timeGuard.updateTimeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - String result = CommandUtil.ntpDate(timeGuard.host); - System.out.println("命令执行结果 ===> " + result); - boolean isSuccess = result.contains("step time server"); - if (isSuccess) { - timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png")); - timeGuard.dotView.setText("同步成功"); - timeGuard.timeValueLabel.setText(result); - } else { - timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png")); - timeGuard.dotView.setText("同步失败"); - timeGuard.timeValueLabel.setText(""); - } + updateView(timeGuard); } }); } + private static void updateView(TimeGuardNtp timeGuard) { + String result = CommandUtil.ntpDate(timeGuard.host); + System.out.println("命令执行结果 ===> " + result); + boolean isSuccess = result.contains("step time server") || result.contains("adjust time server"); + if (isSuccess) { + String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis()); + timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png")); + timeGuard.dotView.setText("同步成功"); + timeGuard.timeValueLabel.setText(systemTime); + //同步成功之后将时间存入本地 + StringHelper.saveAssertsData(systemTime); + } else { + timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png")); + timeGuard.dotView.setText("同步失败"); + timeGuard.timeValueLabel.setText(""); + } + } + private static void initComBox(TimeGuardNtp timeGuard) { for (String s : HOST_NAME) { timeGuard.hostComboBox.addItem(s); diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml index c90834f..f54a89f 100644 --- a/CasicTimeGuard.iml +++ b/CasicTimeGuard.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/asserts/update.log b/asserts/update.log new file mode 100644 index 0000000..0b93cb0 --- /dev/null +++ b/asserts/update.log @@ -0,0 +1 @@ +2021-12-09 10:37:28 \ No newline at end of file diff --git a/lib/guava-18.0.jar b/lib/guava-18.0.jar new file mode 100644 index 0000000..8f89e49 --- /dev/null +++ b/lib/guava-18.0.jar Binary files differ diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java index 5235034..7140412 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.java +++ b/src/com/casic/swing/ui/TimeGuardNtp.java @@ -1,7 +1,9 @@ package com.casic.swing.ui; import com.casic.swing.utils.CommandUtil; +import com.casic.swing.utils.StringHelper; import com.casic.swing.utils.TimeOrDateUtil; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import javax.swing.*; import javax.swing.border.EmptyBorder; @@ -12,6 +14,9 @@ import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * @author a203 @@ -34,6 +39,8 @@ private static final Integer[] PERIOD = {1, 6, 12, 24}; private boolean hasNtp = false; private String host = HOST_IP[0]; + private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor( + 1, new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build()); public static void main(String[] args) { TimeGuardNtp timeGuard = new TimeGuardNtp(); @@ -63,6 +70,13 @@ } }).start(); + String assertsData = StringHelper.getAssertsData(); + if (!"".equals(assertsData)) { + timeGuard.timeValueLabel.setText(assertsData); + } else { + timeGuard.timeValueLabel.setText("无法确定最近同步时间"); + } + //初始化JComboBox initComBox(timeGuard); timeGuard.hostComboBox.addItemListener(new ItemListener() { @@ -123,7 +137,20 @@ public void itemStateChanged(ItemEvent e) { int period = (Integer) e.getItem(); if (e.getStateChange() == ItemEvent.SELECTED) { - System.out.println(period + "h"); + /** + * 开启同步,只能开启一个同步线程 + * + * scheduleAtFixedRate + * 是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕 + * 如果上一个任务执行完毕,则当前任务立即执行 + * 如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行 + * */ + timeGuard.executorService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + updateView(timeGuard); + } + }, 0, period, TimeUnit.HOURS); } } }); @@ -132,22 +159,29 @@ timeGuard.updateTimeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - String result = CommandUtil.ntpDate(timeGuard.host); - System.out.println("命令执行结果 ===> " + result); - boolean isSuccess = result.contains("step time server"); - if (isSuccess) { - timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png")); - timeGuard.dotView.setText("同步成功"); - timeGuard.timeValueLabel.setText(result); - } else { - timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png")); - timeGuard.dotView.setText("同步失败"); - timeGuard.timeValueLabel.setText(""); - } + updateView(timeGuard); } }); } + private static void updateView(TimeGuardNtp timeGuard) { + String result = CommandUtil.ntpDate(timeGuard.host); + System.out.println("命令执行结果 ===> " + result); + boolean isSuccess = result.contains("step time server") || result.contains("adjust time server"); + if (isSuccess) { + String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis()); + timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png")); + timeGuard.dotView.setText("同步成功"); + timeGuard.timeValueLabel.setText(systemTime); + //同步成功之后将时间存入本地 + StringHelper.saveAssertsData(systemTime); + } else { + timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png")); + timeGuard.dotView.setText("同步失败"); + timeGuard.timeValueLabel.setText(""); + } + } + private static void initComBox(TimeGuardNtp timeGuard) { for (String s : HOST_NAME) { timeGuard.hostComboBox.addItem(s); diff --git a/src/com/casic/swing/utils/StringHelper.java b/src/com/casic/swing/utils/StringHelper.java new file mode 100644 index 0000000..6a76e68 --- /dev/null +++ b/src/com/casic/swing/utils/StringHelper.java @@ -0,0 +1,55 @@ +package com.casic.swing.utils; + +import java.io.*; + +/** + * @author a203 + */ +public class StringHelper { + + private static final String FILE_NAME = "asserts/update.log"; + + /** + * 保存文件到本地Asserts + */ + public static void saveAssertsData(String data) { + try { + File writeName = new File(FILE_NAME); + if (!writeName.exists()) { + // 创建新文件,有同名的文件的话直接覆盖 + writeName.createNewFile(); + } + FileWriter writer = new FileWriter(writeName); + BufferedWriter out = new BufferedWriter(writer); + out.write(data); + out.flush(); + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 获取本地Asserts文件内容 + */ + public static String getAssertsData() { + try { + File file = new File(FILE_NAME); + InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file)); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + StringBuilder data = new StringBuilder(); + String s; + try { + while ((s = bufferedReader.readLine()) != null) { + data.append(s); + } + return data.toString(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } +}