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

标签 java xml jaxb marshalling

在服务器上,有一个进程每隔特定的时间间隔(例如 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/

相关文章:

java - 相同的 org.w3c.dom.Document 变量的行为不同

java - java中的非重写方法?

java - GWT 由 Manuel Carrasco 上传 Moñino Issue

javascript - 使用 Javascript 读取 XML

java - 为 MOXy JSON 编码重命名元素名称

java - 制作一个java程序来通知我的方法

c# - 使用 Linq,如何将 Xml 解析为仅在构造函数中接受参数的 C# 对象?

Excel 中的 XML 命名空间

java - 如何使用 JPA (Java EE) 将 XML 数据结构映射到数据库

mysql - 具有 JPA 实体和 JAXB 的 rest(JAX-RS) Web 服务