Explorar el Código

新增同步异步效果

chenweida hace 9 años
padre
commit
b3e093ff57

+ 15 - 1
Hos-Resource-Rest/src/main/java/com/yihu/hos/gateway/control/GatewayControl.java

@ -1,5 +1,6 @@
package com.yihu.hos.gateway.control;
import com.yihu.hos.gateway.thread.ResponseThread;
import com.yihu.hos.gateway.exception.EHRException;
import com.yihu.hos.gateway.exception.EHRExceptionConstant;
import com.yihu.hos.gateway.model.rest.RestRequsetResult;
@ -7,6 +8,7 @@ import com.yihu.hos.gateway.model.rest.RestResponseResult;
import com.yihu.hos.gateway.service.intf.IGatewayService;
import com.yihu.hos.resource.util.httpclient.HttpClientUtil;
import com.yihu.hos.resource.viewModel.ResourceRestDetailModel;
import com.yihu.hos.resource.viewModel.SQLResponResult;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -43,6 +45,7 @@ public class GatewayControl {
     */
    @RequestMapping("/transfer")
    public Object transfer(HttpServletRequest request) {
        boolean isSyn = false;//是否异步
        String resultData = null;
        RestRequsetResult restRequsetResult = new RestRequsetResult();
        RestResponseResult restResponseResult = new RestResponseResult();
@ -51,6 +54,7 @@ public class GatewayControl {
            //---start  扩展type和data两个参数 作用于API和param一样
            if (StringUtils.isEmpty(restRequsetResult.getApi())) {
                restRequsetResult.setApi(restRequsetResult.getType());
                isSyn = true;
            }
            if (StringUtils.isEmpty(restRequsetResult.getParam())) {
                restRequsetResult.setParam(restRequsetResult.getData());
@ -61,7 +65,17 @@ public class GatewayControl {
            //根据API得到资源对象
            ResourceRestDetailModel resourceRestDetailModel = gatewayService.getResourceRestDetailModelByCode(restRequsetResult.getApi());
            //根据资源对象的信息还有访问的参数通过httpclient得到数据
            resultData = getResultData(resourceRestDetailModel, restRequsetResult.getParam());
            if (isSyn) {
                new Thread(new ResponseThread(httpClientUtil,
                        resourceRestDetailModel.getUrl() + resourceRestDetailModel.getNamespace().replace(".", "/"),
                        resourceRestDetailModel.getUsername(),
                        resourceRestDetailModel.getPassword(),
                        JSONObject.fromObject(resourceRestDetailModel.getParam()),
                        resourceRestDetailModel.getType())).start();
                return new SQLResponResult();
            } else {
                resultData = getResultData(resourceRestDetailModel, restRequsetResult.getParam());
            }
            return resultData;
        } catch (Exception e) {
            e.printStackTrace();

+ 47 - 0
Hos-Resource-Rest/src/main/java/com/yihu/hos/gateway/thread/ResponseThread.java

@ -0,0 +1,47 @@
package com.yihu.hos.gateway.thread;
import com.yihu.hos.gateway.exception.EHRException;
import com.yihu.hos.gateway.exception.EHRExceptionConstant;
import com.yihu.hos.resource.util.httpclient.HttpClientUtil;
import com.yihu.hos.resource.viewModel.ResourceRestDetailModel;
import net.sf.json.JSONObject;
/**
 * Created by Administrator on 2016/4/25.
 */
public class ResponseThread implements Runnable {
    private String url = "";
    private String username = "";
    private String password = "";
    private String type = "";
    private JSONObject param;
    private HttpClientUtil httpClientUtil = null;
    public ResponseThread(HttpClientUtil httpClientUtil, String url, String username, String password, JSONObject param, String type) {
        this.httpClientUtil = httpClientUtil;
        this.url = url;
        this.username = username;
        this.password = password;
        this.param = param;
        this.type = type;
    }
    @Override
    public void run() {
        String returnData = null;
        //拼凑出URL
        try {
            //根据配置好的类型判断是get还是post
            if (ResourceRestDetailModel.ResourceRest_TYPE_GET.equals(type)) {
                returnData = httpClientUtil.doGet(url, JSONObject.fromObject(param), username, password);
            } else if (ResourceRestDetailModel.ResourceRest_TYPE_POST.equals(type)) {
                returnData = httpClientUtil.doPost(url, JSONObject.fromObject(param), username, password);
            } else {
                throw new EHRException(EHRExceptionConstant.EHREXCEPTION_BUSINESS_REOURCE_TYPE_EXCEPTION, EHRExceptionConstant.EHREXCEPTION_BUSINESS_REOURCE_TYPE_EXCEPTION_MESSAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}