泽西岛的线程2

时间:2015-12-26 02:33:23

标签: java jersey jersey-2.0

在Jersey 2.22中,使用以下代码:

@Path("/header")
public class TestResource {

    @GET
    public Response get(){
    ...
    }

    @Path("sub")
    public Sub sub(){
        return new Sub();
    }
}

public class Sub {

    @GET
    public Response get(){
    ...
    }

    @Path("sub")
    public SubSub sub(){
        return new SubSub();
    }
}

public class SubSub {

    @GET
    public Response get(){
    ...
    }

    @Path("sub")
    public SubSubSub sub(){
        return new SubSubSub();
    }
}

public class SubSubSub {

    @GET
    public Response get(){
    ...
    }
}

对于/header/sub/sub/sub,是否保证在从TestResource.sub()一直遍历到SubSubSubSubSubSubSub ...的get()方法时会使用相同的线程?< / p>

1 个答案:

答案 0 :(得分:0)

如果你担心线程,你应该使用AsyncResource来处理线程池和超时等事情。

@POST
@Path("bid/{exchangeId}/{key}")
public void handleBid(@PathParam("exchangeId") final String exchangeId, @PathParam("key") final String key,
            final OrtbBidRequest request, @Suspended final AsyncResponse asyncResponse) throws FilterException, IOException {

    // Timeout for clarity.
    asyncResponse.setTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    asyncResponse.setTimeoutHandler(timeoutHandler);

    // Sending no data as an example
    asyncResponse.resume(Response.noContent().build());
}

此示例允许我将消息发送到消息队列以进行处理。异步响应将允许您根据需要处理异步线程。除非你创建一个新的执行线程,否则它将是执行代码的同一个线程。如果它是正交的。