rdelossantos
asked on
How could i create a socket connection pooling for handling a custom ascii text protocol?
I have a webservices multithreading service made in java with apache mina. It receives multiples connection and i need after that parse the webservice data and create a custom ascii protocol request to send thru tcp ip socket. What i would like to have is some way to pool the connections or have only 1 connection and pool the inputstream and outputstream for parsing the strings that contains the response with multithreading in mind.
Do you know of any documentation or library that does something like this from where i could get the ideas for this?
Do you know of any documentation or library that does something like this from where i could get the ideas for this?
ASKER
I don't have a specific approach. I'm trying to hear from programmers ways to handle this objective.
The idea I have is the next:
public class Request {
public String Id;
public String Body;
}
public class ConnectionManager {
ArrayBlockingQueue outputQueue = new ArrayBlockingQueue(50);
Map inputMap = new HashMap(50);
Socket clientSocket;
DataInputStream clientIS;
DataOutputStream clientOS;
public ConnectionManager (String address, int port) {
clientSocket = new Socket(address, port);
clientIS = new DataInputStream(clientSock et.getInpu tStream()) ;
clientOS = new DataOutputStream(clientSoc ket.getOut putStream( ));
}
public String sendMessage(Request request)
{
outputQueue.put(request);
inputMap.put(request.Id, "");
return "";
}
}
Were Im stuck is in the multithreading parts that needs to insert into the outputstream from the ArrayBlockingQueue and the thread that need to put the response coming from the inputstream to the hashMap with the corresponding id. This way I only need to create a connection manager object and then call ConnectionManager.sendMess age(Reques t).
I'm open to others ideas to do the same thing.
The idea I have is the next:
public class Request {
public String Id;
public String Body;
}
public class ConnectionManager {
ArrayBlockingQueue outputQueue = new ArrayBlockingQueue(50);
Map inputMap = new HashMap(50);
Socket clientSocket;
DataInputStream clientIS;
DataOutputStream clientOS;
public ConnectionManager (String address, int port) {
clientSocket = new Socket(address, port);
clientIS = new DataInputStream(clientSock
clientOS = new DataOutputStream(clientSoc
}
public String sendMessage(Request request)
{
outputQueue.put(request);
inputMap.put(request.Id, "");
return "";
}
}
Were Im stuck is in the multithreading parts that needs to insert into the outputstream from the ArrayBlockingQueue and the thread that need to put the response coming from the inputstream to the hashMap with the corresponding id. This way I only need to create a connection manager object and then call ConnectionManager.sendMess
I'm open to others ideas to do the same thing.
ConnectionManager has to contain an array of sockets DataInputStream and DataOutputStream.
I don't know very well java sintax but I'm quite sure that will be very easy for you to understand and adapt.
I don't know very well java sintax but I'm quite sure that will be very easy for you to understand and adapt.
Socket[] clientSocket = new Socket[50]();
DataInputStream[] clientIS...;
DataOutputStream[] clientOS..;
Boolean[] freeConnections = new Boolean[50]();
// initialize all connections
public ConnectionManager (String address, int port)
{
for(int i=0; i<50; i++)
{
clientSocket[i] = new Socket(address, port);
clientIS[i] = new DataInputStream(clientSocket.getInputStream());
clientOS[i] = new DataOutputStream(clientSocket.getOutputStream());
}
}
public String sendMessage(Request request)
{
outputQueue.put(request);
int i = AquireConnection();
clientOS[i].write(request.body);
string body = clientIS[i].read();
inputMap.put(request.Id, body);
return "";
}
// retrieve the index of the first free connection
int AquireConnection()
{
synchronized(this)
{
for(int i=0;i<50; i++)
{
if (freeConnections[i] == true)
{
// mark connection as used
freeConnections[i] = false;
return i;
}
}
}
// no connection available
return -1;
}
ReleaseConnection(int i)
{
synchronized(this)
{
freeConnections[i] = true;
}
}
ASKER
Is this solutions multithread safe? because multiple threads will try to comunicate and sendmessage. Another question comes about what happen if one connection is disconnected from the server, how could I recover this connection in the array?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A simple approach is to create thread safe list(or hash list) of buffers and every time when you receive something you will put also in that list. When that buffer is no longer available on your connection just remove it also from list or hash list.