我正在尝试将消息(字符串)压缩到 zip 文件中,然后将其设置为 Apache Camel 中的交换对象的主体,以便下游服务之一(也使用 Apache Camel)能够提取 zip文件使用exchange.getIn().getBody()
方法。
第一部分很好,我可以将 zip 文件设置body
为. 事实上,它本身就是.exchange.getIn().getBody(ZipFile.class)
null
body
null
为什么会这样?
我试过在正文中发送一个普通的字符串,效果很好。文件(ZipFile)没有设置,想知道为什么。
以下是代码片段 -
路线 -
from(some_route)
.bean(SomeClass.class, "zipAndSend")
.to("activemq:queue:" + somequeue)
压缩文件 -
public void zipAndSend(Exchange exchange) throws Exception {
String incomingMessage;
try {
incomingMessage = exchange.getIn().getBody().toString();
File file = ZipUtil.createFile(incomingMessage);
String zipFilePath = file.getPath().replace(".xml", ".zip");
ZipFile zipFile = ZipUtil.zipFile(file.getPath(), zipFilePath);
exchange.getOut().setHeader("Compressed", "Y");
exchange.getOut().setHeader("ZipFilePath", zipFilePath);
exchange.getOut().setBody(zipFile);
//the body is set correctly here, so far so good
} catch (Exception e) {
e.printStackTrace(); //other operations
}
}
public static File createFile(String incomingMessaage) {
String fileName = "C:\\Project\\ZipUnzipTest\\incoming.xml";
File file = new File(fileName);
try {
FileUtils.writeStringToFile(file, incomingMessaage);
} catch (Exception e) {
//log.error("Error in Writing Message into file " + fileName, e);
String errorFile = fileName.replace("work", "error");
}
return file;
}
在另一个服务(队列末尾)中,我重写了process()
如下所示的方法,以便能够从压缩文件中的文件中提取消息(字符串)。
public void process(Exchange exchange) throws WorkflowDBException,
Exception {
try {
ZipFile zipFile = exchange.getIn().getBody(ZipFile.class); //NPE as body is null
String zipFilePath = exchange.getIn().getHeader("ZipFilePath").toString();
File inFile = ZipUtil.unzipFile(zipFile, "C:\\Project\\ZipUnzipTest\\Output\\WF", true);
String incomingMessage;
incomingMessage = FileUtils.readFileToString(inFile, "UTF-8");
} catch (Exception e) {e.printStackTrace();}
}
依赖项 -
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
我希望正文的内容在in
andout
空间中是相同的。唉,事实并非如此。