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