如何在流程中提交或刷新其余响应

发布于 2024-12-13 20:13:22 字数 1610 浏览 0 评论 0原文

我对java和jersey都是新手。现在,我想使用 jersey 来实现 REST 服务,并在发送响应后进行额外处理(具体来说,休眠固定的秒数,然后在同一 servlet 上下文中触发不同的 REST 请求,因此它与 REST 代理不同)。我已经用谷歌搜索了一段时间,但似乎所有人都想当然地认为在方法结束时隐式刷新响应。以下是我正在努力处理的启用了 JAXB 的当前代码。

@Path("/chat")
public class LoadSimulator {

    @Context private UriInfo uriInfo;

    @Path("/outbound/{senderAddress}/requests")
    @POST
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response createOutboundSMSMessage(OutboundSMSMessageRequest inSmsReq, @PathParam("senderAddress") String senderAddress) throws JAXBException {
        String requestId = UUID.randomUUID().toString();
        URI uri = uriInfo.getAbsolutePathBuilder().path(requestId).build();

        ObjectFactory factory = new ObjectFactory();
        ResourceReference resourceReference = new ResourceReference();
        resourceReference.setResourceURL(uri.toString());
        JAXBElement<ResourceReference> inSmsResponse = factory.createResourceReference(resourceReference);
                return Response.created(uri).entity(inSmsResponse).build();
             //// want to flush or commit the response explicitly like:
              //        out.flush();
            //        out.close();
            //// Then sleep for a few second and fire a new REST request
            //        sleep(5);
            //          ....   
            // ClientConfig config = new DefaultClientConfig();
            // String response = r.path("translate").queryParams(params).get(String.class);



    }
}

I'm new to both java and jersey. Now I want to use the jersey to realize a REST services with extra processing after sending the response (specifically, sleep a fix amount of seconds and then fire a different REST request in the same servlet context, so it's unlike a REST proxy). I had googled for a while but all seems take it for granted that implicitly flushing the response at the end of method. Here are the current codes with JAXB enabled I'm struggling to work on.

@Path("/chat")
public class LoadSimulator {

    @Context private UriInfo uriInfo;

    @Path("/outbound/{senderAddress}/requests")
    @POST
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response createOutboundSMSMessage(OutboundSMSMessageRequest inSmsReq, @PathParam("senderAddress") String senderAddress) throws JAXBException {
        String requestId = UUID.randomUUID().toString();
        URI uri = uriInfo.getAbsolutePathBuilder().path(requestId).build();

        ObjectFactory factory = new ObjectFactory();
        ResourceReference resourceReference = new ResourceReference();
        resourceReference.setResourceURL(uri.toString());
        JAXBElement<ResourceReference> inSmsResponse = factory.createResourceReference(resourceReference);
                return Response.created(uri).entity(inSmsResponse).build();
             //// want to flush or commit the response explicitly like:
              //        out.flush();
            //        out.close();
            //// Then sleep for a few second and fire a new REST request
            //        sleep(5);
            //          ....   
            // ClientConfig config = new DefaultClientConfig();
            // String response = r.path("translate").queryParams(params).get(String.class);



    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-12-20 20:13:22

如果你能做你想做的事,你就会耗尽服务器上的资源,因为每个请求都需要 X 秒,并且在盒子哭叔叔之前你有有限数量的可用线程。

不评论为什么你想要这样做;如果您为 LoadSimulator 使用了 @Singleton 注释,您可以在 @PostConstruct public void init()< 中设置一个监听(并发)队列的线程。 /code> - 当您的 servlet 启动时被调用。

@Singleton
@Path("/chat")
public class LoadSimulator {
    private Thread restCaller;
    private ConcurrentLinkedQueue<MyInfo> queue = new ConcurrentLinkedQueue<MyInfo>();

    ...
    @PostConstruct public void init()
    {
        restCaller = new Thread(new MyRunnable(queue));
        restCaller.start();
    }
    ...

然后,在 REST 调用中,您可以将进行第二个 REST 调用所需的任何信息放入该队列中,并让上述线程将其拉出并进行查询。

If you could do what you're trying to do, you would exhaust the resources on your server because every request would take X seconds and you have a finite amount of threads available before the box cries uncle.

Without commenting on why you'd want to do this; If you used the @Singleton annotation for your LoadSimulator you could set up a thread that listens on a (concurrent) queue in @PostConstruct public void init() - that gets called when your servlet starts up.

@Singleton
@Path("/chat")
public class LoadSimulator {
    private Thread restCaller;
    private ConcurrentLinkedQueue<MyInfo> queue = new ConcurrentLinkedQueue<MyInfo>();

    ...
    @PostConstruct public void init()
    {
        restCaller = new Thread(new MyRunnable(queue));
        restCaller.start();
    }
    ...

Then in your REST call, you'd put whatever information is needed to make the second REST call on that queue, and have the aforementioned thread pulling it off and making queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文