MyBatis系列之mapper.xml实现热加载HotSwap

概述

说来惭愧,工作5年多,才来研究这个问题。平心而论,这种实际问题比什么单例模式的几种写法有意义有价值多。真要给自己找理由的话,刚工作时用hibernate及Spring Data Jpa,后短暂使用过MyBatis,再后来使用公司自研的数据访问层框架DAL,也没用到MyBatis,再就是现在的工作,一年零3个月,遇到这个问题,忍无可忍。

这个问题就是:在使用MyBatis时,不能保证一次性写对mapper.xml文件,尤其是在有join分句时,怎么办?只能改mapper.xml文件,然后重启应用。这个过程很耗时。短则1分钟,长则2~3分钟。

题外话:
IDEA自带热更新功能,参考Java开发IDE神器IntelliJ IDEA 教程,但是这个热更新对于MyBatismapper.xml文件是不生效的。

另,JRebel(需要破解)也有很强大的热更新功能(比IDEA自带的热更新功能强,但是也会有插件版本兼容问题,参考IDEA使用插件JRebel热部署失败的问题排查),现在不用这个插件,不知道能不能更新MyBatismapper.xml文件,有待调研。

实现

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.Configuration;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author johnny
 */
@Slf4j
public class MapperRefresh implements Runnable {
    /**
     * 是否启用Mapper刷新线程功能
     */
    private static final boolean ENABLED = true;

    /**
     * 刷新启用后,是否启动了刷新线程
     */
    private static boolean refresh;

    /**
     * Mapper实际资源路径
     */
    private Set<String> location;

    /**
     * Mapper资源路径
     */
    private final Resource[] mapperLocations;

    /**
     * MyBatis配置对象
     */
    private final Configuration configuration;

    /**
     * 上一次刷新时间
     */
    private Long beforeTime = 0L;

    /**
     * 延迟刷新秒数
     */
    private static final int DELAY_SECONDS = 3;

    /**
     * 休眠时间
     */
    private static final int SLEEP_SECONDS = 1;

    /**
     * xml文件夹匹配字符串,需要根据需要修改
     */
    private static final String MAPPING_PATH = "mapper";

    public static boolean isRefresh() {
        return refresh;
    }

    public MapperRefresh(Resource[] mapperLocations, Configuration configuration) {
        this.mapperLocations = mapperLocations;
        this.configuration = configuration;
    }

    @Override
    public void run() {
        beforeTime = System.currentTimeMillis();
        log.debug("[location] " + location);
        log.debug("[configuration] " + configuration);
        if (ENABLED) {
            // 启动刷新线程
            final MapperRefresh runnable = this;
            new Thread(() -> {
                if (location == null) {
                    location = Sets.newHashSet();
                    log.debug("MapperLocation's length:" + mapperLocations.length);
                    for (Resource mapperLocation : mapperLocations) {
                        String s = mapperLocation.toString().replaceAll("\\\\", "/");
                        s = s.substring("file [".length(), s.lastIndexOf(MAPPING_PATH) + MAPPING_PATH.length());
                        if (!location.contains(s)) {
                            location.add(s);
                            log.debug("Location:" + s);
                        }
                    }
                    log.debug("location's size:" + location.size());
                }
                try {
                    Thread.sleep(DELAY_SECONDS * 1000);
                } catch (InterruptedException e) {
                    log.error("run error: " + e.getMessage());
                }
                refresh = true;
                while (true) {
                    try {
                        for (String s : location) {
                            runnable.refresh(s, beforeTime);
                        }
                    } catch (Exception e) {
                        log.error("run error: " + e.getMessage());
                    }
                    try {
                        Thread.sleep(SLEEP_SECONDS * 1000);
                    } catch (InterruptedException e) {
                        log.error("run error: " + e.getMessage());
                    }
                }
            }, "MyBatis-Mapper-Refresh").start();
        }
    }

    /**
     * 执行刷新
     *
     * @param filePath   刷新目录
     * @param beforeTime 上次刷新时间
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    private void refresh(String filePath, Long beforeTime) throws FileNotFoundException, NestedIOException {
        // 本次刷新时间
        long refreshTime = System.currentTimeMillis();
        // 获取需要刷新的Mapper文件列表
        List<File> fileList = this.getRefreshFile(new File(filePath), beforeTime);
        if (fileList.size() > 0) {
            log.debug("Refresh file: " + fileList.size());
        }
        for (File item : fileList) {
            InputStream inputStream = new FileInputStream(item);
            String resource = item.getAbsolutePath();
            try {
                // 清理原有资源,更新为自己的StrictMap方便,增量重新加载
                String[] mapFieldNames = new String[]{
                        "mappedStatements", "caches",
                        "resultMaps", "parameterMaps",
                        "keyGenerators", "sqlFragments"
                };
                for (String fieldName : mapFieldNames) {
                    Field field = configuration.getClass().getDeclaredField(fieldName);
                    field.setAccessible(true);
                    Map map = ((Map) field.get(configuration));
                    if (!(map instanceof StrictMap)) {
                        Map newMap = new StrictMap(StringUtils.capitalize(fieldName) + "collection");
                        for (Object key : map.keySet()) {
                            try {
                                newMap.put(key, map.get(key));
                            } catch (IllegalArgumentException ex) {
                                newMap.put(key, ex.getMessage());
                            }
                        }
                        field.set(configuration, newMap);
                    }
                }
                // 清理已加载的资源标识,方便重新加载
                Field loadedResourcesField = configuration.getClass().getDeclaredField("loadedResources");
                loadedResourcesField.setAccessible(true);
                Set loadedResourcesSet = ((Set) loadedResourcesField.get(configuration));
                loadedResourcesSet.remove(resource);

                // 重新编译加载资源文件
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, configuration,
                        resource, configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + resource + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }
            if (log.isDebugEnabled()) {
                log.debug("Refresh file: " + item.getAbsolutePath());
                log.debug("Refresh filename: " + item.getName());
            }
        }
        // 如果刷新文件,则修改刷新时间,否则不修改
        if (fileList.size() > 0) {
            this.beforeTime = refreshTime;
        }
    }

    /**
     * 获取需要刷新的文件列表
     *
     * @param dir        目录
     * @param beforeTime 上次刷新时间
     * @return 刷新文件列表
     */
    private List<File> getRefreshFile(File dir, Long beforeTime) {
        List<File> fileList = new ArrayList<>();
        File[] files = dir.listFiles();
        if (files != null) {
            for (File item : files) {
                if (item.isDirectory()) {
                    fileList.addAll(this.getRefreshFile(item, beforeTime));
                } else if (item.isFile()) {
                    if (this.checkFile(item, beforeTime)) {
                        fileList.add(item);
                    }
                } else {
                    log.info("Error file." + item.getName());
                }
            }
        }
        return fileList;
    }

    /**
     * 判断文件是否需要刷新
     *
     * @param file       文件
     * @param beforeTime 上次刷新时间
     * @return 需要刷新返回true,否则返回false
     */
    private boolean checkFile(File file, Long beforeTime) {
        return file.lastModified() > beforeTime;
    }

    /**
     * 重写 org.apache.ibatis.session.Configuration.StrictMap 类
     * 来自 MyBatis3.4.0版本,修改 put 方法,允许反复 put更新。
     */
    public static class StrictMap<V> extends HashMap<String, V> {
        private final String name;

        public StrictMap(String name, int initialCapacity, float loadFactor) {
            super(initialCapacity, loadFactor);
            this.name = name;
        }

        public StrictMap(String name, int initialCapacity) {
            super(initialCapacity);
            this.name = name;
        }

        public StrictMap(String name) {
            super();
            this.name = name;
        }

        public StrictMap(String name, Map<String, ? extends V> m) {
            super(m);
            this.name = name;
        }

        @Override
        @SuppressWarnings("unchecked")
        public V put(String key, V value) {
            // ThinkGem 如果现在状态为刷新,则刷新(先删除后添加)
            if (MapperRefresh.isRefresh()) {
                remove(key);
            }
            // ThinkGem end
            if (containsKey(key)) {
                throw new IllegalArgumentException(name + " already contains value for " + key);
            }
            if (key.contains(".")) {
                final String shortKey = getShortName(key);
                if (super.get(shortKey) == null) {
                    super.put(shortKey, value);
                } else {
                    super.put(shortKey, (V) new Ambiguity(shortKey));
                }
            }
            return super.put(key, value);
        }

        @Override
        public V get(Object key) {
            V value = super.get(key);
            if (value == null) {
                throw new IllegalArgumentException(name + " does not contain value for " + key);
            }
            if (value instanceof Ambiguity) {
                throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name
                        + " (try using the full name including the namespace, or rename one of the entries)");
            }
            return value;
        }

        private String getShortName(String key) {
            final String[] keyparts = key.split("\\.");
            return keyparts[keyparts.length - 1];
        }

        protected static class Ambiguity {
            private final String subject;

            public Ambiguity(String subject) {
                this.subject = subject;
            }

            public String getSubject() {
                return subject;
            }
        }
    }

}

RootConfiguration.java

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @author johnny
 */
@Configuration
@ComponentScan(value = "com.johnny.bbb", excludeFilters = {@ComponentScan.Filter(Controller.class),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {RootConfiguration.class})})
@MapperScan("com.johnny.bbb.common.mapper")
public class RootConfiguration extends SpringBootServletInitializer {
    @javax.annotation.Resource
    private SqlSession sqlSession;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        application.registerShutdownHook(false);
        return application.sources(RootConfiguration.class);
    }

    @PostConstruct
    public void postConstruct() throws IOException {
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*Mapper.xml");
        new MapperRefresh(resources, sqlSession.getConfiguration()).run();
    }
}

启动类WebApplication.java改造,替换掉WebApplication.class

@EnableTransactionManagement
@EnableScheduling
@SpringBootApplication(scanBasePackages = {"com.johnny.web.*"})
@MapperScan("com.johnny.common.mapper")
public class WebApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
    	// 改造
        SpringApplication.run(RootConfiguration.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebApplication.class);
    }

}

参考

Spring Boot+MyBatis热加载mapper.xml文件

posted @ 2021-09-12 00:14  johnny233  阅读(78)  评论(0编辑  收藏  举报  来源