我正在尝试将一个大文件写入mule中的File outbound endpoint。我的要求是读取一个大文件,使用java转换器转换它并将其写入文件出站端点。我不能使用datamapper,我的转换逻辑很复杂。转换器工作正常,但似乎没有流。请建议。下面是我的示例代码:
流量:
<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;
}
答案 0 :(得分:1)
您的变换器返回一个字节数组而不是InputStream
,因此它不是一个流变换器。这将耗尽内存并导致非常大的文件出现问题。
您需要重写变换器,使其返回InputStream
。
编辑 - 调查:
PipedInputStream
的管道输入/输出流返回到Mule。谨防线程:您需要在传递给Mule工作管理器的工作项中执行PipedOutputStream
编写器代码。InputStream
子类,逐步读取入站InputStream
,逐步生成转换后的结果。