gpt4 book ai didi

java - Apache Camel - Exchange.getIn().getBody(ZipFile.class) 返回 NULL 但 Exchange.getOut().setBody(zipfile) 工作正常

转载 作者:行者123 更新时间:2023-12-02 01:58:12 27 4
gpt4 key购买 nike

我正在尝试将消息(字符串)压缩到 zip 文件中,然后将其设置为 Apache Camel 中交换对象的主体,以便下游服务之一(也使用 Apache Camel)能够使用 exchange.getIn().getBody() 方法提取 zip 文件。

第一部分很好,我可以将 zip 文件设置为 body,但是当我尝试在队列的另一端(Active MQ)检索该文件时,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>

我希望inout 空间中的正文内容相同。唉,事实并非如此。

最佳答案

事实证明,Camel(以及其他框架)可以很好地处理bytes[]。我将 ZipFile 转换为 字节数组,并用它来设置 exchange 对象的 body

incomingMessage = exchange.getIn().getBody().toString();
File file = ZipUtil.createFile(incomingMessage);
String zipFilePath = file.getPath().replace(".xml", ".zip");
ZipFile zipFile = ZipUtil.zipFile(file.getPath(), zipFilePath);
messageData = FileUtils.readFileToByteArray(new File(zipFilePath));

exchange.getOut().setHeader("Compressed", "Y");
exchange.getOut().setHeader("ZipFilePath", zipFilePath);
exchange.getOut().setBody(messageData);

在阅读时,我使用 ZipInputStreamByteArrayInputStream 获取 Zip Entry

byte [] bytes = exchange.getIn().getBody(byte[].class);
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));
try {
StringBuilder s = new StringBuilder();
StringBuilder temp = new StringBuilder();
byte[] buffer = new byte[1024];
int read = 0;
ZipEntry entry;
while ((entry = zipStream.getNextEntry())!= null) {
while ((read = zipStream.read(buffer, 0, 1024)) >= 0) {
s.append(new String(buffer, 0, read));
}
temp = temp.append(s);
s.setLength(0);
}
return temp.toString();
} catch (Exception e) {
e.printStackTrace();
}

是的,就是这样。仍然愿意接受其他方法来解决这个问题:)

关于java - Apache Camel - Exchange.getIn().getBody(ZipFile.class) 返回 NULL 但 Exchange.getOut().setBody(zipfile) 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57388695/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com