2014-01-14 41 views
1

我需要我的as400和java连接的帮助。在java套接字中收到的延迟响应

我在SocketTimeoutExceptions期间收到延迟响应时遇到问题。因此,当我发送第一个请求和响应超时时,发送第二个请求后,会收到第一个请求的响应,这会产生差异。

是否有一种方法,在超时异常之后,响应不应该缓存或不应该在下一个请求时收到?

下面是我的代码片断连接到AS400服务器:

public synchronized static byte[] sendRequestAndGetResponse(byte[] requestBytes) { 
    try { 
     if(checkHostConnection()){ 
      LOG.debug("Sending request bytes to Host: {}", Hexifier.toHex(requestBytes)); 
      IOUtils.write(requestBytes, dos); 
      long startTime = System.currentTimeMillis(); 
      LOG.info("Request sent to host."); 

      LOG.info("Waiting on host response"); 
      byte[] responseLengthBuffer = new byte[4]; 
      IOUtils.readFully(dis, responseLengthBuffer); 

      ByteBuffer bb = ByteBuffer.wrap(responseLengthBuffer); 
      int msgLength = bb.getInt(); 

      byte[] responseRemBytes = new byte[msgLength]; 
      IOUtils.readFully(dis, responseRemBytes); 
      long endTime = System.currentTimeMillis(); 

      byte[] fullResponseBytes = new byte[responseLengthBuffer.length + responseRemBytes.length]; 

      System.arraycopy(responseLengthBuffer, 0, fullResponseBytes, 0, responseLengthBuffer.length); 
      System.arraycopy(responseRemBytes, 0, fullResponseBytes, responseLengthBuffer.length, responseRemBytes.length); 
      LOG.debug("Response from server is received. Time elapsed at {} millis.", (endTime - startTime)); 
      LOG.debug("Bytes received: {}", Hexifier.toHex(fullResponseBytes)); 

      return fullResponseBytes; 

     } else { 
      reconnectToHost(); 
     } 

    } catch (SocketTimeoutException ste){ 
     LOG.error("Reading response from socket timeout: {}", ste.getClass().getName()); 

     try { 
      LOG.warn("Waiting for Host reply from its socket timeout."); 
      socket.shutdownInput(); 
      socket.shutdownOutput(); 

      int hostResponsefromTimeout = dis.read(); 
      if (hostResponsefromTimeout == -1){ 
       LOG.debug("Host has responded with -1"); 

      } else { 
       LOG.debug("Host responded with: {}", hostResponsefromTimeout); 
      } 

     } catch (Exception e){ 
      LOG.error("Error encountered while trying to validate if Host responded with last timeout."); 
      LOG.error(e.getMessage(), e); 

     } 


     reconnectToHost(); 

    } catch (EOFException eofe) { 
     LOG.debug("An error occurred while reading stream from Host socket connection: {}", eofe.getClass().getName()); 
     LOG.error(eofe.getMessage(), eofe); 

     if (eofe.getMessage() != null && eofe.getMessage().contains("actual: 0")){ 
      LOG.warn("No bytes were found at stream. Host did not respond."); 
     } 

     reconnectToHost(); 

    } catch (SocketException e) { 
     LOG.error("An error occurred while attempting to connect to Host: {}", e.getClass().getName()); 
     LOG.error(e.getMessage(), e); 

     if (e.getMessage().contains("Broken pipe")){ 
      LOG.debug("Connection to Host was lost."); 
     } 

     reconnectToHost(); 

    } catch (Exception e) { 
     LOG.error(e.getMessage(), e); 
     LOG.debug("An error occurred while attempting to connect to Host: {}", e.getClass().getName()); 
     reconnectToHost(); 
    } 

    return null; 
} 
+5

你有一些代码可以分享吗? – AxiomaticNexus

+0

您好,请参阅我的代码 – alvin

回答

0

如果你得到你,否则计划重新用于另一个请求插座上SocketTimeoutException,关闭它,请勿将重新用于其他请求。摆脱所有关闭并在catch处理程序中读取代码并关闭套接字。您不可能在同一个套接字上获得后续请求的'错误响应'。

如果您得到很多超时,则您的超时时间太短。通常,它应该是有关请求的预期服务时间的两倍。

+0

您好,在SocketTimeoutException关闭套接字并为另一个请求创建一个新套接字后,方法reconnectToHost()。这是我最初的实施,但延迟响应仍然经历。我添加了关机事件和读取代码,以确保我优雅地丢弃了与主机的连接:[link](http://stackoverflow.com/questions/2028620/java-sockets-and-dropped-connections) – alvin