Spring MVC中异步数据访问的全局变量

时间:2015-10-13 09:25:03

标签: java spring spring-mvc

我有一个以下控制器

@Controller
public class TestController{

public Future<ArrayList<String>> global_value = null;

@Autowired
private AsyncService asyncService;

@RequestMapping(value = "/asyncCall", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String getToken(HttpServletRequest request) {

   global_value = asyncService.execute();

return "OK";

}

    @RequestMapping(value = "/getAsyncData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ArrayList<String> getData() {
    try {
            if (global_value != null && global_value.get().size() > 0) {

                return global_value.get();

            }
        } catch (InterruptedException e) {

            e.printStackTrace();
        } catch (ExecutionException e) {

            e.printStackTrace();
        }
return null;
}

}

当我长时间轮询getAsncData端点时,如何维护global_value仅用于请求。 global_value对于请求范围必须是唯一的。

我怎样才能做到这一点?

0 个答案:

没有答案