加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

escache访问webservice做缓存

发布时间:2020-12-17 00:18:10 所属栏目:安全 来源:网络整理
导读:最近项目中访问 webservice用escache做缓存,减少服务器压力 ? package com.cattsoft.itdc.roadTransport.webservice; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properti

最近项目中访问 webservice用escache做缓存,减少服务器压力

?

package com.cattsoft.itdc.roadTransport.webservice;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.json.JSONObject;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

/**
?*
?* @author ITS-HURY
?余票查询
?*/
public class RamainTicekets {
??? public static CacheManager singletonManager = null;
??? public static Properties prop? = getProperties();
??? /**
???? * 模拟多用户并发访问
???? * @param args
???? */
?public static void main(String[] args) {
??TestThread t1 = new TestThread("a");
??TestThread t2 = new TestThread("b");
??TestThread t3 = new TestThread("c");
??TestThread t4 = new TestThread("d");
??TestThread t5 = new TestThread("e");
??t1.start();
??t2.start();
??t3.start();
??t4.start();
??t5.start();
?}
?private final static class TestThread extends Thread{
??private String name ;
??public TestThread(String name) {
??this.name = name;
??}
?
??public void run() {
???System.out.println(this.name+"启动");
????Map map = new HashMap();
??????? map.put("BeginDrvDate","2013-06-01");
??????? map.put("EndDrvDate","2013-06-02");
??????? map.put("CarryStaId","");
??????? map.put("StopName","成都");
?????? System.out.println(getRamainTicekets(map));
?????? try {
????Thread.sleep(3000);
???} catch (InterruptedException e) {
????// TODO Auto-generated catch block
????e.printStackTrace();
???}
?????? System.out.println(getRamainTicekets(map));
???super.run();
??}
?}
?/**
? * 获取余票 ,通过服务器缓存设置实现
? */
?public static JSONObject getRamainTicekets(Map args){
??
??JSONObject JsonStr = null;
??String cachekey = args.get("BeginDrvDate").toString()+"@"+
????args.get("EndDrvDate").toString()+"@"+
????args.get("CarryStaId").toString()+"@"+
????args.get("StopName").toString();
??try {
???if(singletonManager==null)
????singletonManager = CacheManager.create();
???synchronized (singletonManager) {
????Cache memoryOnlyCache = singletonManager.getCache(cachekey);
?????? if(memoryOnlyCache==null)
?????? {
?????? ?System.out.println("? no cache---0");
?????? ?memoryOnlyCache = new Cache(cachekey,50000,false,50,40);?
?????? ?JsonStr = visiteReticketService(args,JsonStr);
?????? ? synchronized (memoryOnlyCache) {
???????? ?if(!singletonManager.cacheExists(cachekey))
???????? ?{
????????? ?singletonManager.addCache(memoryOnlyCache);
???????? ?}
???????? ?memoryOnlyCache = singletonManager.getCache(cachekey);
????????? Element el = new Element("reticket_el",JsonStr.toString());
???????memoryOnlyCache.put(el);
??????}
?????? }
?????? else if(singletonManager.getCache(cachekey).get("reticket_el")==null)
?????? {
?????? ?System.out.println("? no cache---1");
?????? ?JsonStr = visiteReticketService(args,JsonStr);
?????? ? Element el = new Element("reticket_el",JsonStr.toString());
?????memoryOnlyCache.put(el);
?????? }
?????else{
??????
??????System.out.println(" has? cache---");
???????return ?JSONObject.fromObject(singletonManager.getCache(cachekey).get("reticket_el").getValue());
?????}
????
???}
???
??} catch (Exception e) {
???// TODO Auto-generated catch block
??e.printStackTrace();
??}
??return JsonStr;
?}
?/**
? * 访问余票接口
? * @param args
? * @param JsonStr
? * @return
? * @throws AxisFault
? */
?private static JSONObject visiteReticketService(Map args,JSONObject JsonStr)
???throws AxisFault {
//??Properties prop = getProperties();
??OMFactory fac = OMAbstractFactory.getOMFactory();
??org.apache.axiom.om.OMNamespace omNs = fac.createOMNamespace(prop.getProperty("reticket.nameSpace"),"example1");
??OMElement method = fac.createOMElement(prop.getProperty("reticket.method"),omNs);
??JSONObject json = JSONObject.fromObject(args);
??OMElement value = fac.createOMElement("JsonObject",omNs);
??value.setText(json.toString());
??method.addChild(value);
??
??Options options = new Options();
??EndpointReference url = new EndpointReference(prop.getProperty("reticket.url").toString());
??options.setTo(url);
??options.setAction("urn:"+prop.getProperty("reticket.method"));

??ServiceClient sender = new ServiceClient(); ??sender.setOptions(options); ??OMElement result = sender.sendReceive(method); ??Iterator iter = result.getChildren(); ??while(iter.hasNext()) ??{ ???OMElement element = (OMElement) iter.next(); ???String returnStr = element.getText(); ???JsonStr?= JSONObject.fromObject(returnStr); ??? ??} ??return JsonStr; ?} /** ?* 获取配置文件,在初始化预读信息 ?* @return ?*/ ?public static Properties getProperties(){ ??Properties props = new Properties(); ??????? try { ???????? InputStream in = RamainTicekets.class.getResourceAsStream("/com/cattsoft/itdc/roadTransport/webservice/webservice.properties"); ???????? props.load(in); ?????? return props; ??????? } catch (Exception e) { ???????? e.printStackTrace(); ???????? return null; ??????? } ?} }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读