介绍
Demo说明
本文基于maven项目开发,idea版本为2022.3以上,jdk为1.8
本文在Tools插件之上进行开发
Tools插件说明
Tools插件是一个Idea插件,此插件提供统一Spi规范,极大的降低了idea插件的开发难度,并提供开发者模块,可以极大的为开发者开发此插件提供便利
Tools插件安装需要idea2022.3以上版本
- 插件下载连接:
https://download.csdn.net/download/qq_42413011/89702325
- sdk下载连接:
https://download.csdn.net/download/qq_42413011/89702330
- pojo-serializer插件:
https://gitee.com/myprofile/pojo-serializer
正文
打开上一篇文章项目
调整代码如下
package com.lhstack.aaa;
import com.lhstack.tools.plugins.IPlugin;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
public class PluginImpl implements IPlugin {
private final Map<String, JLabel> cache = new HashMap<>();
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName("Demo-Plugin-Scheduled-Thread #" + thread.getId());
return thread;
}
});
@Override
public JComponent createPanel(String projectHash) {
return cache.computeIfAbsent(projectHash, key -> {
return new JLabel(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),JLabel.CENTER);
});
}
@Override
public void closeProject(String projectHash) {
//关闭项目,移除项目对应打开的组件
cache.remove(projectHash);
}
@Override
public void unInstall() {
//清除缓存
cache.clear();
//停止线程池
scheduledExecutorService.shutdown();
}
@Override
public void install() {
//初始化任务调度,更新所有组件时间
scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
cache.forEach((key, value) -> value.setText(date));
scheduledExecutorService.schedule(this, 200, TimeUnit.MILLISECONDS);
}
}, 200, TimeUnit.MILLISECONDS);
}
@Override
public Icon pluginIcon() {
try {
return new ImageIcon(ImageIO.read(Objects.requireNonNull(PluginImpl.class.getClassLoader().getResourceAsStream("DEMO-48x48.png"))));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public Icon pluginTabIcon() {
try {
return new ImageIcon(ImageIO.read(Objects.requireNonNull(PluginImpl.class.getClassLoader().getResourceAsStream("DEMO-16x16.png"))));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public String pluginName() {
return "Demo";
}
@Override
public String pluginDesc() {
return "这是一个Demo插件描述";
}
@Override
public String pluginVersion() {
return "0.0.1";
}
}
使用插件开发模块运行,查看效果
jconsole查看线程运行情况
停止运行,查看线程情况
线程释放了
打包安装
多个项目情况