package com.keyware.shandan.component;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.client.RestTemplate;

/**
 * 异步请求的RestTemplate
 * @author Administrator
 */
@Component
public class AsyncRestTemplate extends RestTemplate {

    @Async
    public <T> ListenableFuture<T> getForObjectAsync(String url, Class<T> responseType, Object... uriVariables) {
        try {
            T result =super.getForObject(url, responseType, uriVariables);
            if(result == null){
                result = responseType.newInstance();
            }
            return AsyncResult.forValue(result);
        }catch (Exception e){
            return AsyncResult.forExecutionException(e);
        }
    }
}
