2016-11-28 59 views
4

我試圖實現非阻塞式調用。在春季4,但不幸的是它拋出了以下錯誤。類型不匹配:無法從字符串轉換爲ListenableFuture <String>

類型不匹配:不能從字符串轉換成ListenableFuture

,也同樣的錯誤不能轉換能夠從地圖ListenableFuture>。

我的方法調用堆棧如下。

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

doLogin登錄簡單地返回地圖

是有需要的任何轉換器?

需要做什麼修改?

感謝。

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; 
     } 


    } 

}

+0

這是在控制器內嗎?發佈我們的整個方法代碼 – developer

回答

1

您沒有通過Map對象時存在,這是造成問題的一個例外,因此需要改變,如下圖所示,也將控制器方法中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; 
      } 
    } 

此外,你需要確保你所配置的ThreadPoolTaskExecutor作爲例子解釋here

+0

@SuppressWarnings(「unchecked」) ListenableFuture > unusedQuota = doLogin(userIdentity,request);在這個位置我得到同樣的錯誤。我做了你所建議的改變,但沒有運氣。 –

+0

另外,您的doLogin()應該在可調用狀態下運行以返回'ListenableFuture',更新了答案,並檢查了它 – developer

+0

感謝您的更新。但我通過使用AsyncRestTemplate完成。 –

相關問題