axis2 调用webservice

来源:互联网 发布:网络销售招聘 编辑:程序博客网 时间:2024/04/29 05:55



import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


public class ElderlyExamInfoTask extends AbstractService implements IElderlyExamInfoTask,Task {

    //webservice的发布地址 

    private static final String SOAP_BINDING_ADDRESS = “http://172.29.198.31:8090/meso/ws/noticeService?wsdl”


    //webservice的命名空间 targetNamespace = "http://web.medexa.founder.com/"

   private static final String TARGET_NAMESPACE = "http://web.medexa.founder.com/";


     //webservice的方法的action action = "http://web.medexa.founder.com/send",如果方法没有action地址,则不写option.toAction

    private static final String WEB_METHOD_SEND_ACTION = "http://web.medexa.founder.com/send";


    //webservice的目标方法名:xml中为<hm:findExamReportInfo></hm:findExamReportInfo>
    private static final String METHOD_SEND_NAME = "findExamReportInfo";
    

    @Resource
    private IPhysicalExamRecordService physicalExamRecordService;
    
    @Resource(name = "elderlyPhyExaminationDao")
    private IElderlyPhyExaminationDao elderlyPhyExaminationDao;
    
    @Resource(name = "physicalExamRecordDao")
    private IPhysicalExamRecordDao physicalExamRecordDao;
    
    @Resource(name = "ehrHealthEventDao")
    private IEHRHealthEventDao ehrHealthEventDao;
    
    @Autowired
    private IHealthExaminationDao healthExaminationDao;
   
    @Override
    public void run(Map<String, Object> data) {
       
        //查询为体检的老年人信息

        Criteria criteria = new Criteria();
        criteria.add("confirm", 1);

        List<PhysicalExamRecord> list = physicalExamRecordService.getPhysicalExamRecords(criteria);

        int currentYear = DateUtil.getCurrentYear();


        for(PhysicalExamRecord per : list){

            Map<String, Object> param = new HashMap<String, Object>();

            param.put("year", currentYear);
            param.put("type", 5);
            param.put("idCode", per.getIdcard());
            param.put("name", per.getName());
            param.put("personId", per.getPersonId());
            param.put("examRecordId", per.getId());
            param.put("physicalExamRecord", per);
            requestData(param);
        }
        
    }

    
    

    private void requestData(Map<String, Object> param){

         //请求 并返回结果

         OMElement captureResult = sendRequest(WEB_METHOD_SEND_ACTION, METHOD_SEND_NAME, param);

         //处理返回的结果

         saveExamInfo(captureResult,(PhysicalExamRecord)param.get("physicalExamRecord"));

    }

     /**
     * 调webservice,得到数据
     * @param methodName
     * @param param
     * @return
     */

     //soapUI用xml请求   ,参数为year,type,name,idCode

    private OMElement sendRequest(String methodAction, String methodName, Map<String, Object> param) {


        //传入websevice的发布地址

        EndpointReference endpointReference = new EndpointReference(SOAP_BINDING_ADDRESS);

        //创建一个OMFactory,下面的namespace、方法与参数均需由它创建
        OMFactory factory = OMAbstractFactory.getOMFactory();

        //下面创建命名空间,如果你的WebService指定了targetNamespace属性的话,就要用这个,对应于 @WebService(targetNamespace =

        //"http:  //transmitter.ws.rhip.founder.com");  “web” 对应为soapUI请求xm中的 <soapenv:Body><web:findExamReportInfo></web:findExamReportInfo>  

      
        OMNamespace namespace = factory.createOMNamespace(“http://web.medexa.founder.com/”, "web");

        //下面创建一个method对象,"METHOD_NAME"为方法名 对应<web:findExamReportInfo>
        OMElement method = factory.createOMElement(“findExamReportInfo”, “http:  //transmitter.ws.rhip.founder.com”);


        //请求参数 参数为year,type,name,idCode

        if (param != null) {
            
            //下面创建的是参数对数,对应于@WebParam(name = "param")
            //由于@WebParam没有指定targetNamespace,所以下面创建name参数时,用了null,否则你得赋一个namespace给它
            OMElement paramElement1 = factory.createOMElement("year", null);
            paramElement1.addChild(factory.createOMText(paramElement1, String.valueOf(param.get("year"))));
            method.addChild(paramElement1);
            
            OMElement paramElement2 = factory.createOMElement("type", null);
            paramElement2.addChild(factory.createOMText(paramElement2, String.valueOf(param.get("type"))));
            method.addChild(paramElement2);
            
            OMElement paramElement3 = factory.createOMElement("idCode", null);
            paramElement3.addChild(factory.createOMText(paramElement3, String.valueOf(param.get("idCode"))));
            method.addChild(paramElement3);
            
           /* OMElement paramElement4 = factory.createOMElement("name", null);
            paramElement4.addChild(factory.createOMText(paramElement4, String.valueOf(param.get("name"))));
            method.addChild(paramElement4);*/
        }

        Options options = new Options();

        //此处对应于@WebMethod(action = "http://transmitter.ws.rhip.founder.com/send");如果方法没有配置@WebMethod(action=“”,则不需要options.setAction
        //options.setAction("http://transmitter.ws.rhip.founder.com/send");

        options.setTo(endpointReference);

       //该处非必须:版本兼容问题   请求超时问题报错options.setManageSession(true);  options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);        
sender.cleanupTransport();

        options.setManageSession(true);  

   //设置Http客户端连接可以复用

        options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);  
        ServiceClient sender = null;
        try {
            sender = new ServiceClient();
        } catch (AxisFault axisFault) {
            log.error(axisFault.getMessage());
            axisFault.printStackTrace();
        }
        sender.setOptions(options);

        //发送并得到结果,至此,调用成功,并得到了结果
        OMElement result = null;
        try {
            result = sender.sendReceive(method);
        } catch (AxisFault axisFault) {
            log.error(axisFault.getMessage());
            axisFault.printStackTrace();
        }
        try {
            sender.cleanupTransport();
        } catch (AxisFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        //下面的输出结果为//下面的输出结果为<xsd:test xmlns:xsd="http://transmitter.ws.rhip.founder.com"><callback>java</callback></xsd:test>
        return result;

    }



    private void saveExamInfo(OMElement captureResult, PhysicalExamRecord physicalExamRecord) {

        Iterator iterator = captureResult.getChildElements();

          //每一层节点处理,获取值

         //Iterator iterator = captureResult.getChildElements();  最外层

         //  OMNode omNode = (OMNode) iterator.next();OMElement omElement = (OMElement) omNode; omElement.getLocalName()得到节点的名称如《ExamReportInfo》

        //omElement.getChildrenWithLocalName("dtlConclusionList");父节点得到名为“dtlConclusionList”的子节点,节点类型都为Iterator

       //String name = ((OMElement)((OMNode) omElementC.getChildrenWithLocalName("name").next())).getText();父节点得到名为“name”的子节点值1,<name>1</name>

        while (iterator.hasNext()) {
            OMNode omNode = (OMNode) iterator.next();
            if (omNode.getType() == OMNode.ELEMENT_NODE) {
                OMElement omElement = (OMElement) omNode;
                if (omElement.getLocalName().equals("ExamReportInfo")) {
                    //设置 ElderlyPhyExamination值
                    Iterator dtlConclusionLists = omElement.getChildrenWithLocalName("dtlConclusionList");
                    setHealthExamValue(dtlConclusionLists, "dtlConclusionList");    
                 }
            }
        }
    }

  

/**得到节点值*/


    private String getSmbUpdatedFailureReportLogsIds(OMElement captureResult) {
        Iterator iterator = captureResult.getChildElements();

        while (iterator.hasNext()) {
            OMNode omNode = (OMNode) iterator.next();
            if (omNode.getType() == OMNode.ELEMENT_NODE) {
                OMElement omElement = (OMElement) omNode;
                if (omElement.getLocalName().equals("updatedFailureIds")) {
                   return omElement.getText();
                }
            }
        }
        return "";
    }


    /**得到节点值*/

    private void setHealthExamValue(HealthExamination healthExam, Iterator targetNoticeInfoLists){
        while (targetNoticeInfoLists.hasNext()) {  
            OMNode omNodeC = (OMNode) targetNoticeInfoLists.next();  
            OMElement omElementC = (OMElement) omNodeC;
            String name = ((OMElement)((OMNode) omElementC.getChildrenWithLocalName("name").next())).getText();
            String genderCode = ((OMElement)((OMNode) omElementC.getChildrenWithLocalName("genderCode").next())).getText();
        }
    }
    

}