我们正在开发一个Java(1.6)服务器应用程序,它是一个事务服务器,通过TCP套接字监听连接.每个新连接都会创建一个新线程,该线程将保持活动状态,直到连接关闭 每个客户端都会将事务发送到将要处理的服务器,然后将响应发送回客户端.
这很好用.当我们想要通过同一个套接字发送许多异步事务(或消息)时,就会出现问题.我写了一个小应用程序,在每次事务之间发送1000个事务,间隔为10毫秒.该应用程序是异步的,因此发送消息并且响应位于中间.
这是处理incomming消息并将它们发送到另一个要处理的组件的部分的代码(该组件有一个线程池):
public void run() {
...
...
socketBuf = new BufferedInputStream(input);
baos = new ByteArrayOutputStream();
while ((bytes_read = socketBuf.read(buffer)) != -1) {
if (bytes_read < 0) {
log.error("Tried to read from socket, read() returned < 0, Closing socket.");
return;
}
baos.write(buffer, 0, bytes_read);
break;
}
if (bytes_read >= 0) {
baos.flush();
byte data[] = baos.toByteArray();
if (data.length > 0) {
GWTranData tData = posMessage.decode(data, false);
if (tData.getMessageType() > 0) {
// Send to the Pre-Online Manager to …Run Code Online (Sandbox Code Playgroud)