微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

escache访问webservice做缓存

最近项目中访问 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;         }  } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐