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, 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;
        }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值