`

java 定时抓取数据(axis--解析java调用webservice)

    博客分类:
  • java
 
阅读更多
package com.ss.apitask;

import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.*;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ss.data.ISubsDomain;
import com.ss.notice.NotifyService;

import com.inspur.paas.StartSubscribe;
import com.inspur.paas.util.*;
import javax.xml.namespace.QName;


/**
*思路:定时抓取数据(线程睡眠机制),首先查询哪些用户订阅了数据,然后根据这些用户的社保账号信息,
* 取社保详情,之后比对数据,上次抓取的数据是否记录在案,否则插入数据(抓取数据完成)
*
**/

public class ShebaoApiWorker extends ApiWorker {
	
	private String[] columns = {"cardID","password"};

	private static Log log = LogFactory.getLog(ShebaoApiWorker.class);
	private static String send_id=ConfUtil.getValue("mc_subscribe_sendid").toString();
	private static String send_name=ConfUtil.getValue("site_name").toString();
	private static String msg_type=ConfUtil.getValue("mc_shebao_msgtype").toString();

	private static ISubsDomain subsDomain;
	private List subsList = new ArrayList();
	
	public static ISubsDomain getSubsDomain() {
		if (subsDomain != null) {
			return subsDomain;
		} else {
			subsDomain = (ISubsDomain) StartSubscribe.getApplicationContext()
					.getBean("SubsDomain", ISubsDomain.class);
			return subsDomain;
		}
	}
	
	public void run() {
		
		Map shebaoParam = new HashMap();
		String cache_time = (String) this.apiInfo.get("cache_time");
		Map param = new HashMap();
		// 订阅用户
		subsList = getSubsList();
		try {
			if(subsList.size()>0){
				List<Map> shebaoData = new ArrayList();
				for(int k=0, len = subsList.size();k<len;k++){
					Map temp = (Map) subsList.get(k);
					//社保账号
					String note = temp.get("note").toString();
					//解密
					Map shebaoParamInfo = getDecode(JsonUtil.readToObject(note, Map.class));
					shebaoParamInfo.put("uid", temp.get("uid"));
					//抓数据
					Map shebaoInfo = getSheBaoInfo(shebaoParamInfo);
					//数据List
					shebaoData.add(shebaoInfo);
				}
				if(shebaoData.size()>0){
					//存数据和发消息
					parseData(shebaoData);
				}
			}
			subsList.clear();
			long start = System.currentTimeMillis();
			//睡眠
			Calendar cl = Calendar.getInstance();
			int day = Integer.parseInt(cache_time.replace("d", ""));
			cl.add(Calendar.DATE, day);
			Date d = new Date();
			long sleepTime = cl.getTimeInMillis()-d.getTime();
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				long end = System.currentTimeMillis();
				log.error("社保抓取数据时sleep时间在:"+(end-start)+"毫秒时出现异常", e);
			}
		} catch (IOException e) {
			
			long start = System.currentTimeMillis();
			//睡眠
			Calendar cl = Calendar.getInstance();
			int day = Integer.parseInt(cache_time.replace("d", ""));
			cl.add(Calendar.DATE, day);
			Date d = new Date();
			long sleepTime = cl.getTimeInMillis()-d.getTime();
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e1) {
				long end = System.currentTimeMillis();
				log.error("社保抓取数据时sleep时间在:"+(end-start)+"毫秒时出现异常", e1);
			}
			log.error("社保抓取数据出错"+(List)subsList, e);
		}
	}
	
	//数据量比较大时,查询会对数据库造成大压力,可以每次查询5000次
	private synchronized List getSubsList(){
		if(subsList == null){
			subsList = new ArrayList();
		}
		if(subsList.size()>0){
			return subsList;
		}
		subsList.clear();
		int num = 5000;
		int nowCount = 0;
		int page = 1;
		
		Map sub = new HashMap();
		sub.put("uc_api_id", this.apiId);
		sub.put("num", num);
		
		do{
			sub.put("page", page);
			page++;
			Map subMap = getSubsDomain().getSubss(sub);
			List subList = (List) subMap.get("data");
			nowCount = subList.size();
			
			for(int k=0, len = subList.size();k<len;k++){
				subsList.add(subList.get(k));
			}
			
		}while(nowCount == num);
		System.out.println("subsMap 次数"+subsList.size());
		return subsList;
	}
	
	private synchronized int parseData(List<Map> dataList){
		if(dataList.size()<0){
			return 0;
		}
		
		for(Map temp : dataList){
			int uid = Integer.parseInt(temp.get("uid").toString());
			Map payInfo = new HashMap();
			//yanglao
			boolean yangl = false;
			String yanglDay = "";
			if(temp.get("yanglao") != null){
				
				Map yanglao = (Map)temp.get("yanglao");
				Map yanglaoParam = new HashMap();
				
				yanglaoParam.put("uid", uid);
				yanglaoParam.put("type", "shebao");
				yanglaoParam.put("title", "yanglao");
				try {
					yanglaoParam.put("content", JsonUtil.convertToString(yanglao));
				} catch (IOException e) {
					log.error("社保-养老转json出错:"+yanglao, e);
				}
				String yanglaoHash = CodeUtil.md5(yanglao.get("jfdate").toString()+yanglao.get("ysdate").toString()+String.valueOf(uid)+"yanglao");
				yanglaoParam.put("hash", yanglaoHash);
				Map perYanglao = getDataDomain().getDataMap(yanglaoParam); 
				if(perYanglao.size() == 0){
					getDataDomain().addData(yanglaoParam);
					yangl = true;
					yanglDay = yanglao.get("jfdate").toString();
				}
			}
			payInfo.put("yangl", yangl);
			payInfo.put("yanglDay", yanglDay);
			
			//yiliao
			boolean yil = false;
			String yilDay = "";
			if(temp.get("yiliao") != null){
				Map yiliao= (Map)temp.get("yiliao");
				Map yiliaoParam = new HashMap();
				
				yiliaoParam.put("uid", uid);
				yiliaoParam.put("type", "shebao");
				yiliaoParam.put("title", "yiliao");
				try {
					yiliaoParam.put("content", JsonUtil.convertToString(yiliao));
				} catch (IOException e) {
					log.error("社保-医疗转json出错:"+yiliao, e);
				}
				String yiliaoHash = CodeUtil.md5(yiliao.get("jfdate").toString()+yiliao.get("ysdate").toString()+String.valueOf(uid)+"yiliao");
				yiliaoParam.put("hash", yiliaoHash);
				Map perYiliao = getDataDomain().getDataMap(yiliaoParam); 
				if(perYiliao.size() == 0){
					getDataDomain().addData(yiliaoParam);
					yil = true;
					yilDay = yiliao.get("jfdate").toString();
				}
			}
			payInfo.put("yil", yil);
			payInfo.put("yilDay", yilDay);
			
			//shiy
			boolean shiy = false;
			String shiyDay = "";
			if(temp.get("shiye") != null){
				Map shiye = (Map)temp.get("shiye");
				Map shiyeParam = new HashMap();
				
				shiyeParam.put("uid", uid);
				shiyeParam.put("type", "shebao");
				shiyeParam.put("title", "shiye");
				//内容shiye
				//============================ToDO
				try {
					shiyeParam.put("content", JsonUtil.convertToString(shiye));
				} catch (IOException e) {
					log.error("社保-失业转json出错:"+shiye, e);
				}
				String shiyeHash = CodeUtil.md5(shiye.get("jfdate").toString()+shiye.get("ysdate").toString()+String.valueOf(uid)+"shiye");
				shiyeParam.put("hash", shiyeHash);
				Map perShiye = getDataDomain().getDataMap(shiyeParam); 
				if(perShiye.size() == 0){
					getDataDomain().addData(shiyeParam);
					shiy = true;
					shiyDay = shiye.get("jfdate").toString();
				}
			}
			payInfo.put("shiy", shiy);
			payInfo.put("shiyDay", shiyDay);
			sendMsg(uid, payInfo);
		}
		return 1;
	}
	
	private Map getDecode(Map map){
			if (map != null) {
				for (int i = 0; i < columns.length; i++) {
					String column = columns[i];
					if(map.get(column) != null){
						map.put(column,AESUtil.decode(String.valueOf(map.get(column))));
					}
				}
			}
			return map;
	}
	
	private void sendMsg(int uid, Map payInfo){
		
		if( uid <= 0 ){//update 数据
			return;
		}
		String str = "";
		if(Boolean.parseBoolean(payInfo.get("yangl").toString())){
			str += "养老保险("+payInfo.get("yanglDay").toString()+")";
		}
		if(Boolean.parseBoolean(payInfo.get("yil").toString())){
			str += "、医疗保险("+payInfo.get("yilDay").toString()+")";
		}
		if(Boolean.parseBoolean(payInfo.get("shiy").toString())){
			str += "、失业保险("+payInfo.get("shiyDay").toString()+")";
		}
		
		if(!("".equals(str))){
			Map notifyMap = new HashMap();
			notifyMap.put("send_id", send_id);
			notifyMap.put("user_id", uid);
			notifyMap.put("send_name", send_name);
			notifyMap.put("msg_type", msg_type);
			notifyMap.put("param1", str);
			NotifyService.addNotification(notifyMap);
		}
		
	}
	
	private Map getSheBaoInfo(Map shebaoParam){
		
		Map res = new HashMap();
		res.put("uid", shebaoParam.get("uid").toString());
		String url = ConfUtil.getValue("shebao_getInfo").toString();
		String cardID = shebaoParam.get("cardID").toString();
		String password = shebaoParam.get("password").toString();
		Calendar cal = Calendar.getInstance(Locale.CHINA);  
		String nowYear = new SimpleDateFormat( "yyyy").format(cal.getTime());
		String nowMonth = new SimpleDateFormat( "MM").format(cal.getTime());
		
//		登录CheckLogin(string cardID, string password, string userCode)
		String soapaction = "http://tempuri.org/";
		String funcnameLogin = "CheckLogin";
		String paramsLogin[] = {"cardID","password","userCode"};
		String runParamsLogin[] = {cardID,password,"lc"};
		Boolean isLogin = false; 
		String contentLogin = "";
		try {
			contentLogin = callWsApiForAxis(url, soapaction, funcnameLogin, paramsLogin, runParamsLogin);
//			System.out.println("LoginResXml===="+contentLogin);
		} catch (Exception e1) {
			log.error("调登录出错"+funcnameLogin, e1);
		}
		if(contentLogin != null && !"".equals(contentLogin) && !"1".equals(contentLogin) && !"2".equals(contentLogin) && !"0".equals(contentLogin)){
			Map loginRes = xmlLogin(contentLogin);
			if(loginRes != null){
				isLogin = true;
			}
			System.out.println("loginRes=="+loginRes);
		}
		
		if(isLogin){
//			养老GetSocialInsuranceInfo(string cardID, string year, string month, string userCode)
			String funcnameYanglao = "GetSocialInsuranceInfo";
			String paramsYanglao[] = {"cardID","year","month","userCode"};
			String runParamsYanglao[] = {cardID,nowYear,nowMonth,"lc"};
			String contentYanglao = "";
			try {
				contentYanglao = callWsApiForAxis(url, soapaction, funcnameYanglao, paramsYanglao, runParamsYanglao);
//				System.out.println("YanglaoResXml===="+contentYanglao);
			} catch (Exception e2) {
				log.error("调养老出错"+funcnameYanglao, e2);
			}
			if(contentYanglao != null && !"".equals(contentYanglao) &&  !"1".equals(contentYanglao) && !"2".equals(contentYanglao) && !"3".equals(contentYanglao)){
				Map yanglaoRes = xmlShebao(contentYanglao);
				if(yanglaoRes != null){
					res.put("yanglao", yanglaoRes);
				}
				System.out.println("YanglaoRes===" +yanglaoRes);
			}
//			医疗GetMedicalInsuranceInfo (string cardID, string year, string month, string userCode)
			String funcnameYiliao = "GetMedicalInsuranceInfo";
			String paramsYiliao[] = {"cardID","year","month","userCode"};
			String runParamsYiliao[] = {cardID,nowYear,nowMonth,"lc"};
			String contentYiliao = "";
			
			try {
				contentYiliao = callWsApiForAxis(url, soapaction, funcnameYiliao, paramsYiliao, runParamsYiliao);
//				System.out.println("YiliaoResXml===="+contentYiliao);
			} catch (Exception e2) {
				log.error("调医疗出错"+funcnameYiliao, e2);
			}
			if(contentYiliao != null && !"".equals(contentYiliao) &&  !"1".equals(contentYiliao) && !"2".equals(contentYiliao) && !"3".equals(contentYiliao)){
				Map yiliaoRes = xmlShebao(contentYiliao);
				if(yiliaoRes != null){
					res.put("yiliao", yiliaoRes);
				}
				System.out.println("YiliaoRes===" +yiliaoRes);
			}
			
//			失业GetIdlenessInfo (string cardID, string year, string month, string userCode)
			String funcnameShiye = "GetIdlenessInfo";
			String paramsShiye[] = {"cardID","year","month","userCode"};
			String runParamsShiye[] = {cardID,nowYear,nowMonth,"lc"};
			String contentShiye = "";
			
			try {
				contentShiye = callWsApiForAxis(url, soapaction, funcnameShiye, paramsShiye, runParamsShiye);
//				System.out.println("ShiyeResXml===="+contentShiye);
			} catch (Exception e3) {
				log.error("调失业出错"+funcnameShiye, e3);
			}
			if(contentShiye != null && !"".equals(contentShiye) &&  !"1".equals(contentShiye) && !"2".equals(contentShiye) && !"3".equals(contentShiye)){
				Map shiyeRes = xmlShebao(contentShiye);
				if(shiyeRes != null){
					res.put("shiye", shiyeRes);
				}
				System.out.println("ShiyeRes===" +shiyeRes);
			}
			
//			退出LogOut()
			String funcnameLogout = "LogOut";
			String paramsLogout[] = null;
			String runParamsLogout[] = null;
			String contentLogout = "";
			try {
				contentLogout = callWsApiForAxis(url, soapaction, funcnameLogout, paramsLogout, runParamsLogout);
				System.out.println("LogoutResXml===="+contentLogout);
			} catch (Exception e1) {
				log.error("调退出出错"+funcnameLogin, e1);
			}
		}
		System.out.println("调取接口的所有Res================"+res);
		return res;
	}
	public String callWsApiForAxis(String url,String soapaction,String funcname,String params[],String runParams[])throws Exception{
		String resXmlStr = "";
		try {
			Service service = new Service();
			Call call;
			call = (Call) service.createCall();
			call.setTargetEndpointAddress(url);
			call.setOperationName(new QName(soapaction, funcname)); // 设置要调用哪个方法
			if(params != null){
				for(int i = 0 ; i < params.length ; i++){
					call.addParameter(
							new QName(soapaction, params[i]), // 设置要传递的参数
							org.apache.axis.encoding.XMLType.XSD_STRING,
							javax.xml.rpc.ParameterMode.IN);
				}
			}
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 返回类型
			call.setUseSOAPAction(true);
			call.setSOAPActionURI(soapaction + funcname);
			resXmlStr = (String) call.invoke(runParams);
		} catch (AxisFault e) {
			log.error("调用webservice数据出错"+funcname, e);
		}
		return resXmlStr;
	}
	public Map xmlShebao(String resXmlStr){
		if(resXmlStr == null && "".equals(resXmlStr)){
			return null;
		}
		Map res = new HashMap();
		try{
			StringReader read = new StringReader(resXmlStr);
			InputSource source = new InputSource(read);
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(source);
			Element root = doc.getRootElement();
			List jiedian = root.getChildren();
			if(jiedian.size()>0){
				root = (Element) jiedian.get(0);
				jiedian = root.getChildren();
				Element et = null;
				for (int i = 0; i < jiedian.size(); i++) {
					et = (Element) jiedian.get(i);
					//et.getAttributeValue("id") 获取xml里id的属性值
					if(et.getAttributeValue("id")!= null && "jfdate".equals(et.getAttributeValue("id").toString())){
						res.put("jfdate", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "ysdate".equals(et.getAttributeValue("id").toString())){
						res.put("ysdate", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "dwname".equals(et.getAttributeValue("id").toString())){
						res.put("dwname", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "jftype".equals(et.getAttributeValue("id").toString())){
						res.put("jftype", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "grjs".equals(et.getAttributeValue("id").toString())){
						res.put("grjs", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "dwjf".equals(et.getAttributeValue("id").toString())){
						res.put("dwjf", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "spjf".equals(et.getAttributeValue("id").toString())){
						res.put("spjf", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "jfbz".equals(et.getAttributeValue("id").toString())){
						res.put("jfbz", et.getText());
					}
				}
			 }
			}catch (Exception e) {
				log.error("解析webservice返回数据出错"+resXmlStr, e);
			}
			return res;
		}
	
	public Map xmlLogin(String resXmlStr){
		
		if(resXmlStr == null && "".equals(resXmlStr)){
			return null;
		}
		Map res = new HashMap();
		try{
			StringReader read = new StringReader(resXmlStr);
			InputSource source = new InputSource(read);
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(source);
			Element root = doc.getRootElement();
			List jiedian = root.getChildren();
			if(jiedian.size()>0){
				root = (Element) jiedian.get(0);
				jiedian = root.getChildren();
				Element et = null;
				Map yanglao = new HashMap();
				for (int i = 0; i < jiedian.size(); i++) {
					et = (Element) jiedian.get(i);
					if(et.getAttributeValue("id")!= null && "cardID".equals(et.getAttributeValue("id").toString())){
						res.put("cardID", et.getText());
					}
					if(et.getAttributeValue("id")!= null && "name".equals(et.getAttributeValue("id").toString())){
						res.put("name", et.getText());
				 }
			  }
			}
		}catch (Exception e) {
			log.error("解析登录返回数据出错"+resXmlStr, e);
		}
		return res;
	}

}

 

分享到:
评论

相关推荐

    java-webservice-axis-例子

    当服务端接收到请求时,Axis会将SOAP消息解析成Java对象,然后调用相应的服务方法。在客户端,Axis会将Java对象转换为SOAP消息发送给服务。 4. **示例代码**:在exampleforwebservice文件中,可能包含以下组件: -...

    [JAVA]-Axis-Webservice-Demo

    Java Axis Web服务示例(`[JAVA]-Axis-Webservice-Demo`)提供了发布和调用Web服务的实际操作步骤。通过学习和实践这个示例,开发者可以更好地掌握使用Axis创建Web服务的技术,理解Web服务的基本原理,以及如何通过...

    axis-jaxrpc-1.4.jar

    axis-jaxrpc-1.4.jar jaxrpc.jar webservice java工具类jar包。

    axis-bin-1_4.zip

    标题中的"axis-bin-1_4.zip"是一个与 Axis1 相关的压缩包,Axis1 是一个开源的 Java 库,主要用于创建、部署和使用 Web Services。这个版本(1.4)是 Axis 的一个稳定版本,包含了 Axis 框架的可执行文件和其他必要...

    axis调用webservice接口所需jar包.rar

    axis调用第三方webservice接口所需的jar包,包括axis-1.4.jar,axis-jaxrpc-1.3.jar,axis-jaxrpc-1.4.jar,commons-discovery-0.5.jar,commons-logging-1.2.jar,cxf-rt-databinding-jaxb-3.3.1.jar,javax.mail-...

    axis-jaxrpc1.4

    标题“axis-jaxrpc1.4”所指的是一款名为Axis的Java XML Web服务框架中的一个组件,即JAX-RPC(Java API for XML-based Remote Procedure Calls)的1.4版本。Axis是Apache软件基金会开发的一个开源项目,主要用于...

    java调用webservice的axis2.jar包

    Java调用WebService是Web服务交互的一种常见方式,而Axis2是一个高效的、可扩展的Web服务框架,它在Java世界中广泛用于创建和消费Web服务。本文将深入探讨使用Axis2.jar包来调用WebService的相关知识点。 一、Axis2...

    Java+Axis2调用Web Services 网络接口

    本文将深入探讨如何利用Java和Axis2库来实现这一功能,同时结合提供的代码示例进行详细解析。 首先,Web服务是一种通过网络进行通信的软件系统,通常基于WSDL(Web Services Description Language)定义接口,SOAP...

    Java-tomcat-axis2开发webservice返回json数据

    标题“Java-tomcat-axis2开发webservice返回json数据”涉及的是使用Java、Tomcat服务器以及Axis2框架来创建Web服务,并返回JSON格式的数据。这是一个常见的技术组合,用于构建RESTful API或者提供服务化接口。下面...

    java 调用https webservice实例及axis包

    通常包括`axis.jar`, `axis-ant.jar`, `commons-discovery.jar`, `commons-logging.jar`, `jaxrpc.jar`, `saaj.jar`等。 2. **生成客户端代理类**:使用Axis的`wsdl2java`工具,从Web Service的WSDL(Web Service...

    axis.jar,axis-saaj-1.4.jar

    标题中的"axis.jar"和"axis-saaj-1.4.jar"是两个重要的Java库文件,它们在Web服务开发中扮演着核心角色。本文将详细介绍这两个库以及它们与Web服务的关系。 首先,让我们深入了解Axis。Axis是Apache软件基金会的一...

    axis2发布webservice和调用axis2服务接口

    1. **创建WebService**:在Axis2中,可以通过编写一个简单的Java类并暴露其方法作为Web服务接口。这个类通常会遵循SOAP协议,定义服务操作。例如,你可以创建一个名为`HelloWorldService`的类,包含一个`sayHello`...

    java 通过axis调用天气预报的webservice

    标题中的“Java通过Axis调用天气预报的WebService”是指使用Java编程语言,通过Apache Axis库来访问和使用公开的天气预报Web服务。Apache Axis是一个开放源码的SOAP(简单对象访问协议)工具包,它允许开发者创建和...

    webService-Axis-tomcat发布教程.docx编程资料

    ### WebService-Axis-Tomcat 发布教程详细解析 #### 一、准备工作 在开始发布 WebService 之前,首先需要完成一系列的准备工作。 ##### 1. 安装 Axis - **下载 Axis 包**:访问 Apache Axis 的官方网站 ...

    Java Axis 直接调用webservice 接口工具类

    Java Axis 直接调用webservice 接口工具类, 无需根据wsdl编译客户端,需要axis相关包,测试使用axis 1.4, 附demo引入axis包后直接可以测试;

    axis-1_4(bin)用axis进行webservice开发

    标题中的“axis-1_4(bin)用axis进行webservice开发”表明了本文将围绕Axis 1.4版本,这是一个用于构建Web服务的开源工具,主要应用于Java平台。Axis允许开发者快速、简单地创建和部署Web服务,同时也支持消费其他Web...

    axis-1_4.zip

    标题中的"axis-1_4.zip"表明这是一个与Axis相关的软件包,具体是Axis1的第4个版本。Axis是Apache软件基金会开发的一个开源项目,主要用于构建和部署Web服务。在Web服务的世界里,Axis扮演着客户端和服务端的SOAP绑定...

    java axis 调用webservice服务端

    Java Axis调用WebService服务端是Java开发者在进行分布式系统交互时常见的一种技术实践。Axis是Apache软件基金会开发的一个开源项目,它提供了一种方便、高效的方式,使得Java应用程序能够作为客户端来消费或作为...

    axis-bin-1_4.zip_Axis1.4-bin_axis 1.4_axis-1_4.rar_axis-bin-_axi

    "axis-1_4.rar"可能是同一版本的另一种压缩格式,而"axis-bin-_axi"可能是文件名的一部分或者一个错误的拼写。 Axis1.4的主要特点包括: 1. **SOAP支持**:Axis1.4支持SOAP 1.1协议,允许应用程序通过HTTP进行远程...

Global site tag (gtag.js) - Google Analytics