JAXB通过套接字和流 - 读取器块 [英] JAXB through sockets and streams - reader blocks

查看:74
本文介绍了JAXB通过套接字和流 - 读取器块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过网络流发送java bean实例。我想用 JAXB 编组/解组java实例,并使用普通的 OutputStream 将其推送到网络上。

I'm trying to send java bean instances over a network stream. I want to marshal/unmarshal java instances with JAXB and a normal OutputStream to push it over the network.

服务器在unmarshal点等待,但客户端已经很多了。

The servers wait at the unmarshal point but the client is already alot further.

服务器:

inputStream = new BufferedInputStream(this.socket.getInputStream());
outputStream = new BufferedOutputStream(this.socket.getOutputStream());
JAXBContext requestContext = JAXBContext.newInstance(this.requestClass);
Unmarshaller unmarshaller = requestContext.createUnmarshaller();
@SuppressWarnings("unchecked")
K request = (K) unmarshaller.unmarshal(inputStream); //blocks here
respond();

客户:

JAXBContext messageContext = JAXBContext.newInstance(message.getClass());
Marshaller marshaller = messageContext.createMarshaller();
out = new BufferedOutputStream(socket.getOutputStream());
marshaller.marshal(message, out);
out.flush();
waitForResponse();// blocks here

编辑:我切换到正常的输出流但它仍然阻塞。我是否必须发送一些特殊信号告诉JAXB停止解组?如果我关闭客户端输出流,则消息到达服务器端。

I switched to a normal output stream but it still blocks. Do I have to send some special signal to tell JAXB to stop unmarshalling? If I close the client output stream the message arrives at the server side.

推荐答案

我切换到 XMLEventWriter XMLEventWriter ,它的工作原理。我感觉 XMLStreamReader 是错误的。它被卡在某些 skipSpaces()方法上。 XMLStreamReader 的代码看起来好像应该在文档结束时立即返回。

I switched to XMLEventWriter and XMLEventWriter and it works. I've got the feeling XMLStreamReader is buggy. It gets stuck on some skipSpaces() method. The code of XMLStreamReader looks as if it should return as soon as a end of document appears.

这篇关于JAXB通过套接字和流 - 读取器块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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