3

我刚刚尝试了示例资源,该示例资源解释了异步响应由未启动资源方法的不同线程恢复。

[使用环境:tomcat 7, jersey 2.3] 在 get 请求后立即发出 post 请求时,如示例中所示。我有以下问题:

1)我在服务器运行时将响应写为“关闭输出流以进行响应时出错。(<-有时发布请求完成时没有任何异常)

at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
at org.apache.coyote.http11.InternalOutputBuffer.endRequest(InternalOutputBuffer.java:159)
at org.apache.coyote.http11.AbstractHttp11Processor.action(AbstractHttp11Processor.java:749)
at org.apache.coyote.Response.action(Response.java:173)
at org.apache.coyote.Response.finish(Response.java:279)
at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:293)
at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:108)
at org.glassfish.jersey.message.internal.CommittingOutputStream.close(CommittingOutputStream.java:275)
at org.glassfish.jersey.message.internal.OutboundMessageContext.close(OutboundMessageContext.java:835)
at org.glassfish.jersey.server.ContainerResponse.close(ContainerResponse.java:411)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:625)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:357)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:347)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder$3.run(ServerRuntime.java:759)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:295)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:797)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:754)

2) 我的客户端请求(方法类型 - GET)没有得到 post 方法调用写入的异步响应。我没有任何例外。

我试过的代码:

import java.text.DateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;

import org.glassfish.jersey.server.ManagedAsync;

@Path("/messages/next")
public class MessagingResource {
    private static final BlockingQueue<AsyncResponse> suspended =
        new ArrayBlockingQueue<AsyncResponse>(5, Boolean.TRUE);

@GET
@ManagedAsync
public void readMessage(@Suspended AsyncResponse ar) throws InterruptedException {
    echo(" readMessage: before put");
    synchronized (MessagingResource.class) {
        echo(" readMessage: before put, count :"+suspended.size());
        suspended.put(ar);
        echo(" readMessage: after put, count :"+suspended.size());
    }
    echo(" readMessage: after put");
}

@POST
@ManagedAsync
public String postMessage() throws InterruptedException {

    AsyncResponse ar = null; 
    echo(" postMessage: before take, resume");
    synchronized (MessagingResource.class) {
        echo(" postMessage: before put, count :"+suspended.size());
        ar = suspended.take();
        echo(" postMessage: after put, count :"+suspended.size());
    }
    String message = " TEST TEST";
    ar.resume(message ); // resumes the processing of one GET request
    echo(" postMessage: after take , resume");
    return "Message sent";
}

private void echo(String string) {
    String dtStr = DateFormat.getTimeInstance(DateFormat.LONG).format(new Date());
    System.out.println( dtStr + " : " +string );
}
4

0 回答 0