Skip to content

Commit 8c1a678

Browse files
authored
Merge pull request #87 from alipay/20230324-template
writer buffix
2 parents 668f79a + 68094fe commit 8c1a678

File tree

8 files changed

+74
-14
lines changed

8 files changed

+74
-14
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<url>https://github.com/alipay/rdf-file</url>
1212

1313
<properties>
14-
<rdf.file.core.version>2.2.10</rdf.file.core.version>
15-
<rdf.file.oss.version>2.2.10</rdf.file.oss.version>
16-
<rdf.file.sftp.version>2.2.10</rdf.file.sftp.version>
14+
<rdf.file.core.version>2.2.11</rdf.file.core.version>
15+
<rdf.file.oss.version>2.2.11</rdf.file.oss.version>
16+
<rdf.file.sftp.version>2.2.11</rdf.file.sftp.version>
1717
</properties>
1818

1919
<modules>
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
}

rdf-file-test/src/main/resources/pom/rdf-file-core.pom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.alipay.rdf.file</groupId>
66
<artifactId>rdf-file-core</artifactId>
7-
<version>2.2.10</version>
7+
<version>2.2.11</version>
88
<name>RDF FILE CORE</name>
99
<description>RDF FILE CORE</description>
1010
<url>https://github.com/alipay/rdf-file</url>

rdf-file-test/src/main/resources/pom/rdf-file-oss.pom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.alipay.rdf.file</groupId>
66
<artifactId>rdf-file-oss</artifactId>
7-
<version>2.2.10</version>
7+
<version>2.2.11</version>
88
<name>RDF FILE OSS</name>
99
<description>RDF FILE OSS</description>
1010
<url>https://github.com/alipay/rdf-file</url>

rdf-file-test/src/main/resources/pom/rdf-file-sftp.pom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.alipay.rdf.file</groupId>
66
<artifactId>rdf-file-sftp</artifactId>
7-
<version>2.2.10</version>
7+
<version>2.2.11</version>
88
<name>RDF FILE SFTP</name>
99
<description>RDF FILE SFTP</description>
1010
<url>https://github.com/alipay/rdf-file</url>

0 commit comments

Comments
 (0)