Skip to content

Commit 4bedee7

Browse files
author
hongwei.quhw
committed
writer buffix
1 parent 668f79a commit 4bedee7

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Ant Group
3+
* Copyright (c) 2004-2023 All Rights Reserved.
4+
*/
5+
package com.alipay.rdf.file.common;
6+
7+
import com.alipay.rdf.file.interfaces.FileFactory;
8+
import com.alipay.rdf.file.interfaces.FileWriter;
9+
import com.alipay.rdf.file.model.FileConfig;
10+
import com.alipay.rdf.file.util.RdfFileUtil;
11+
12+
/**
13+
* @author quhongwei
14+
* @version FileWriterTemplate.java, v 0.1 2023年03月24日 2:07 下午 quhongwei Exp $
15+
*/
16+
public abstract class FileWriterTemplate {
17+
private final FileWriter fileWriter;
18+
19+
public FileWriterTemplate(FileConfig fileConfig) {
20+
this.fileWriter = FileFactory.createWriter(fileConfig);
21+
}
22+
23+
public void process() throws Throwable {
24+
try {
25+
doProcess(fileWriter);
26+
} catch (Throwable t) {
27+
RdfFileUtil.setWriteError(fileWriter);
28+
29+
if (t instanceof RuntimeException) {
30+
throw t;
31+
} else {
32+
throw new RuntimeException("rdf-file#FileWriterTemplate file write error.", t);
33+
}
34+
} finally {
35+
fileWriter.close();
36+
}
37+
}
38+
39+
protected abstract void doProcess(FileWriter fileWriter) throws Throwable;
40+
}

rdf-file-core/src/main/java/com/alipay/rdf/file/common/FileWriterWrapper.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public void writeHead(Object headBean) {
3333
} catch (RuntimeException e) {
3434
hasError = true;
3535
throw e;
36-
} catch (Exception e) {
36+
} catch (Throwable e) {
37+
hasError = true;
3738
throw new RdfFileException("rdf-file#FileWriterWrapper writeHead error filePath=["
3839
+ getFileConfig().getFilePath() + "], head=[" + headBean
3940
+ "]",
@@ -48,7 +49,8 @@ public void writeRow(Object rowBean) {
4849
} catch (RuntimeException e) {
4950
hasError = true;
5051
throw e;
51-
} catch (Exception e) {
52+
} catch (Throwable e) {
53+
hasError = true;
5254
throw new RdfFileException("rdf-file#FileWriterWrapper writeRow error filePath=["
5355
+ getFileConfig().getFilePath() + "], row=[" + rowBean + "]",
5456
e, RdfErrorEnum.UNKOWN);
@@ -62,7 +64,8 @@ public void writeTail(Object tailBean) {
6264
} catch (RuntimeException e) {
6365
hasError = true;
6466
throw e;
65-
} catch (Exception e) {
67+
} catch (Throwable e) {
68+
hasError = true;
6669
throw new RdfFileException("rdf-file#FileWriterWrapper writeTail error filePath=["
6770
+ getFileConfig().getFilePath() + "], tail=[" + tailBean
6871
+ "]",
@@ -77,7 +80,8 @@ public void writeLine(String line) {
7780
} catch (RuntimeException e) {
7881
hasError = true;
7982
throw e;
80-
} catch (Exception e) {
83+
} catch (Throwable e) {
84+
hasError = true;
8185
throw new RdfFileException("rdf-file#FileWriterWrapper writeLine error filePath=["
8286
+ getFileConfig().getFilePath() + "], line=[" + line + "]",
8387
e, RdfErrorEnum.UNKOWN);
@@ -111,7 +115,8 @@ public void append(InputStream in) {
111115
} catch (RuntimeException e) {
112116
hasError = true;
113117
throw e;
114-
} catch (Exception e) {
118+
} catch (Throwable e) {
119+
hasError = true;
115120
throw new RdfFileException("rdf-file#FileWriterWrapper append error filePath=["
116121
+ getFileConfig().getFilePath() + "]",
117122
e, RdfErrorEnum.UNKOWN);
@@ -125,7 +130,8 @@ public void ensureOpen() {
125130
} catch (RuntimeException e) {
126131
hasError = true;
127132
throw e;
128-
} catch (Exception e) {
133+
} catch (Throwable e) {
134+
hasError = true;
129135
throw new RdfFileException("rdf-file#FileWriterWrapper ensureOpen error filePath=["
130136
+ getFileConfig().getFilePath() + "]",
131137
e, RdfErrorEnum.UNKOWN);
@@ -137,4 +143,7 @@ public FileConfig getFileConfig() {
137143
return writer.getFileConfig();
138144
}
139145

146+
public void setHasError(boolean hasError) {
147+
this.hasError = hasError;
148+
}
140149
}

rdf-file-core/src/main/java/com/alipay/rdf/file/common/ProtocolFileMerger.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636

3737
/**
3838
* Copyright (C) 2013-2018 Ant Financial Services Group
39-
*
39+
*
4040
* 协议文件合并
41-
*
41+
*
4242
* @author hongwei.quhw
4343
* @version $Id: ProtocolFileMerger.java, v 0.1 2017年8月10日 下午8:01:11 hongwei.quhw Exp $
4444
*/
@@ -85,6 +85,9 @@ public void merge(MergerConfig config) {
8585
RdfProfiler.enter("rdf-file#merge tail start...");
8686
mergeTail(config);
8787
RdfProfiler.release("rdf-file#merge tail end");
88+
} catch (Throwable e) {
89+
RdfFileUtil.setWriteError(fileWriter);
90+
throw new RuntimeException("rdf-file#ProtocolFileMerger error.", e);
8891
} finally {
8992
if (null != fileWriter) {
9093
fileWriter.close();

rdf-file-core/src/main/java/com/alipay/rdf/file/util/RdfFileUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.alipay.rdf.file.util;
22

3+
import com.alipay.rdf.file.common.FileWriterWrapper;
34
import com.alipay.rdf.file.exception.RdfErrorEnum;
45
import com.alipay.rdf.file.exception.RdfFileException;
6+
import com.alipay.rdf.file.interfaces.FileWriter;
57
import com.alipay.rdf.file.loader.ProtocolLoader;
68
import com.alipay.rdf.file.loader.TemplateLoader;
79
import com.alipay.rdf.file.meta.FileMeta;
@@ -906,4 +908,10 @@ public static boolean isRelationReadRowCompatibility(FileConfig fileConfig) {
906908

907909
return false;
908910
}
911+
912+
public static void setWriteError(FileWriter fileWriter) {
913+
if (fileWriter instanceof FileWriterWrapper) {
914+
((FileWriterWrapper) fileWriter).setHasError(true);
915+
}
916+
}
909917
}

0 commit comments

Comments
 (0)