我一直在使用Spring Cloud Dataflow。我编写了一个自定义的Cloud Stream Processor应用程序,该应用程序使用特定类型的XML文档,并将其拆分为较小的XML文档。
我期望下面的Cloud Stream定义写出多个文件。 Instead在使用同一文件进行测试时它偶尔将我的一些较小的XML写入一个文件,有时又将它们写成两个](我认为这是由于我的fixed-delay
值下面的定义)。
我想知道如何让我的流将每个XML文档写入其自己的文件。当我编写处理器时,我专门使用this.processor.output().send(message);
而不是@SendTo(Processor.OUTPUT)
,以为可以避免这个确切的问题。
一如既往,我们将不胜感激。谢谢。
数据流流定义:
xmlSplit=fileIn: file --directory=/root/file_in --filename-regex=redactApplication.xml --fixed-delay=30 --markers-json=false --mode=contents | custom-xml-splitter | fileOut: file --directory=/root/file_out --name-expression="'test' + new java.text.SimpleDateFormat('yyyyMMddHHmmss').format(new java.util.Date()) + '.txt'"
我的自定义处理器的代码:
@Component
@EnableBinding(Processor.class)
public class REDACTXMLSplitter {
@Autowired
private Processor processor;
@SuppressWarnings("unchecked")
@StreamListener(Processor.INPUT)
public void parseForREDACTApplications(byte[] redcactXMLByteArray) {
String redcactXML = new String(redcactXMLByteArray);
InputSource doc = new InputSource( new StringReader( redcactXML ) );
try
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
XPathFactory xfactory = XPathFactory.newInstance();
XPath xpath = xfactory.newXPath();
String xpathQuery = "//REDACT/Application";
xpath = xfactory.newXPath();
XPathExpression query = xpath.compile(xpathQuery);
NodeList productNodesFiltered = (NodeList) query.evaluate(doc, XPathConstants.NODESET);
for (int i=0; i<productNodesFiltered.getLength(); ++i)
{
Document suppXml = dBuilder.newDocument();
//we have to recreate the root node <products>
Element root = suppXml.createElement("REDACT");
Node productNode = productNodesFiltered.item(i);
//we append a product (cloned) to the new file
Node clonedNode = productNode.cloneNode(true);
suppXml.adoptNode(clonedNode); //We adopt the orphan :)
root.appendChild(clonedNode);
suppXml.appendChild(root);
//write out files
//At the end, we save the file XML on disk
// TransformerFactory transformerFactory = TransformerFactory.newInstance();
// Transformer transformer = transformerFactory.newTransformer();
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// DOMSource source = new DOMSource(suppXml);
// StreamResult result = new StreamResult(new File("test_" + i + ".xml"));
// transformer.transform(source, result);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(suppXml), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
System.out.println(output);
Message<String> message = new GenericMessage<>(output);
this.processor.output().send(message);
}
}
catch (XPathExpressionException | ParserConfigurationException | TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
也许文件会突然到达,并且因为接收器步骤正在将其发送到名称为日期时间(以秒为单位)的文件?如果它们在同一秒内到达,则多个xml最终会出现在同一个文件中?