KML marshal生成零长度文件 [英] KML marshal produces zero-length files

查看:202
本文介绍了KML marshal生成零长度文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java代码中,我生成一个Thread,它每秒从模拟器收集DIS数据包,尝试格式化KML输出。在我的Java代码中,我正在创建一个包含样式,地标和多边形的文档。每隔一秒我就可以看到marshalAPI例程确实用我的数据创建了一个KML文件。但是,随机有时会创建一个完整的文件,有时它会创建一个零长度文件。即使我关闭DIS数据包生成器并让我的Java代码仍然运行,它仍继续随机创建一个完整的文件,有时会创建一个零长度文件。当我将计时器从每秒更改为每10秒时,行为每10秒发生一次。

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.

我尝试将marshal()用于File对象和OutputStream宾语;同样奇怪的行为。

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

推荐答案

这可能是由于并发访问造成的。创建一个marshaller是一项昂贵的操作,需要时间,据我所知,marshallers不是线程安全的。我在Restlet中使用jaxb连接器,加上JAK,这是一个关于如何创建marshallers并以线程安全方式缓存它们的好例子:

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>() {

查看marshaller如何在同步方法中创建,然后存储在ThreadLocal变量中。

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

这篇关于KML marshal生成零长度文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆