问题
I want to send a request over TCP transport to a proxy service, when use this code to send my soap xml
Socket clientSocket = new Socket("host", port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes("soap xml instance");
clientSocket.close();
it works fine and my business is continues.
But when i send two xml without closing the socket like this:
Socket clientSocket = new Socket("host", port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes("soap xml instance");
outToServer.writeBytes("another soap instance");
clientSocket.close();
it always throws this exception:
TID: [0] [ESB] [2013-06-28 13:36:10,838] ERROR {org.apache.axis2.transport.tcp.TCPWorker} - Error while processing TCP request through the Axis2 engine {org.apache.axis2.transport.tcp.TCPWorker} org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs. at [row,col {unknown-source}]: [2,5] at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296) at org.apache.axiom.om.impl.llom.OMDocumentImpl.buildNext(OMDocumentImpl.java:135) at org.apache.axiom.om.impl.llom.OMNodeImpl.getNextOMSibling(OMNodeImpl.java:122) at org.apache.axiom.om.impl.llom.OMElementImpl.getNextOMSibling(OMElementImpl.java:343) at org.apache.axiom.om.impl.traverse.OMChildrenIterator.getNextNode(OMChildrenIterator.java:36) at org.apache.axiom.om.impl.traverse.OMAbstractIterator.hasNext(OMAbstractIterator.java:58) at org.jaxen.util.DescendantAxisIterator.hasNext(DescendantAxisIterator.java:101) at org.jaxen.expr.DefaultStep.evaluate(DefaultStep.java:152) at org.jaxen.expr.DefaultLocationPath.evaluate(DefaultLocationPath.java:140) at org.jaxen.expr.DefaultAbsoluteLocationPath.evaluate(DefaultAbsoluteLocationPath.java:113) at org.jaxen.expr.DefaultXPathExpr.asList(DefaultXPathExpr.java:102) at org.jaxen.BaseXPath.selectNodesForContext(BaseXPath.java:674) at org.jaxen.BaseXPath.selectNodes(BaseXPath.java:213) at org.jaxen.BaseXPath.evaluate(BaseXPath.java:172) at org.apache.synapse.util.xpath.SynapseXPath.stringValueOf(SynapseXPath.java:297) at org.apache.synapse.mediators.builtin.PropertyMediator.getResultValue(PropertyMediator.java:299) at org.apache.synapse.mediators.builtin.PropertyMediator.mediate(PropertyMediator.java:95) at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71) at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:114) at org.apache.synapse.core.axis2.ProxyServiceMessageReceiver.receive(ProxyServiceMessageReceiver.java:154) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180) at org.apache.axis2.transport.tcp.TCPWorker.run(TCPWorker.java:68) at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) Caused by: com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs. at [row,col {unknown-source}]: [2,5] at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:606) at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:479) at com.ctc.wstx.sr.BasicStreamReader.readPIPrimary(BasicStreamReader.java:3903) at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2037) at com.ctc.wstx.sr.BasicStreamReader.closeContentTree(BasicStreamReader.java:2886) at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2629) at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1062) at org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.next(XMLStreamReaderWrapper.java:225) at org.apache.axiom.util.stax.dialect.DisallowDoctypeDeclStreamReaderWrapper.next(DisallowDoctypeDeclStreamReaderWrapper.java:34) at org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.next(XMLStreamReaderWrapper.java:225) at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:681) at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214) ... 25 more
I used appending '\n', "\r\n" to my message but non of them make that work.
Also i tried that used the method that explined in this link.How to Send SOAP Messages Using TCP Transport and it worked fine.
But i could not use that kind of api in my project. How can i get rid of this problem.
回答1:
I was able to send TCP messages to ESB using a sample client but you have to make sure you send an XML element.
First make sure you have enabled transport reciver for tcp in axis2.xml (repository/conf/axis2/axis2.xml)
<transportReceiver name="tcp" class="org.apache.axis2.transport.tcp.TCPTransportListener" >
========================My Proxy Service which recives TCP messages ===========
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="TCPProxy"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<parameter name="transport.tcp.port">6789</parameter>
<parameter name="transport.tcp.contentType">application/xml</parameter>
<description/>
</proxy>
==Client that send messages to TCP server ====
import java.io.*;
import java.net.*;
class TCPClient {
String host = "localhost";
int port = 6789;
Socket socket = null;
public static void main(String args[]) throws Exception{
String name ="Amani";
TCPClient client = new TCPClient();
String message = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
"<soapenv:Header/>\n" +
"<soapenv:Body>\n" +
" <p:greet xmlns:p=\"http://greet.service.amani.org\">\n" +
" <in>" + name + "</in>\n" +
" </p:greet>\n" +
"</soapenv:Body>\n" +
"</soapenv:Envelope>";
client.SendToServer("<test></test>");
client.close();
}
TCPClient(String _host, int _port) throws Exception{
host = _host;
port = _port;
socket = new Socket(host, port);
}
TCPClient() throws Exception{
socket = new Socket(host, port);
}
void SendToServer(String msg) throws Exception{
//create output stream attached to socket
PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
//send msg to server
outToServer.print(msg + '\n');
outToServer.flush();
}
String RecieveFromServer() throws Exception{
//create input stream attached to socket
BufferedReader inFromServer = new BufferedReader(new InputStreamReader (socket.getInputStream()));
//read line from server
String res = inFromServer.readLine(); // if connection closes on server end, this throws java.net.SocketException
return res;
}
void close() throws IOException{
socket.close();
}
}
来源:https://stackoverflow.com/questions/17362982/tcp-transport-in-proxy-wso2-esb