gpt4 book ai didi

java - 如何确定 JAXB 已完成编码到文件

转载 作者:行者123 更新时间:2023-12-02 11:57:43 26 4
gpt4 key购买 nike

在服务器上,有一个进程每隔特定的时间间隔(例如 5 分钟)从特定的目录中获取文件。正在拾取的文件是由 Web 服务生成的。JAXB 编码将文件从对象转换为 xml 文件。问题是经常发生文件在完成之前就被拾取的情况。解决方案是将文件放在临时目录中或给它们一个临时的特定扩展名,以便让轮询过程知道跳过这些文件。然后,可以将文件移动到处理目录或更改扩展名,以便轮询过程可以拾取它们。

该方法如下所示;

    private void writeToFile(Object obj, String outputFileName) {
LOG.debug("Executing operation writeToFile using filename '{}' and object of type '{}'.", outputFileName, obj.getClass());

try {
Marshaller jaxbMarshaller = JAXB_CONTEXT.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(obj, new File(outputFileName));
LOG.debug("Wrote request to file '{}'.", outputFileName);
} catch (JAXBException e) {
LOG.error("Exception occurred while writing request to file:", e);
}
LOG.debug("Done executing operation writeToFile.");
}

我的问题是确定编码过程是否已完成,以便可以释放文件以进行进一步处理?

最佳答案

请从此链接下载代码 [ https://turreta.com/2017/03/07/jaxb-perform-pre-and-post-processing-with-unmarshaller-listener/]将下面的 Person Marshall Listener 代码添加到源中,因为每个节点都会调用此代码,在 Marshall 方法之前和之后都检查了 Person 内部的根节点实例(需要将其修改为基本节点),还使用了count 为 2,因为起始节点和结束节点每次都会调用此函数,因为仅应在结束节点检查 count == 2 时调用此函数

package com.turreta.jaxb.unmarshaller.listener;

public class PersonMarshallListener extends javax.xml.bind.Marshaller.Listener {

int beforeCount = 1;
int afterCount = 1;

@Override
public void beforeMarshal(Object source) {
super.beforeMarshal(source);
if (source instanceof Person) {

if (beforeCount == 2) {
beforeCount = 1;
System.out.println("BEFORE MARSHAL");
} else {
beforeCount++;
}
}

}

@Override
public void afterMarshal(Object source) {
super.afterMarshal(source);

if (source instanceof Person) {

if (afterCount == 2) {

afterCount = 1;
System.out.println("AFTER MARSHAL");
// System.out.println("This will be called once the marshall has been completed");
} else {
afterCount++;
}
}


}

}

并将 DemoApp 替换为以下代码

package com.turreta.jaxb.unmarshaller.listener;

import java.io.FileInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

public class DemoApp {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Person.class);

XMLInputFactory xif = XMLInputFactory.newFactory();
FileInputStream xml = new FileInputStream("src/main/resources/person.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);

Unmarshaller unmarshaller = jc.createUnmarshaller();
PersonUnmarshallListener pul = new PersonUnmarshallListener();
unmarshaller.setListener(pul);

Person person = (Person) unmarshaller.unmarshal(xsr);
System.out.println(person);

Marshaller marshaller = jc.createMarshaller();
marshaller.setListener(new PersonMarshallListener());
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(person, System.out);


}
}

关于java - 如何确定 JAXB 已完成编码到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471082/

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