如何以java中的块形式返回web服务中的数据

问题描述:

你好我正在使用一个web服务,它在完成代码执行时返回一个输出。是否有可能web服务可能会返回状态块像自定义字符串:测试开始,测试进行中,测试完成等如何以java中的块形式返回web服务中的数据

我需要做什么来实现这一点。这里是我当前的代码,我期待一个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; 
    } 
} 
+0

您使用的是什么JAX-RS实现和版本? –

+0

@peeskillet:我正在使用Jersey-bundle使用JAX-WS Web Services。 –

+0

我不确定在Jersey 1.x中如何处理它,但Jersey 2.x有[chunking support](https://jersey.java.net/documentation/latest/async.html) –

您需要对服务器进行高频连续轮询以实现当前状态。

对于多一点信息,有一个看看:

Continuous polling of output using spring ,rest and angular js

这包括设计考虑使用的WebSockets等,但据我所知没有直接的解决方案。

+0

我不确定此示例适用于并发请求。 –