When we are write data using socket channel write method. It's taking too much time for small byte of data. For example if many client is connected to my application then it take 43950 ms to write 16249 byte data. It try 442 times to write above data.
public static void writeBytesFixedLength(SocketChannel sChannel, String msg){
ByteBuffer writeBuffer = ByteBuffer.allocate(msg.length()*2);
writeBuffer.put(msg.getBytes());
writeBuffer.flip();
long nbytes = 0;
long toWrite = writeBuffer.remaining();
int sleepCount = 0;
int loopCount = 0;
int sleepTime = 50;
int sumSleepCount = 0;
int sumSleepTime = 0;
try
{
int count = 0;
while (nbytes != toWrite) {
count= sChannel.write(writeBuffer);
nbytes +=count;
loopCount += 1;
try {
if(count == 0){
OSUsage();
MainImapServer.printThreadStatus();
sleepCount += 1;
sumSleepCount += 1;
if(sleepCount == 4){
sleepTime = 50;
sleepCount =1;
}
sumSleepTime += sleepTime*sleepCount;
Thread.sleep(sleepTime*sleepCount);
}
}
catch (InterruptedException e) {}
}
}
catch (ClosedChannelException cce) {}
catch (Exception e) {}
if(sumSleepCount >3)
logger.info("Send buffer is full: Total Byte: "+ toWrite+ " sleepCount: "+sumSleepCount +" sleepTime: "+sumSleepTime+ " loopCount: "+ loopCount);
}
please provide some suggestion to over come this problem
Thanks in advance