我和sheduler.sheduleWithFixedDelay(????, 5,3, TimeUnit.MINUTES);一起工作。我应该在这里设置什么可运行的命令?发送"hello“响应的是webserver。我要给服务器应答增加延迟。
我已经修改了我的代码,谢谢matt。现在我有了可运行的命令,但没有任何延误。
void main(...){
SheduledExecutorService sheduler = Executors.newScheduledThreadPool(10);
HttpServer server = HttpServer.create(new InetSocketAddress(8001),5);
server.createContext("/test", (exchange -> {
String respText = "";
String requestText = CFG.getConntent(exchange);
if (requestText.contains("Hello")){
respText = "Hello";
}
else if (requestText.contains("Bye")){
respText = "Bye";
}
exchange.sendResponseHeaders(200, 0);
String textToSend = respText;
sheduler.sheduleWithFixedDelay(()->{
OutputStream output = exchange.getResponseBody();
try{
output.write(textToSend.getBytes(StandardCharsets.UTF_8));
output.flush();
}
catch (Exception e){}
exchange.close();
}, 0,10, TimeUnit.MINUTES);
server.setExecutor(sheduler);
server.start();
}发布于 2022-04-26 12:51:48
如果我对你的理解是正确的,那么你就需要在你的答复中加上延迟。
server.createContext("/test", (exchange -> {
String respText = "hello";
String requestText = CFG.getConntent(exchange);
exchange.sendResponseHeaders(200, 0);
sheduler.sheduleWithFixedDelay(()->{
OutputStream output = exchange.getResponseBody();
output.write(resp.Text.getBytes(UTF_8));
output.flush();
exchange.close();
}, 5,3, TimeUnit.MINUTES);
});这将导致响应主体在延迟后完成发送。我想它应该首先发送“200”ok信号来开始响应。
https://stackoverflow.com/questions/72014063
复制相似问题