Thursday, April 10, 2014

TCP Transport in Proxy WSO2 ESB

Its simple you need do following steps

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" />
<transportSender name="tcp" class="org.apache.axis2.transport.tcp.TCPTransportSender"/>



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

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


No comments:

Post a Comment