开发者

How can I marshal Objects from a Socket without closing it? (JAXB Marshaling from Inputstream via Socket)

I have tried in many different ways to send my xml document over a socket connection between a server and a client without closing the socket after sending (keep the outputstream open, for sending another document). I have found several sites who claimed that it should work, so I tried it in all the ways they sugested, but I did not found a way which works. (that describes the same what I would like to do: http://jaxb.java.net/guide/Designing_a_client_server_protocol_in_XML.html)

The follwing code works perfectly if I am closing the socket after sending (@code marsh.marshal(element, xsw);), but it stucks on unmarshaling on the server side, if I try to keep the socket open.

Client Side....

public void sendMessage(String message){
         JAXBContext jaxbContext;   
        try {
            jaxbContext = JAXBContext.newInstance("cdl.wizard.library");
            Marshaller marsh = jaxbContext.createMarshaller();
            marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marsh.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.org/WizardShema WizardsSchema.xsd");
            ObjectFactory of = new ObjectFactory();
                    // the Dataset is the root element of the xml document
            Dataset set = new Dataset("CONN01", "CONTR", "MCL01@localhost", "SV01@localhost:32000");
            CommandSet cmdSet = new CommandSet();
            Command cmd = new Command();
            cmd.setFunctionName("RegisterAs");
            Param p = new Param();
            p.setString("RemoteCli开发者_如何学编程ent");
            cmd.addParameter(p);
            cmdSet.addCommand(cmd);
            set.setInstruction(cmdSet);
                    // creates a valid xml dataset, with startDocument, startElement...
            JAXBElement<Dataset> element = of.createData(set);
            XMLStreamWriter xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(mOOS);
            marsh.marshal(element, xsw);
            xsw.flush();
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
        }

SERVER Side....

private void handleMessage() {
        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance("cdl.wizard.library") ;
            Unmarshaller um = jaxbContext.createUnmarshaller();
            XMLInputFactory xmlif = XMLInputFactory.newInstance();
            // XMLEventReader xmlr = xmlif.createXMLEventReader(mOIS);
            XMLStreamReader xmlr = xmlif.createXMLStreamReader(mOIS, "UTF8");
            // move to the root element and check its name.
            xmlr.nextTag();
            System.out.println("TagName:" + xmlr.getLocalName());
            xmlr.require(START_ELEMENT, null, "Data");

            JAXBElement<Dataset> obj = um.unmarshal(xmlr, Dataset.class);
            Dataset set = obj.getValue();
            System.out.println("ID:"+ set.getID());

        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜