KML marshal produces zero-length files

烂漫一生 提交于 2019-12-23 02:56:16

问题


In Java code, I spawn a Thread which every second gathers DIS packets from a simulator, and tries to format KML output. In my Java code I am creating a Document, with Styles and Placemarks and Polygons. Once every second I can see that the "marshal" API routine does indeed create a KML file with my data. However, randomly sometimes it creates a complete file, and sometimes it creates a zero-length file. Even when I turn my DIS packet generator off, and leave my Java code still running, it continues to randomly create a complete file, and sometimes create a zero-length file. When I change my timer from every second to every 10 seconds, the behavior occurs every 10 seconds.

I tried using marshal() to a File object, and to an OutputStream object; Same weird behavior.


回答1:


It could be due to concurrent access. Creating a marshaller is an expensive operation, it takes time and as far as I know marshallers are not threadsafe. I use the jaxb connector in Restlet, coupled with JAK, and that's a good example on how to create marshallers and cache them in a threadsafe way:

/** Use thread identity to preserve safety of access to marshalers. */
private final ThreadLocal<javax.xml.bind.Marshaller> marshaller = new ThreadLocal<javax.xml.bind.Marshaller>() {

See there how the marshaller is created in a synchronized method and then stored in a ThreadLocal variable.



来源:https://stackoverflow.com/questions/12267393/kml-marshal-produces-zero-length-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!