在 mule 中写入大文件
Writing large files in mule
我正在尝试将一个大文件写入 mule 中的文件出站端点。我的要求是读取一个大文件,使用 java 转换器对其进行转换并将其写入文件出站端点。我不能使用 datamapper,我的转换逻辑是 complex.The 转换器工作正常,但它似乎没有流式传输。请 suggest.Below 是我的示例代码:
流量:
<spring:beans>
<spring:bean id="responseTransformer" scope="prototype"
class="my.streaming.StreamTransformer">
</spring:bean>
</spring:beans>
<file:connector name="File" autoDelete="false"
streaming="true" validateConnections="true" doc:name="File" />
<flow name="mystreamflow">
<file:inbound-endpoint path="C:\mylocation"
responseTimeout="10000" doc:name="File" connector-ref="File"
pollingFrequency="1000" />
<logger message="OUTPUT:#[payload]" level="INFO" doc:name="Logger" />
<file:outbound-endpoint path="C:\output"
outputPattern="output.txt" responseTimeout="10000" doc:name="File"
transformer-refs="responseTransformer" connector-ref="File" />
</flow>
Java 变压器:
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
InputStream is = (InputStream) message.getPayload();
InputStreamReader isr = new InputStreamReader(is);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedWriter bw = null;
try {
bw = getWriter(bos);
while (isr.read() > 0) {
//transform and write
bw.write("writing something");
bw.flush();
System.out.println("Writing something.....");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bos.close();
if (bw != null) {
bw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bos.toByteArray();
}
private BufferedWriter getWriter(ByteArrayOutputStream bos)
throws IOException {
BufferedWriter bw = null;
try {
BufferedOutputStream bo = new BufferedOutputStream(bos);
bw = new BufferedWriter(new OutputStreamWriter(bo, "UTF-8"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bw;
}
您的转换器 returns 是字节数组而不是 InputStream
,因此它不是流式转换器。这会耗尽内存并导致非常大的文件出现问题。
您需要重写您的转换器,以便 returns 成为 InputStream
。
编辑 - 调查:
- 管道 input/output 流将转换的流输出连接到
PipedInputStream
转换器 returns 到 Mule。当心线程:您需要在传递给 Mule 工作管理器的工作项中执行 PipedOutputStream
编写器代码。
- 创建您自己的
InputStream
子 class,通过逐步读取入站 InputStream
. 逐步生成转换结果
我正在尝试将一个大文件写入 mule 中的文件出站端点。我的要求是读取一个大文件,使用 java 转换器对其进行转换并将其写入文件出站端点。我不能使用 datamapper,我的转换逻辑是 complex.The 转换器工作正常,但它似乎没有流式传输。请 suggest.Below 是我的示例代码:
流量:
<spring:beans>
<spring:bean id="responseTransformer" scope="prototype"
class="my.streaming.StreamTransformer">
</spring:bean>
</spring:beans>
<file:connector name="File" autoDelete="false"
streaming="true" validateConnections="true" doc:name="File" />
<flow name="mystreamflow">
<file:inbound-endpoint path="C:\mylocation"
responseTimeout="10000" doc:name="File" connector-ref="File"
pollingFrequency="1000" />
<logger message="OUTPUT:#[payload]" level="INFO" doc:name="Logger" />
<file:outbound-endpoint path="C:\output"
outputPattern="output.txt" responseTimeout="10000" doc:name="File"
transformer-refs="responseTransformer" connector-ref="File" />
</flow>
Java 变压器:
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
InputStream is = (InputStream) message.getPayload();
InputStreamReader isr = new InputStreamReader(is);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedWriter bw = null;
try {
bw = getWriter(bos);
while (isr.read() > 0) {
//transform and write
bw.write("writing something");
bw.flush();
System.out.println("Writing something.....");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bos.close();
if (bw != null) {
bw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bos.toByteArray();
}
private BufferedWriter getWriter(ByteArrayOutputStream bos)
throws IOException {
BufferedWriter bw = null;
try {
BufferedOutputStream bo = new BufferedOutputStream(bos);
bw = new BufferedWriter(new OutputStreamWriter(bo, "UTF-8"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bw;
}
您的转换器 returns 是字节数组而不是 InputStream
,因此它不是流式转换器。这会耗尽内存并导致非常大的文件出现问题。
您需要重写您的转换器,以便 returns 成为 InputStream
。
编辑 - 调查:
- 管道 input/output 流将转换的流输出连接到
PipedInputStream
转换器 returns 到 Mule。当心线程:您需要在传递给 Mule 工作管理器的工作项中执行PipedOutputStream
编写器代码。 - 创建您自己的
InputStream
子 class,通过逐步读取入站InputStream
. 逐步生成转换结果