简体   繁体   English

RESTful Web服务可以从子线程返回POST响应吗?

[英]Can a RESTful Web Service return a POST Response from Child Thread?

Currently this is the implementation I have for the incoming POST Request. 目前这是我对传入的POST请求的实现。 The processing is handled by a Child Thread, but the response must be returned back to the top level to send a response. 处理由子线程处理,但必须将响应返回到顶层以发送响应。

@Post("json")
public String POSTMessage(String myJsonString) throws Exception {
    JSONObject msg = (JSONObject) new JSONParser().parse(myJsonString);
    ClassA Client = new ClassA();
    Server.client_list.add(Client);

    return Client.processMSG(msg);
}

ClassA Processing builds a JSON Response and returns that to Top Level. ClassA Processing构建JSON响应并将其返回到顶级。 Is there any way the Child Thread could send the response back in a Restful architecture? Child Thread有没有办法在Restful架构中发回响应? In other words, does the POSTmessage function need to return the value, or is there some way a Child Thread can respond as the POSTMessage can go back to listening on a port. 换句话说,POSTmessage函数是否需要返回值,或者Child Thread是否可以通过某种方式响应,因为POSTMessage可以返回监听端口。

You can use asynchronous response , like: 您可以使用异步响应 ,例如:

@Post("json")
public void POSTMessage(String myJsonString, @Suspended final AsyncResponse asyncResponse) throws Exception {
    new Thread(new Runnable() {
        @Override
        public void run() {
           JSONObject msg = (JSONObject) new JSONParser().parse(myJsonString);
           ClassA client = new ClassA();                
           asyncResponse.resume(client.processMSG(msg));
        }
    }).start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM