类型不匹配:无法从String转换为ListenableFuture <string>

时间:2016-11-28 06:49:04

标签: spring

我正在尝试实现非阻塞调用。在春天4,但不幸的是它抛出了以下错误。

类型不匹配:无法从String转换为ListenableFuture

并且同样的错误也无法从Map转换为ListenableFuture&gt;。

我的方法调用堆栈如下所示。

ListenableFuture<Map<String,String>> unusedQuota = doLogin(userIdentity,request,"0");

doLogin登录只需返回Map

是否需要转换器?

需要进行哪些更改?

感谢。

public class MyController {



final DeferredResult<Map<String,String>> deferredResult = new DeferredResult<Map<String,String>>(5000l);

private final Logger log = LoggerFactory.getLogger(MyController.class);

 @Inject
 RestTemplate restTemplate;


    @RequestMapping(value = "/loginservice", method = RequestMethod.GET)
    @Timed
    public DeferredResult<Map<String,String>> loginRequestService(@RequestParam String userIdentity,HttpServletRequest request) throws Exception {
        deferredResult.onTimeout(new Runnable() {

            @Override
            public void run() { // Retry on timeout
                deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).body("Request timeout occurred."));
            }
        });

        @SuppressWarnings("unchecked")
        ListenableFuture<Map<String,String>> unusedQuota =  doLogin(userIdentity,request);
        unusedQuota.addCallback(new ListenableFutureCallback<Map<String,String>>() {

            @SuppressWarnings("unchecked")
            @Override
            public void onSuccess(Map<String, String> result) {
                // TODO Auto-generated method stub
                deferredResult.setResult((Map<String, String>) ResponseEntity.ok(result));
            }

            @Override
            public void onFailure(Throwable t) {
                // TODO Auto-generated method stub
                deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(t));
            }

        });             
        return deferredResult;

    }


    private Map<String,String> doLogin(String userIdentity,HttpServletRequest request) throws Exception{

        Map<String,String> unusedQuota=new HashMap<String,String>();

        unusedQuota.put("quota", "100");
        return unusedQuota;
        }


    }

}

1 个答案:

答案 0 :(得分:1)

当存在导致问题的异常时,您没有传递Map对象,因此需要如下所示更改控制器方法,同时在Controller方法中移动deferredResult对象作为您应该为不同的用户请求共享deferredResult的相同实例。

public class MyController {

     @Autowired
     private TaskExecutor asyncTaskExecutor; 

    @RequestMapping(value = "/loginservice", method = RequestMethod.GET)
    @Timed
    public DeferredResult<Map<String,String>> loginRequestService(@RequestParam String userIdentity,HttpServletRequest request) throws Exception {

                final DeferredResult<Map<String,String>> deferredResult = new DeferredResult<Map<String,String>>(5000l);

                deferredResult.onTimeout(new Runnable() {

                    @Override
                    public void run() { // Retry on timeout
                        Map<String, String> map = new HashMap<>();
                        //Populate map object with error details with Request timeout occurred.

                        deferredResult.setErrorResult(new ResponseEntity
                                   <Map<String, String>>(map, null,  
                                          HttpStatus.REQUEST_TIMEOUT));

                    }
                });

            ListenableFuture<String> task = asyncTaskExecutor.submitListenable(new Callable<String>(){
        @Override
        public Map<String,String> call() throws Exception {
            return doLogin(userIdentity,request);
        }
        });

                unusedQuota.addCallback(new ListenableFutureCallback<Map<String,String>>() {

                    @SuppressWarnings("unchecked")
                    @Override
                    public void onSuccess(Map<String, String> result) {
                        // TODO Auto-generated method stub
                        deferredResult.setResult((Map<String, String>) ResponseEntity.ok(result));
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        Map<String, String> map = new HashMap<>();
                        //Populate map object with error details

                       deferredResult.setErrorResult(new ResponseEntity<Map<String, String>>(
                       map, null, HttpStatus.INTERNAL_SERVER_ERROR));
                    }

                });       
                return deferredResult;
            }
     }

此外,您需要确保按照示例here中的说明配置ThreadPoolTaskExecutor