解组套接字流时JAXB挂起

编程入门 行业动态 更新时间:2024-10-26 00:24:53
本文介绍了解组套接字流时JAXB挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

马歇尔和解读读者和作家的问题。所以这就是。这就是我给PrintWriter编写内容的方法。

Have an issue with marshall and unmarshall readers and writers. So here it is. This is how i marshall something to the PrintWriter.

try { JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); //jaxbMarshaller.setProperty(, value) //jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); jaxbMarshaller.marshal(protocol, out); } catch (JAXBException e) { LOG.error("Error while processing protocol"+e); }

这就是我得到它的方式:

This is how i get it:

private BufferedReader in; private PrintWriter out; private String buffer; private StringBuffer stringBuffer; public ServerThread(Socket s) throws IOException { socket = s; in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); start(); } public void run() { try { while (true) { // LOG.trace("Waiting for input stream ready"); /* * if (in.readLine().endsWith(">")) { * LOG.trace("here we go"+buffer); * JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class); * Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); * XMLProtocol protocol = (XMLProtocol) jaxbUnmarshaller.unmarshal(in); * LOG.trace("Nop it no the end " + protocol.getContent()); * } */ if (in.ready()) { LOG.trace("BufferedReader - ready"); buffer += in.readLine(); if (buffer.endsWith("</XMLProtocol>")) { LOG.trace("Wish you were here1"+in); JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); XMLProtocol protocol = (XMLProtocol) jaxbUnmarshaller.unmarshal(in); LOG.trace("Getting message from user " + protocol.getContent()); } else { LOG.trace("Nop it no the end " + buffer); } } } } catch (IOException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } finally { try { socket.close(); LOG.trace("Socket closed"); } catch (IOException e) { LOG.error("Socket no closed" + e); } } }

然后这个Unmarshall操作挂起。并且它甚至没有显示任何异常。

Then this Unmarshall operation hangs. And it show me nothing even Exception.

推荐答案

是的,我发现了这个问题的解决方案! JAXB不是SOCKET流的最佳决策,因为它等待流的结束。解决方案是使用清晰的SAX解析器,您可以在其中编写自己的ParserHandler(DefaultHandler.class),当您到达最后一个标记时,应该抛出自己的异常,表示停止解析。

Yeah i found solvation of this issue! JAXB is not the best decision to SOCKET streams, cause it wait the end of the stream. Solution is to use clear SAX parser where you can to write your own ParserHandler (DefaultHandler.class) and when you reach your last tag, you should to throw your own exception which indicates to stop parsing.

更多推荐

解组套接字流时JAXB挂起

本文发布于:2023-11-25 12:31:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:挂起   字流时   JAXB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!