简体   繁体   English

如何在Java中以块形式从Web服务返回数据

[英]How to return data from a webservice in chunks in java

Hello I am using a webservice which returns a output upon completion of code execution. 您好,我正在使用一个Web服务,该服务在代码执行完成时返回输出。 Is it possible that webservice may return the status in chunks like custom strings: Test Started, Test In Progress, Test Completed etc. 网络服务是否可能以大块形式返回状态,例如自定义字符串:“测试已开始”,“测试正在进行中”,“测试完成”等。

What I need to do to achieve this. 我需要做些什么才能做到这一点。 Here is my current code where I am expecting a json string as input, supplied json is parsed and further processing is being performed. 这是我当前的代码,我期望将json字符串作为输入,解析提供的json并执行进一步的处理。

//Class
public class WebserviceClient 
{
    /** calling constructor to initialize logger */
    Utils c = new Utils();
    Logger logger = Logger.getLogger(WebserviceClient.class.getName());


    @Path("/test")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    //@Produces(MediaType.APPLICATION_JSON)
    public String processRequest(final String inputData)
    {
        String executionID = "NOT_FOUND" ;
        String result = "";

        try 
        {
            /** creating a pool of threads to submit a task to a callable thread */
            ExecutorService ex = Executors.newFixedThreadPool(5);

            Future<String> futureObject = ex.submit(new Callable<String>() {
                @Override
                public String call() throws Exception 
                {
                    logger.info("Parsing Received Request: "+inputData);
                    String rID = new JSONObject(inputData).getString("id");
                    logger.info("Received Id: "+rID + " From Request: "+inputData);

                    if(new RunTest().isTestCompleted(rID))
                    {
                        return rID;
                    }
                    else
                    {
                        return "777";
                    }
                }
            });

            result = futureObject.get();

            if(futureObject.get()!=null)
            {
                ex.shutdown();
            }
            else{
                logger.debug("call id: "+executionID +" result is not generated yet. ");
            }

            logger.info("call id && Result: "+result);
        }
        catch(Exception e) 
        {
            logger.error("call id: "+executionID, e);
        }
        return result;
    }
}

You need to do a continuous polling to the server at high frequency to achieve the current status. 您需要对服务器进行高频连续轮询以获取当前状态。

For little more information have a look at the : 有关更多信息,请参阅:

Continuous polling of output using spring ,rest and angular js 使用spring,rest和angular js连续轮询输出

This includes design consideration of using WebSockets etc, but there is no straight forward solution that I'm aware of. 这包括使用WebSockets等的设计注意事项,但是我所知道的没有简单的解决方案。

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

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