324 Star 2.4K Fork 857

AE86/DBSyncer

 / 详情

加载插件,重复类

已完成
创建于  
2023-08-08 15:18

我写了一个 插件,插件作用是接收到数据库的日志,然后将消息通过websocket发送到另外一个系统,目前问题是,类加载器加载插件的时候,每次都是new UrlClassLoader,导致系统中存在多个同名类,原来的类的实例也继续存在,每次重新上传一次插件,就会多一个。能不能在加载前做一个同名类卸载的接口,在卸载中可以关闭之前的资源

评论 (1)

落叶之夜 创建了任务
/* ConvertService 接口增加 close方法 */
package org.dbsyncer.common.spi;

public interface ConvertService {
    void convert(ConvertContext var1);

    default void postProcessAfter(ConvertContext context) {
    }

    default String getVersion() {
        return "1.0.0";
    }

    default String getName() {
        return this.getClass().getSimpleName();
    }

    default void close() {
    }
}

org.dbsyncer.plugin.PluginFactory#loadPlugin


    /**
     * SPI, 扫描jar扩展接口实现,注册为本地服务
     *
     * @param jar
     */
    private void loadPlugin(File jar) {
        try {
            String fileName = jar.getName();
            URL url = jar.toURI().toURL();
            URLClassLoader loader = new URLClassLoader(new URL[] {url}, Thread.currentThread().getContextClassLoader());
            ServiceLoader<ConvertService> services = ServiceLoader.load(ConvertService.class, loader);
            for (ConvertService s : services) {
                String className = s.getClass().getName();
                /*增加一个销毁注销接口*/
                if (service.containsKey(className)) {
                    try {
                        ConvertService convertService = service.get(className);
                        convertService.close();
                    } catch (Exception e) {
                        logger.error(e.getMessage(),e);
                    }
                }
                service.put(className, s);
                plugins.add(new Plugin(s.getName(), className, s.getVersion(), fileName));
                logger.info("{}, {}_{} {}", fileName, s.getName(), s.getVersion(), className);
            }
        } catch (MalformedURLException e) {
            logger.error(e.getMessage());
        }

    }
AE86 任务状态待办的 修改为进行中
AE86 添加了
 
question
标签
AE86 任务状态进行中 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
Java
1
https://gitee.com/ghi/dbsyncer.git
git@gitee.com:ghi/dbsyncer.git
ghi
dbsyncer
DBSyncer

搜索帮助

Cb406eda 1850385 E526c682 1850385