Java spring MVC中异步数据访问的全局变量
我有一个控制器Java spring MVC中异步数据访问的全局变量,java,spring,spring-mvc,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
@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;
}
}
@控制器
公共类测试控制器{
公共未来全局_值=空;
@自动连线
专用异步服务;
@RequestMapping(value=“/asyncCall”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
@应答器
公共字符串getToken(HttpServletRequest请求){
全局_值=asyncService.execute();
返回“OK”;
}
@RequestMapping(value=“/getAsyncData”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
@应答器
公共ArrayList getData(){
试一试{
if(global_value!=null&&global_value.get().size()>0){
返回全局_值。get();
}
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
返回null;
}
}
当我长时间轮询getAsncData
端点时,如何维护仅针对请求的全局_值。全局_值对于请求范围必须是唯一的
如何实现这一点?如果您希望在请求范围中使用全局值,则不要将其作为实例变量。您的意思是将其定义为静态?