Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输出延迟数据传递_Java_Jersey_Jax Rs_Jersey 2.0_Server Sent Events - Fatal编程技术网

Java 输出延迟数据传递

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()) {

我用SSE打电话给我的客户。通过SSE发送ping请求,客户机通过POST响应该请求。不知何故,我的ping请求常常在客户机上延迟到达。延迟似乎与所使用的ping超时直接相关

客户端类:

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();
}
这种延误来自何处,如何避免