JAXB hangs while unmarshaling socket stream

こ雲淡風輕ζ 提交于 2019-12-11 04:36:51

问题


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);
        }
    }
}

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


回答1:


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.



来源:https://stackoverflow.com/questions/10949813/jaxb-hangs-while-unmarshaling-socket-stream

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