Browse Source

* 修复ehcache缓存在目录/tmp被系统清理导致异常

pull/2962/head
lonecloud 2 years ago
parent
commit
592291de19
No known key found for this signature in database
GPG Key ID: 430D137FD8F84C06
  1. 22
      easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java
  2. 2
      easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java
  3. 19
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java

22
easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java vendored

@ -35,6 +35,8 @@ public class Ehcache implements ReadCache {
private static final CacheManager FILE_CACHE_MANAGER; private static final CacheManager FILE_CACHE_MANAGER;
private static final CacheConfiguration<Integer, ArrayList> FILE_CACHE_CONFIGURATION; private static final CacheConfiguration<Integer, ArrayList> FILE_CACHE_CONFIGURATION;
private static final CacheManager ACTIVE_CACHE_MANAGER; private static final CacheManager ACTIVE_CACHE_MANAGER;
private static final File CACHE_PATH_FILE;
private final CacheConfiguration<Integer, ArrayList> activeCacheConfiguration; private final CacheConfiguration<Integer, ArrayList> activeCacheConfiguration;
/** /**
* Bulk storage data * Bulk storage data
@ -59,9 +61,9 @@ public class Ehcache implements ReadCache {
} }
static { static {
File cacheFile = FileUtils.createCacheTmpFile(); CACHE_PATH_FILE = FileUtils.createCacheTmpFile();
FILE_CACHE_MANAGER = FILE_CACHE_MANAGER =
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)).build(true); CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(CACHE_PATH_FILE)).build(true);
ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true); ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true);
FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class, .newCacheConfigurationBuilder(Integer.class, ArrayList.class,
@ -72,7 +74,21 @@ public class Ehcache implements ReadCache {
@Override @Override
public void init(AnalysisContext analysisContext) { public void init(AnalysisContext analysisContext) {
cacheAlias = UUID.randomUUID().toString(); cacheAlias = UUID.randomUUID().toString();
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION); try {
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
} catch (IllegalStateException e) {
//fix Issue #2693,重建缓存文件夹
if (CACHE_PATH_FILE.exists()) {
throw e;
}
synchronized (Ehcache.class) {
if (!CACHE_PATH_FILE.exists()){
log.debug("cache file dir is not exist retry create");
FileUtils.createDirectory(CACHE_PATH_FILE);
}
}
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
}
activeCache = ACTIVE_CACHE_MANAGER.createCache(cacheAlias, activeCacheConfiguration); activeCache = ACTIVE_CACHE_MANAGER.createCache(cacheAlias, activeCacheConfiguration);
} }

2
easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java

@ -174,7 +174,7 @@ public class FileUtils {
* *
* @param directory * @param directory
*/ */
private static File createDirectory(File directory) { public static File createDirectory(File directory) {
if (!directory.exists() && !directory.mkdirs()) { if (!directory.exists() && !directory.mkdirs()) {
throw new ExcelCommonException("Cannot create directory:" + directory.getAbsolutePath()); throw new ExcelCommonException("Cannot create directory:" + directory.getAbsolutePath());
} }

19
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java

@ -2,14 +2,19 @@ package com.alibaba.easyexcel.test.core.large;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.cache.Ehcache;
import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFSheet;
@ -55,6 +60,7 @@ public class LargeDataTest {
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
} }
@Test @Test
public void t02Fill() { public void t02Fill() {
try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) { try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) {
@ -127,7 +133,18 @@ public class LargeDataTest {
LOGGER.info("{} vs {}", cost, costPoi); LOGGER.info("{} vs {}", cost, costPoi);
Assert.assertTrue(costPoi * 2 > cost); Assert.assertTrue(costPoi * 2 > cost);
} }
@Test
public void t05Read() throws Exception {
long start = System.currentTimeMillis();
EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class,
new LargeDataListener()).headRowNumber(2).sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
FileUtils.delete(new File(System.getProperty(TempFile.JAVA_IO_TMPDIR)));
start = System.currentTimeMillis();
EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class,
new LargeDataListener()).headRowNumber(2).sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
}
private List<LargeData> data() { private List<LargeData> data() {
List<LargeData> list = new ArrayList<>(); List<LargeData> list = new ArrayList<>();
int size = i + 100; int size = i + 100;

Loading…
Cancel
Save