Browse Source

RPC调用diamante提交

chenweida 9 years ago
parent
commit
4af188d51c

+ 20 - 0
Hos-Resource-Rest/pom.xml

@ -165,6 +165,26 @@
            <artifactId>xom</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>org.apache.axis2.osgi</artifactId>
            <version>1.6.3</version>
        </dependency>
    </dependencies>

+ 4 - 4
Hos-Resource-Rest/src/main/java/com/yihu/hos/config/Config.java

@ -10,9 +10,9 @@ import java.util.Properties;
 * Created by Administrator on 2016/4/13.
 */
public class Config {
    public static String ip;
    public static String port;
    public static String clazz;
    public static String method;
    public static String url;
    public static String namespace;
    public static String monogoUrl;
    public static String startCollect;
}

+ 32 - 14
Hos-Resource-Rest/src/main/java/com/yihu/hos/gateway/control/rpc/impl/ResourceRpcImpl.java

@ -8,6 +8,8 @@ import com.yihu.hos.config.Config;
import com.yihu.hos.gateway.control.rpc.IResourceRpc;
import com.yihu.hos.gateway.model.rpc.RPCResponseResult;
import com.yihu.hos.gateway.util.Constant;
import com.yihu.hos.gateway.util.RPCUtil;
import com.yihu.hos.resource.util.httpclient.HttpClientUtil;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
@ -25,6 +27,7 @@ import java.util.Map;
 */
public class ResourceRpcImpl implements IResourceRpc {
    private DBHelper dbHelper = new DBHelper();
    private HttpClientUtil httpClientUtil = new HttpClientUtil();
    @Override
    public String transport(String RPCRequestXml) {
@ -43,11 +46,8 @@ public class ResourceRpcImpl implements IResourceRpc {
            String trancode = root.element("TransactionCode").toString();
            if (trancode.equals(Constant.PATIENT_INFORMATION)) {
                String cardNo = root.element("Data").element("CardNo").toString();
                ;
                String cardType = root.element("Data").element("CardType").toString();
                ;
                String patientId = root.element("Data").element("PatientId").toString();
                ;
                //查询病人基本信息
                patientInformation(cardNo, cardType, patientId);
            } else if (trancode.equals(Constant.PUSHREPORT)) {
@ -76,24 +76,32 @@ public class ResourceRpcImpl implements IResourceRpc {
                        "<PatientId>" + patientId + "</PatientId>" +
                        "</Data>" +
                        "</Req>";
        InvokeRequest r = new InvokeRequest(new Url(Config.ip, Integer.valueOf(Config.port)), Config.clazz, Config.method, String.class);
        r.setSyncMode();//设置调用模式为同步模式
        Object s = Rpc.invoke(r);
        Class[] classes = new Class[]{String.class};
        Object[] v = new Object[1];
        v[0] = data;
        Object s = RPCUtil.getRPCData("report.ReportWs.GetReportInfo", classes, v);
        //解析xml
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(s.toString());
        Element root = document.getRootElement();
        if ("10000".equals(root.element("RespCode"))) {
            //xml轉json
            JSONObject obj = new JSONObject();
            obj.put(root.getName(), iterateElement(root));
            String jsonString = obj.toString();
            JSONObject obj = xml2json(root.element("Data"));
            //調用接口存入mongo
            Map<String, Object> params = new HashMap<String, Object>();
            httpClientUtil.doPost(Config.monogoUrl, params, null, null);
            //出發採集上傳
            params = new HashMap<String, Object>();
            httpClientUtil.doPost(Config.startCollect, params, null, null);
        }
    }
    private JSONObject xml2json(Element root) {
        JSONObject obj = new JSONObject();
        obj.put(root.getName(), iterateElement(root));
        return obj;
    }
    //检查检验报告
    private void pushreport(String reportType, String reportId) throws Exception {
        String data =
@ -104,9 +112,10 @@ public class ResourceRpcImpl implements IResourceRpc {
                        "<ReportType>" + reportType + "</ReportType>" +
                        "</Data>" +
                        "</Req>";
        InvokeRequest r = new InvokeRequest(new Url(Config.ip, Integer.valueOf(Config.port)), Config.clazz, Config.method, String.class);
        r.setSyncMode();//设置调用模式为同步模式
        Object s = Rpc.invoke(r);
        Class[] classes = new Class[]{String.class};
        Object[] v = new Object[1];
        v[0] = data;
        Object s = RPCUtil.getRPCData("report.ReportWs.GetReportInfo", classes, v);
        //解析xml
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(s.toString());
@ -121,13 +130,22 @@ public class ResourceRpcImpl implements IResourceRpc {
            if (elements != null && elements.size() > 0) {
                for (Object obj1 : elements) {
                    Element data1XML = (Element) obj1;
                    //調用接口存入mongo
                    Map<String, Object> params = new HashMap<String, Object>();
                    httpClientUtil.doPost(Config.monogoUrl, params, null, null);
                    //出發採集上傳
                    params = new HashMap<String, Object>();
                    httpClientUtil.doPost(Config.startCollect, params, null, null);
                }
            }
            obj.put(root.getName(), iterateElement(root));
            String jsonString = obj.toString();
            //調用接口存入mongo
            Map<String, Object> params = new HashMap<String, Object>();
            httpClientUtil.doPost(Config.monogoUrl, params, null, null);
            //出發採集上傳
            params = new HashMap<String, Object>();
            httpClientUtil.doPost(Config.startCollect, params, null, null);
        }
    }

+ 54 - 0
Hos-Resource-Rest/src/main/java/com/yihu/hos/gateway/util/RPCUtil.java

@ -0,0 +1,54 @@
package com.yihu.hos.gateway.util;
import com.yihu.hos.config.Config;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import javax.xml.namespace.QName;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by Administrator on 2016/4/18.
 */
public class RPCUtil {
    public static String getRPCData(String methos, Class[] classes, Object[] v) {
        String SERVER_URL = Config.url;
        String namespace = Config.namespace;
        String xml = getObjectByAxis2CallClient(SERVER_URL, namespace, methos, v, classes);
        return xml;
    }
    public static String getObjectByAxis2CallClient(String webServiceAddr, String webserviceNamespace, String webServiceMethod,
                                                    Object[] inputValues, Class[] inputValuetype) {
        try {
            //新建RPC服务客户端
            RPCServiceClient serviceClient = new RPCServiceClient();
            Options options = serviceClient.getOptions();
            //设置URL
            EndpointReference targetEPR = new EndpointReference(webServiceAddr);
            options.setTo(targetEPR);
            //设置参数的类型
            //设置namespace和方法名
            QName opAddEntry = new QName(webserviceNamespace, webServiceMethod);
            //数组的第一个值为返回值
            return (String) (serviceClient.invokeBlocking(opAddEntry, inputValues, inputValuetype))[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static void main(String[] args) {
        String SERVER_URL = "http://service.yihu.com:8080/WSGW/services/ServiceGateWay";
        String namespace = "http://com.yihu.wsgw/ServiceGateWay";
        String methos = "yy.yygh.QueryNumbers";
        Class[] classes = new Class[]{String.class};
        Object[] v = new Object[1];
        v[0] = "";
        String xml = getObjectByAxis2CallClient(SERVER_URL, namespace, methos, v, classes);
    }
}

+ 4 - 4
Hos-Resource-Rest/src/main/java/com/yihu/hos/resource/base/App.java

@ -79,10 +79,10 @@ public class App {
        }
    }
    private static void readpop(){
        Config.ip= prop.getProperty("rpc.ip");
        Config.port = prop.getProperty("rpc.port");
        Config.clazz = prop.getProperty("rpc.clazz");
        Config.method = prop.getProperty("rpc.method");
        Config.url= prop.getProperty("rpc.url");
        Config.namespace = prop.getProperty("rpc.namespace");
        Config.monogoUrl = prop.getProperty("http.monogourl");
        Config.startCollect = prop.getProperty("http.startCollect");
    }
    /**
     * spring boot 定时任务

+ 5 - 4
Hos-Resource-Rest/src/main/resources/rpc.properties

@ -1,4 +1,5 @@
rpc.ip=192.168.2.1
rpc.port=4430
rpc.clazz=4430
rpc.method=4430
rpc.url=http://service.yihu.com:8080/WSGW/services/ServiceGateWay
rpc.namespace=http://com.yihu.wsgw/ServiceGateWay
http.monogourl=http://localhost:8080/datapush/datapush
http.startCollect=http://localhost:8080/datapush/datapush