代理wso2 esb中的TCP传输

时间:2013-06-28 10:48:45

标签: sockets tcp wso2 wso2esb

我想通过TCP传输向代理服务发送请求,当使用此代码发送我的soap xml

Socket clientSocket = new Socket("host", port);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

outToServer.writeBytes("soap xml instance");

clientSocket.close();

它工作正常,我的业务仍在继续。

但是当我发送两个xml而没有像这样关闭套接字时:

Socket clientSocket = new Socket("host", port);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

outToServer.writeBytes("soap xml instance");

outToServer.writeBytes("another soap instance");

clientSocket.close();

它总是抛出这个异常:

  

TID:[0] [ESB] [2013-06-28 13:36:10,838]错误   {org.apache.axis2.transport.tcp.TCPWorker} - 处理时出错   TCP请求通过Axis2引擎   {} org.apache.axis2.transport.tcp.TCPWorker   org.apache.axiom.om.OMException:   com.ctc.wstx.exc.WstxParsingException:非法处理指令   target(“xml”);规范保留xml(不区分大小写)。在   [row,col {unknown-source}]:[2,5]           在org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296)           在org.apache.axiom.om.impl.llom.OMDocumentImpl.buildNext(OMDocumentImpl.java:135)           at org.apache.axiom.om.impl.llom.OMNodeImpl.getNextOMSibling(OMNodeImpl.java:122)           在org.apache.axiom.om.impl.llom.OMElementImpl.getNextOMSibling(OMElementImpl.java:343)           在org.apache.axiom.om.impl.traverse.OMChildrenIterator.getNextNode(OMChildrenIterator.java:36)           在org.apache.axiom.om.impl.traverse.OMAbstractIterator.hasNext(OMAbstractIterator.java:58)           at org.jaxen.util.DescendantAxisIterator.hasNext(DescendantAxisIterator.java:101)           在org.jaxen.expr.DefaultStep.evaluate(DefaultStep.java:152)           在org.jaxen.expr.DefaultLocationPath.evaluate(DefaultLocationPath.java:140)           在org.jaxen.expr.DefaultAbsoluteLocationPath.evaluate(DefaultAbsoluteLocationPath.java:113)           在org.jaxen.expr.DefaultXPathExpr.asList(DefaultXPathExpr.java:102)           at org.jaxen.BaseXPath.selectNodesForContext(BaseXPath.java:674)           在org.jaxen.BaseXPath.selectNodes(BaseXPath.java:213)           在org.jaxen.BaseXPath.evaluate(BaseXPath.java:172)           at org.apache.synapse.util.xpath.SynapseXPath.stringValueOf(SynapseXPath.java:297)           在org.apache.synapse.mediators.builtin.PropertyMediator.getResultValue(PropertyMediator.java:299)           在org.apache.synapse.mediators.builtin.PropertyMediator.mediate(PropertyMediator.java:95)           在org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)           在org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:114)           at org.apache.synapse.core.axis2.ProxyServiceMessageReceiver.receive(ProxyServiceMessageReceiver.java:154)           在org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)           在org.apache.axis2.transport.tcp.TCPWorker.run(TCPWorker.java:68)           在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)引起:com.ctc.wstx.exc.WstxParsingException:非法处理指令   target(“xml”);规范保留xml(不区分大小写)。在   [row,col {unknown-source}]:[2,5]           在com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:606)           在com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:479)           在com.ctc.wstx.sr.BasicStreamReader.readPIPrimary(BasicStreamReader.java:3903)           在com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2037)           在com.ctc.wstx.sr.BasicStreamReader.closeContentTree(BasicStreamReader.java:2886)           在com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2629)           在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)           在org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:681)           在org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214)           ......还有25个

我使用了'\ n',“\ r \ n”添加到我的消息中,但是没有使用它们。

我也尝试使用此链接中显示的方法。How to Send SOAP Messages Using TCP Transport并且它运行正常。

但我不能在我的项目中使用那种api。我怎样才能摆脱这个问题。

1 个答案:

答案 0 :(得分:1)

我能够使用示例客户端向ESB发送TCP消息,但您必须确保发送XML元素。

首先确保在axis2.xml中启用了tcp的transport reciver(repository / conf / axis2 / axis2.xml)

<transportReceiver name="tcp" class="org.apache.axis2.transport.tcp.TCPTransportListener" >

========================我的代理服务收回TCP消息===========

<?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>

==向TCP服务器发送消息的客户端====

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