Geotools中CRS.decode("EPSG:4326")执行卡顿问题求助
Geotools中CRS.decode("epsg:4326")卡顿/疑似死锁的解决思路
问题背景
使用Maven + Java 11环境,调用CRS.decode("epsg:4326")时出现卡顿,疑似死锁。尝试过升级Geotools至18版本、添加线程超时执行机制(代码如下)均未解决,最终通过添加gt-referencing依赖解决问题,现询问其他可行解决思路。
尝试过的超时线程代码
ExecutorService executorService = Executors.newSingleThreadExecutor(); while(systemCrs == null) { Callable<CoordinateReferenceSystem> task = new CRSDecoderTask(); Future<CoordinateReferenceSystem> submit = executorService.submit(task); try { CoordinateReferenceSystem coordinateReferenceSystem = submit.get(5, TimeUnit.SECONDS); if (coordinateReferenceSystem != null) { executorService.shutdown(); systemCrs = coordinateReferenceSystem; } } catch (TimeoutException e) { logger.debug("crs timeout", e); submit.cancel(true); } }
private class CRSDecoderTask implements Callable<CoordinateReferenceSystem> { @Override public CoordinateReferenceSystem call() throws Exception { return CRS.decode("espg-4326"); // 注:原代码拼写错误,应为epsg:4326 } }
当前依赖(已添加gt-referencing解决问题)
<artifactId>gt-main</artifactId> <artifactId>gt-shapefile</artifactId> <artifactId>gt-epsg-hsql</artifactId> <artifactId>gt-geometry</artifactId> <artifactId>gt-epsg-wkt</artifactId> <artifactId>gt-referencing</artifactId> <!-- 添加后解决问题 -->
其他备选解决思路
强制指定EPSG数据源类型
Geotools加载EPSG坐标系统时会自动选择数据源,若gt-epsg-hsql的嵌入式数据库初始化异常,可强制使用WKT数据源规避:// 全局配置 System.setProperty("org.geotools.referencing.factory", "org.geotools.referencing.factory.wkt.WKTCRSAuthorityFactory"); // 或代码中显式指定工厂 CRSAuthorityFactory factory = CRS.getAuthorityFactory(true); CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326");排查线程死锁细节
用JDK工具定位真实死锁原因:- 执行
jstack <进程PID>导出线程栈,搜索BLOCKED状态线程,查看是否存在循环等待锁的情况,重点关注EPSG数据源初始化相关线程。 - 用
jvisualvm或jconsole监控线程状态,直接定位死锁位置。
- 执行
清理EPSG缓存
Geotools首次加载EPSG数据会生成本地缓存,缓存损坏可能导致初始化卡顿。删除用户目录下的.geotools/epsg-hsql缓存目录,让程序重新初始化数据源。替换EPSG数据源实现
若gt-epsg-hsql的嵌入式数据库存在问题,可替换为其他数据源:- 使用
gt-epsg-postgresql(需配置PostgreSQL数据库存储EPSG标准数据) - 切换为纯内存数据源,避免磁盘IO引发的卡顿。
- 使用
排查依赖冲突
执行mvn dependency:tree查看依赖树,确认是否存在旧版本Geotools模块(尤其是gt-referencing)被间接引入的情况,通过<exclusions>排除冲突依赖。
内容的提问来源于stack exchange,提问作者sh l
相关产品推荐
相关产品推荐

