我在 Windows 7 上使用带有 Java 1.7.0 的 Jetty 9.0.4。
我如何证明这些超时有效?理想情况下,我正在寻找一个集成单元测试,如果它们不起作用,它将失败。
org.mortbay.jetty.nio.SelectChannelConnector connector = new SelectChannelConnector();
connector.setMaxIdleTime(10_000);
connector.setSoLingerTime(10_000);
connector.setLowResourceMaxIdleTime(10_000);
我尝试通过以下方式连接到 http 端口,但它没有失败,只是每分钟读取一个字节。我预计它会失败,因为最大空闲时间设置为 10 秒。
Socket socket = new Socket("localhost", port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".getBytes());
outputStream.flush();
InputStream content = socket.getInputStream();
char aChar;
int bytesToReceive = 1000;
while((aChar = (char) content.read()) != -1 && bytesToReceive-- > 0) {
System.out.println("Read '" + aChar + "' and going to sleep for a minute.");
Thread.sleep(60_000);
}