---恢復內容開始---java
package util; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigDecimal; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; import org.directwebremoting.json.types.JsonArray; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import hry.core.constant.StringConstant; import hry.core.quartz.QuartzJob; import hry.core.quartz.QuartzManager; import hry.core.quartz.ScheduleJob; import hry.core.util.date.DateUtil; import hry.core.util.sys.ContextUtil; import hry.redis.common.utils.RedisService; /** * 調用匯率 * * @author Administrator * */ @Service @Scope public class HuiLvService { private static final Logger log = Logger.getLogger(HuiLvService.class); /** * @param urlAll * :請求接口 * @param httpArg * :參數 * @return 返回結果 */ public static void getHuilvData() { log.info("-----進入匯率定時器-----"); RedisService redisService = (RedisService) ContextUtil.getBean("redisService"); JSONArray obj= JSON.parseArray(redisService.get("configCache:financeConfig")); for(Object o:obj){ JSONObject oo=JSON.parseObject(o.toString()); if("ChooseRate".equals(oo.getString("configkey"))){ String value =oo.getString("value"); if("1".equals(value)){ redisService.save("USDCNY", "0"); log.info("讀取後臺手動配置的USDCNY匯率"); return; } } } BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); long currentTime=System.currentTimeMillis(); String httpUrl = "http://hq.sinajs.cn/"; String httpArg = "rn="+currentTime+"list=fx_susdcny"; httpUrl = httpUrl + "?" + httpArg; try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "GBK")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); String [] arr = result.split(","); redisService.save("USDCNY", arr[8]); log.info("最新USDCNY匯率:" +new BigDecimal(arr[8])); } catch (Exception e) { e.printStackTrace(); } } /** * getHuilvData 定時器 * <p> * TODO * </p> * */ public static void getHuilvDataJob() { ScheduleJob job = new ScheduleJob(); job.setBeanClass("util.HuiLvService"); job.setMethodName("getHuilvData"); // Object[] object = { 1 }; // job.setMethodArgs(object); QuartzManager.addJob("huilvData", job, QuartzJob.class, "0 0/5 * * * ?");//5分鐘 } public static void main(String[] args) { String httpUrl = "http://hq.sinajs.cn/"; String httpArg = "rn=1526351165800list=fx_susdcny"; long currentTime=System.currentTimeMillis(); System.out.println(currentTime); // String send = getHuilvData(httpUrl, httpArg); // String send = HttpConnectionUtil.getSend(httpUrl, httpArg); // System.out.println(send); } }
---恢復內容結束---web