Skip to content

Commit

Permalink
Merge pull request #257 from liaochong/Hotfix/3.9.1
Browse files Browse the repository at this point in the history
1. 修复异常情况下消费者线程未被关闭问题
  • Loading branch information
liaochong authored Jul 4, 2020
2 parents bdf5813 + 902d0c9 commit 4d966a1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>3.9.0</version>
<version>3.9.1</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import com.github.liaochong.myexcel.core.strategy.WidthStrategy;
import com.github.liaochong.myexcel.core.templatehandler.TemplateHandler;
import com.github.liaochong.myexcel.utils.ReflectUtil;
import lombok.NonNull;
import com.github.liaochong.myexcel.utils.TempFileOperator;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedList;
Expand Down Expand Up @@ -66,6 +68,10 @@ public class DefaultStreamExcelBuilder<T> extends AbstractSimpleExcelBuilder imp
* workbook
*/
private Workbook workbook;
/**
* 待追加excel
*/
private Path excel;
/**
* 文件分割,excel容量
*/
Expand All @@ -91,10 +97,10 @@ public class DefaultStreamExcelBuilder<T> extends AbstractSimpleExcelBuilder imp
*/
private TemplateHandler templateHandler;

private List<CompletableFuture<Void>> asyncAppendFutures = new LinkedList<>();
private final List<CompletableFuture<Void>> asyncAppendFutures = new LinkedList<>();

private DefaultStreamExcelBuilder(Class<T> dataType) {
this(dataType, null);
this(dataType, (Workbook) null);
}

private DefaultStreamExcelBuilder(Class<T> dataType, Workbook workbook) {
Expand All @@ -105,14 +111,22 @@ private DefaultStreamExcelBuilder(Class<T> dataType, Workbook workbook) {
this.isMapBuild = dataType == Map.class;
}

private DefaultStreamExcelBuilder(Class<T> dataType, Path excel) {
super(false);
this.dataType = dataType;
this.excel = excel;
configuration.setWidthStrategy(WidthStrategy.NO_AUTO);
this.isMapBuild = dataType == Map.class;
}

/**
* 获取实例,设定需要渲染的数据的类类型
*
* @param dataType 数据的类类型
* @param <T> T
* @return DefaultStreamExcelBuilder
*/
public static <T> DefaultStreamExcelBuilder<T> of(@NonNull Class<T> dataType) {
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType) {
return new DefaultStreamExcelBuilder<>(dataType);
}

Expand All @@ -124,10 +138,34 @@ public static <T> DefaultStreamExcelBuilder<T> of(@NonNull Class<T> dataType) {
* @param <T> T
* @return DefaultStreamExcelBuilder
*/
public static <T> DefaultStreamExcelBuilder<T> of(@NonNull Class<T> dataType, @NonNull Workbook workbook) {
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType, Workbook workbook) {
return new DefaultStreamExcelBuilder<>(dataType, workbook);
}

/**
* 获取实例,设定需要渲染的数据的类类型
*
* @param dataType 数据的类类型
* @param excel excel
* @param <T> T
* @return DefaultStreamExcelBuilder
*/
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType, Path excel) {
return new DefaultStreamExcelBuilder<>(dataType, excel);
}

/**
* 获取实例,设定需要渲染的数据的类类型
*
* @param dataType 数据的类类型
* @param excelInputStream excelInputStream
* @param <T> T
* @return DefaultStreamExcelBuilder
*/
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType, InputStream excelInputStream) {
return of(dataType, TempFileOperator.convertToFile(excelInputStream));
}

/**
* 已过时,请使用of方法代替
* 4.0版本移除
Expand All @@ -151,22 +189,22 @@ public static DefaultStreamExcelBuilder<Map> getInstance(Workbook workbook) {
return new DefaultStreamExcelBuilder<>(Map.class, workbook);
}

public DefaultStreamExcelBuilder<T> titles(@NonNull List<String> titles) {
public DefaultStreamExcelBuilder<T> titles(List<String> titles) {
this.titles = titles;
return this;
}

public DefaultStreamExcelBuilder<T> sheetName(@NonNull String sheetName) {
public DefaultStreamExcelBuilder<T> sheetName(String sheetName) {
configuration.setSheetName(sheetName);
return this;
}

public DefaultStreamExcelBuilder<T> fieldDisplayOrder(@NonNull List<String> fieldDisplayOrder) {
public DefaultStreamExcelBuilder<T> fieldDisplayOrder(List<String> fieldDisplayOrder) {
this.fieldDisplayOrder = fieldDisplayOrder;
return this;
}

public DefaultStreamExcelBuilder<T> workbookType(@NonNull WorkbookType workbookType) {
public DefaultStreamExcelBuilder<T> workbookType(WorkbookType workbookType) {
if (workbook != null) {
throw new IllegalArgumentException("Workbook type confirmed, not modifiable");
}
Expand All @@ -184,13 +222,13 @@ public DefaultStreamExcelBuilder<T> noStyle() {
return this;
}

public DefaultStreamExcelBuilder<T> widthStrategy(@NonNull WidthStrategy widthStrategy) {
public DefaultStreamExcelBuilder<T> widthStrategy(WidthStrategy widthStrategy) {
configuration.setWidthStrategy(widthStrategy);
return this;
}

@Deprecated
public DefaultStreamExcelBuilder<T> autoWidthStrategy(@NonNull AutoWidthStrategy autoWidthStrategy) {
public DefaultStreamExcelBuilder<T> autoWidthStrategy(AutoWidthStrategy autoWidthStrategy) {
configuration.setWidthStrategy(AutoWidthStrategy.map(autoWidthStrategy));
return this;
}
Expand Down Expand Up @@ -298,6 +336,15 @@ public DefaultStreamExcelBuilder<T> start() {
if (head != null) {
htmlToExcelStreamFactory.appendTitles(head);
}
if (excel != null && Files.exists(excel)) {
log.info("start reading existing excel data.");
SaxExcelReader<T> reader = SaxExcelReader.of(dataType)
.readAllSheet();
if (titleLevel > 0) {
reader.rowFilter(row -> row.getRowNum() > titleLevel - 1);
}
reader.readThen(excel.toFile(), (Consumer<T>) this::append);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ public void cancel() {
}

public void clear() {
if (receiveThread != null && receiveThread.isAlive()) {
receiveThread.interrupt();
}
closeWorkbook();
TempFileOperator.deleteTempFiles(tempFilePaths);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.cache.StringsCache;
import com.github.liaochong.myexcel.core.constant.Constants;
import com.github.liaochong.myexcel.exception.ExcelReadException;
import com.github.liaochong.myexcel.exception.SaxReadException;
import com.github.liaochong.myexcel.exception.StopReadException;
Expand All @@ -37,7 +36,6 @@
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand Down Expand Up @@ -177,29 +175,14 @@ public void readThen(File file, Function<T, Boolean> function) {
}

private void doRead(InputStream fileInputStream) {
Path path = this.convertToFile(fileInputStream);
Path path = TempFileOperator.convertToFile(fileInputStream);
try {
doRead(path.toFile());
} finally {
TempFileOperator.deleteTempFile(path);
}
}

private Path convertToFile(InputStream is) {
Path tempFile = TempFileOperator.createTempFile("i_t", Constants.XLSX);
try (FileOutputStream fos = new FileOutputStream(tempFile.toFile())) {
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
TempFileOperator.deleteTempFile(tempFile);
throw new SaxReadException("Fail to convert file inputStream to temp file", e);
}
return tempFile;
}

private void doRead(File file) {
FileMagic fm;
try (InputStream is = FileMagic.prepareToCheckMagic(new FileInputStream(file))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@


import com.github.liaochong.myexcel.core.MyExcelConfiguration;
import com.github.liaochong.myexcel.core.constant.Constants;
import com.github.liaochong.myexcel.exception.ExcelBuildException;
import com.github.liaochong.myexcel.exception.SaxReadException;
import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
Expand Down Expand Up @@ -106,4 +110,19 @@ public static void deleteTempFile(Path path) {
}
}

public static Path convertToFile(InputStream is) {
Path tempFile = createTempFile("i_t", Constants.XLSX);
try (FileOutputStream fos = new FileOutputStream(tempFile.toFile())) {
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
TempFileOperator.deleteTempFile(tempFile);
throw new SaxReadException("Fail to convert file inputStream to temp file", e);
}
return tempFile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.File;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -243,6 +244,17 @@ void appendTemplateBuild() throws Exception {
}
}

@Test
void appendExistExcel() throws Exception {
try (DefaultStreamExcelBuilder<CommonPeople> excelBuilder = DefaultStreamExcelBuilder.of(CommonPeople.class, Paths.get(TEST_OUTPUT_DIR + "common_build.xlsx"))
.fixedTitles()
.start()) {
data(excelBuilder, 5000);
Workbook workbook = excelBuilder.build();
FileExportUtil.export(workbook, new File(TEST_OUTPUT_DIR + "common_build.xlsx"));
}
}

private void data(DefaultStreamExcelBuilder<CommonPeople> excelBuilder, int size) {
BigDecimal oddMoney = new BigDecimal(109898);
BigDecimal evenMoney = new BigDecimal(66666);
Expand Down

0 comments on commit 4d966a1

Please sign in to comment.