Java 输出延迟数据传递
我用SSE打电话给我的客户。通过SSE发送ping请求,客户机通过POST响应该请求。不知何故,我的ping请求常常在客户机上延迟到达。延迟似乎与所使用的ping超时直接相关 客户端类:Java 输出延迟数据传递,java,jersey,jax-rs,jersey-2.0,server-sent-events,Java,Jersey,Jax Rs,Jersey 2.0,Server Sent Events,我用SSE打电话给我的客户。通过SSE发送ping请求,客户机通过POST响应该请求。不知何故,我的ping请求常常在客户机上延迟到达。延迟似乎与所使用的ping超时直接相关 客户端类: public synchronized boolean ping(int timeout){ final OutboundEvent event = EventFactory.newPingEvent(); if (eventSink.isClosed()) {
public synchronized boolean ping(int timeout){
final OutboundEvent event = EventFactory.newPingEvent();
if (eventSink.isClosed()) {
return false;
} else {
try {
responseReceived.set(false);
requestTimestamp.set(System.currentTimeMillis());
eventSink.write(event);
waitForResponseOrTimeout(timeout);
boolean clientAnswered = responseReceived.get();
return result;
} catch (IOException e) {
return false;
}
}
}
public void onPingResponse() {
responseReceived.set(true);
responseTimestamp.set(System.currentTimeMillis());
synchronized (this) {
notifyAll();
}
}
private void waitForResponseOrTimeout(int timeout) {
boolean timeoutExpired;
do {
try {
synchronized (this) {
wait(timeout);
}
} catch (InterruptedException ex) {
LOGGER.error("Wait was interrupted", ex);
}
timeoutExpired = getElapsedTime() > timeout;
} while (!(timeoutExpired || responseReceived.get()));
}
REST-API类:
@POST
@Path("{ClientId}")
public void pingResponse(@PathParam("ClientId") String id){
Client client=getClientById(id);
client.onPingResponse();
}
这种延误来自何处,如何避免