<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    當柳上原的風吹向天際的時候...

    真正的快樂來源于創造

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
    注:以下Cache未加時效處理,是比較簡陋的Cache方案。

    AbstractCacheMap基類,用來定義和限制子類的操作:
    package cachemap.base;

    /**
     * Parent class of InnerCacheMap & OutterCacheMap
     * 
     * 
    @author heyang
     * @time Sep 23, 2011,4:00:45 PM
     
    */
    public abstract class AbstractCacheMap {
        
    // Read-Write lock,for protecting the cacheMap in the Multi-thread environment
        private ReadWriteLock lock;
        
        
    /**
         * Contructor
         
    */
        
    public AbstractCacheMap() {
            lock 
    = new ReadWriteLock();
        }
        
        
    /**
         * Put key-value pair into cacheMap
         * It can be called by any class
         * 
    @param key
         * 
    @param value
         * 
    @throws Exception
         
    */
        
    public void writeData(String key,byte[] value) throws Exception{
            
    try {
                lock.writeLock();
                set(key,value);
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Put key-value pair into cacheMap,force child-class to implement.
         * It only can be called by child-class
         * 
    @param key
         * 
    @param value
         
    */
        
    protected abstract void set(String key,byte[] value) throws Exception;

        
    /**
         * Get value by it's key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public byte[] readData(String key) throws Exception{
            
    try {
                lock.readLock();
                
    return get(key);
            } 
    finally {
                lock.readUnlock();
            }
        }
        
        
    /**
         * Get value by it's key,force child-class to implement.
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract byte[] get(String key) throws Exception;
        
        
    /**
         * Judge the existence of a key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public boolean containsKey(String key) throws Exception{
            
    try {
                lock.readLock();
                
    return contains(key);
            } 
    finally {
                lock.readUnlock();
            }
        }
        
        
    /**
         * Judge the existence of a key,force child-class to implement.
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract boolean contains(String key) throws Exception;
        
        
    /**
         * Remove a key-value pair from cacheMap by it's key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @throws Exception
         
    */
        
    public void removeData(String key) throws Exception{
            
    try {
                lock.writeLock();
                remove(key);
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Remove a key-value pair from cacheMap by it's key
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract void remove(String key) throws Exception;
        
        
    /**
         * Remove all data in the cacheMap
         * It can be called by any class
         * 
         * 
    @throws Exception
         
    */
        
    public void removeAllData() throws Exception{
            
    try {
                lock.writeLock();
                removeAll();
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Remove all data in the cacheMap
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract void removeAll() throws Exception;
    }
    與AbstractCacheMap類配合使用,用來防止讀寫線程沖突和線程擁擠的讀寫鎖類:
    package cachemap.base;


    /**
     * Parent class of InnerCacheMap & OutterCacheMap
     * 
     * 
    @author heyang
     * @time Sep 23, 2011,4:00:45 PM
     
    */
    public abstract class AbstractCacheMap {
        
    // Read-Write lock,for protecting the cacheMap in the Multi-thread environment
        private ReadWriteLock lock;
        
        
    /**
         * Contructor
         
    */
        
    public AbstractCacheMap() {
            lock 
    = new ReadWriteLock();
        }
        
        
    /**
         * Put key-value pair into cacheMap
         * It can be called by any class
         * 
    @param key
         * 
    @param value
         * 
    @throws Exception
         
    */
        
    public void writeData(String key,byte[] value) throws Exception{
            
    try {
                lock.writeLock();
                set(key,value);
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Put key-value pair into cacheMap,force child-class to implement.
         * It only can be called by child-class
         * 
    @param key
         * 
    @param value
         
    */
        
    protected abstract void set(String key,byte[] value) throws Exception;

        
    /**
         * Get value by it's key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public byte[] readData(String key) throws Exception{
            
    try {
                lock.readLock();
                
    return get(key);
            } 
    finally {
                lock.readUnlock();
            }
        }
        
        
    /**
         * Get value by it's key,force child-class to implement.
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract byte[] get(String key) throws Exception;
        
        
    /**
         * Judge the existence of a key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public boolean containsKey(String key) throws Exception{
            
    try {
                lock.readLock();
                
    return contains(key);
            } 
    finally {
                lock.readUnlock();
            }
        }
        
        
    /**
         * Judge the existence of a key,force child-class to implement.
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract boolean contains(String key) throws Exception;
        
        
    /**
         * Remove a key-value pair from cacheMap by it's key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @throws Exception
         
    */
        
    public void removeData(String key) throws Exception{
            
    try {
                lock.writeLock();
                remove(key);
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Remove a key-value pair from cacheMap by it's key
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract void remove(String key) throws Exception;
        
        
    /**
         * Remove all data in the cacheMap
         * It can be called by any class
         * 
         * 
    @throws Exception
         
    */
        
    public void removeAllData() throws Exception{
            
    try {
                lock.writeLock();
                removeAll();
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Remove all data in the cacheMap
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract void removeAll() throws Exception;
    }

    用來往CacheMap中異步添加值的CacheMapSetter類:
    package cachemap.setter;

    import cachemap.base.AbstractCacheMap;


    /**
     * CacheMapSetter
     * It's use is to set a key-value pair into cacheMap
     * 
     * 
    @author heyang
     * @time Sep 26, 2011,10:11:36 AM
     
    */
    public final class CacheMapSetter implements Runnable{
        
    // The reference to the cacheMap
        private AbstractCacheMap cacheMap;
        
    private String key;
        
    private byte[] value;
        
        
    /**
         * Constuctor
         * 
    @param cacheMap
         * 
    @param key
         * 
    @param value
         
    */
        
    public CacheMapSetter(AbstractCacheMap cacheMap,String key,byte[] value){
            
    this.cacheMap=cacheMap;
            
    this.key=key;
            
    this.value=value;
            
            
    new Thread(this).start();
        }

        @Override
        
    public void run(){
            
    try{
                cacheMap.writeData(key, value);        
            }
            
    catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    AbstractCacheMap的實際子類InnerCacheMap,存儲空間使用的是內置的HashMap:
    package cachemap;

    import java.util.HashMap;
    import java.util.Map;

    import cachemap.base.AbstractCacheMap;

    /**
     * CacheMap used local HashMap
     * 
     * 
    @author heyang
     * @time Sep 23, 2011,3:48:17 PM
     
    */
    public class InnerCacheMap extends AbstractCacheMap{
        
    // essential storage
        private Map<String,byte[]> map;
        
        
    /**
         * Contructor
         
    */
        
    public InnerCacheMap(){
            
    super();
            map
    =new HashMap<String,byte[]>();
        }

        @Override
        
    protected byte[] get(String key) throws Exception {
            
    return map.get(key);
        }

        @Override
        
    protected void set(String key, byte[] value) throws Exception {
            map.put(key, value);
        }

        @Override
        
    protected boolean contains(String key) throws Exception {
            
    return map.containsKey(key);
        }

        @Override
        
    protected void remove(String key) throws Exception {
            map.remove(key);
        }

        @Override
        
    protected void removeAll() throws Exception {
            map.clear();
        }
    }
    AbstractCacheMap的子類WxsCacheMap,使用的是外部的WebSphere Extreme Scale作為存儲空間:
    package cachemap;

    import cachemap.base.AbstractCacheMap;

    import com.devwebsphere.wxsutils.WXSMap;
    import com.devwebsphere.wxsutils.WXSUtils;

    /**
     * CacheMap powered by WebSphere eXtreme Scale
     * 
     * 
    @author heyang
     * @time Sep 23, 2011,3:47:54 PM
     
    */
    public class WxsCacheMap extends AbstractCacheMap{
        
    // essential storage
        private WXSMap<String, byte[]> wxsMap;
        
        
    /**
         * Contructor
         
    */
        
    public WxsCacheMap(String host,String grid,String businessObjectName){
            
    super();
            
            
    try{
                WXSUtils wxsUtils 
    = WxsConnection.getWxsConnection(host,grid);
                wxsMap
    =getMap(wxsUtils,businessObjectName);
            }
    catch(Exception ex){
                ex.printStackTrace();
            }
        }
        
        
    /**
         * Get map from WXSUtils
         * 
    @param wxsUtils
         * 
    @param strBusinessObject
         * 
    @return
         
    */
        
    private WXSMap<String, byte[]> getMap(WXSUtils wxsUtils, String strBusinessObject) {
            
    if (strBusinessObject.equalsIgnoreCase("CUSTOMER")){
                
    return wxsUtils.getCache("BO_CUSTOMER");
            }
    else if (strBusinessObject.equalsIgnoreCase("VEHICLE")){
                
    return wxsUtils.getCache("BO_VEHICLE");
            }
    else if (strBusinessObject.equalsIgnoreCase("NAVIGATION")){
                
    return  wxsUtils.getCache("NAV_NAVIGATION");
            }
    else{
                
    return null;
            }
        }
        
        
        @Override
        
    protected byte[] get(String key) throws Exception {
            
    return wxsMap.get(key);
        }

        @Override
        
    protected void set(String key, byte[] value) throws Exception {
            wxsMap.put(key, value);
        }

        @Override
        
    protected boolean contains(String key) throws Exception {
            
    return wxsMap.contains(key);
        }

        @Override
        
    protected void remove(String key) throws Exception {
            wxsMap.remove(key);
        }

        @Override
        
    protected void removeAll() throws Exception {
            wxsMap.clear();
        }
    }
    與WxsCacheMap配合使用的WxsConnection類:
    package cachemap;

    import com.devwebsphere.wxsutils.WXSUtils;
    import com.ibm.websphere.objectgrid.ObjectGrid;
    import com.ibm.websphere.objectgrid.ObjectGridRuntimeException;
    import com.ibm.websphere.objectgrid.TargetNotAvailableException;

    public class WxsConnection {
        
    static WXSUtils utils;
        
    static ObjectGrid grid;

        
    /**
         * 
    @param strCatalogServerEndPoint: Catalog Server End Point example: localhost:2809
         * 
    @param strGrid: Grid name
         * 
    @return
         
    */
        
    public static synchronized WXSUtils getWxsConnection(String strCatalogServerEndPoint,String strGrid) {
            
    if (utils == null) {
                
                grid 
    = WXSUtils.connectClient(strCatalogServerEndPoint,strGrid);  
                utils 
    = new WXSUtils(grid);
            }
            
    return utils;
        }

        
    public static void handleException(ObjectGridRuntimeException e) {
            Throwable ne 
    = e.getCause();
            
    if (ne instanceof TargetNotAvailableException) {
                
    // bad target (maybe ip changed)
                WXSUtils oldUtils;
                
    synchronized (WxsConnection.class) {
                    oldUtils 
    = utils;
                    utils 
    = null;
                }
                oldUtils.getObjectGrid().destroy();
            }
        }
    }

    用來從外界得到CacheMap的CacheFactory類:
    package cachemap;

    import cachemap.base.AbstractCacheMap;

    /**
     * CacheMapFacory
     * It' used to get the actual cacheMap
     * 
     * 
    @author heyang
     * @time Sep 26, 2011,10:41:39 AM
     
    */
    public final class CacheMapFacory{
        
    /**
         * getInnerCacheMap
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public static AbstractCacheMap getInnerCacheMap() throws Exception{
            
    return new InnerCacheMap();
        }
        
        
    /**
         * getWxsCacheMap
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public static AbstractCacheMap getWxsCacheMap() throws Exception{
            
    return new WxsCacheMap("host","grid","VEHICLE");
        }
    }

    JavaCompute節點中使用Cache的Java代碼:
    import cachemap.CacheMapFacory;
    import cachemap.base.AbstractCacheMap;
    import cachemap.setter.CacheMapSetter;

    import com.ibm.broker.javacompute.MbJavaComputeNode;
    import com.ibm.broker.plugin.MbElement;
    import com.ibm.broker.plugin.MbException;
    import com.ibm.broker.plugin.MbMessage;
    import com.ibm.broker.plugin.MbMessageAssembly;
    import com.ibm.broker.plugin.MbOutputTerminal;

    /**
     * subflow_JavaCompute class
     * 
     * 
    @author heyang
     * @time Sep 26, 2011,10:14:34 AM
     
    */
    public class subflow_JavaCompute extends MbJavaComputeNode {
        
    private static AbstractCacheMap cacheMap;
        
    static{
            
    try {
                cacheMap
    =CacheMapFacory.getInnerCacheMap();
            } 
    catch (Exception e) {
                e.printStackTrace();
            }
        }

        
    public void evaluate(MbMessageAssembly inAssembly) throws MbException {
            
    /****************************
             * 1.out&alt
             **************************
    */
            MbOutputTerminal out 
    = getOutputTerminal("out");
            
    //MbOutputTerminal alt = getOutputTerminal("alternate");
            
            
    /****************************
             * 2.inMsg& outMsg
             **************************
    */
            MbMessage inMsg 
    = inAssembly.getMessage();
            MbMessage outMsg 
    = new MbMessage();
            
            
    /****************************
             * 3.input&output environment
             **************************
    */
            MbMessage inputEnv
    =inAssembly.getLocalEnvironment();
            MbMessage outputEnv
    =new MbMessage(inAssembly.getLocalEnvironment());
            
            
    /****************************
             * 4.outAssembly
             **************************
    */
            MbMessageAssembly outAssembly
    =new MbMessageAssembly(inAssembly,outputEnv,inAssembly.getExceptionList(),outMsg);
            
            
    try{
                
    // Copy Headers
                copyMessageHeaders(inMsg,outMsg);
                
                
    // Get key
                String key=getKey(inputEnv);
                
                
    // Get operation
                String operation=getOperation(inputEnv);
                
                
                
    if("READ".equalsIgnoreCase(operation)){
                    
    if(cacheMap.containsKey(key)){
                        
    byte[] value=cacheMap.readData(key);
                        
                        
    // TODO:
                        MbElement elementOutputVariables = outputEnv.getRootElement().getFirstElementByPath("Variables");
                        MbElement elementValue 
    = elementOutputVariables.getFirstElementByPath("Value");
                        
    if(elementValue==null){
                            elementValue 
    = elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    "Value",    "");
                        }
                        
                        elementValue.setValue(value);
                        elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    
    "Status",    "SUCCESS");
                        elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    
    "Message",    "CACHE Record Found");    
                    }
    else{
                        
    // TODO:
                        MbElement elementOutputVariables = outputEnv.getRootElement().getFirstElementByPath("Variables");
                        elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    
    "Status",    "FAILURE");
                        elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, 
    "Message",    "No Record Found");    
                    }
                }
    else if("WRITE".equalsIgnoreCase(operation)){
                    
    if(cacheMap.containsKey(key)){
                        cacheMap.removeData(key);
                    }    
                    
                    
    byte[] value=getValue(outputEnv);
                    
    new CacheMapSetter(cacheMap,key, value);
                    
                    
    // TODO:
                    MbElement elementOutputVariables = outputEnv.getRootElement().getFirstElementByPath("Variables");
                    MbElement elementValue 
    = elementOutputVariables.getFirstElementByPath("Value");
                    
    if(elementValue==null){
                        elementValue 
    = elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    "Value",    "");
                    }
                    elementValue.detach();
                    elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    
    "Status",    "SUCCESS");
                    elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, 
    "Message",    "Record Added to Cache");
                    
                }
    else if("REMOVEALL".equalsIgnoreCase(operation)){
                    cacheMap.removeAllData();
                    
                    
    // TODO:
                    MbElement elementOutputVariables = outputEnv.getRootElement().getFirstElementByPath("Variables");
                    MbElement elementValue 
    = elementOutputVariables.getFirstElementByPath("Value");
                    
    if(elementValue==null){
                        elementValue 
    = elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    "Value",    "");
                    }
                    elementValue.detach();
                    elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,    
    "Status",    "SUCCESS");
                    elementOutputVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, 
    "Message",    "Rmoved all data from Cache");
                }
    else{
                    
    throw new Exception("Unknown Operation:"+operation);
                }
                
                
    // Send Back
                out.propagate(outAssembly);
            }
            
    catch(Exception ex){
                ex.printStackTrace();
            }
        }
        
        
    //private 
        
        
    /**
         * get operation
         * get operation from input environment
         * 
         * 
    @param inputEnv
         * 
    @return
         * 
    @throws Exception
         
    */
        
    private String getOperation(MbMessage inputEnv) throws Exception{
            MbElement elementInputEnvironment 
    = inputEnv.getRootElement();
            MbElement elementOperation 
    = elementInputEnvironment.getFirstElementByPath("Variables/Operation");
            String strOperation 
    = elementOperation.getValueAsString();
            
            
    return strOperation;
        }
        
        
    /**
         * getKey
         * get key from input environment
         * 
         * 
    @param inputEnv
         * 
    @return
         * 
    @throws Exception
         
    */
        
    private String getKey(MbMessage inputEnv) throws Exception{
            MbElement elementInputEnvironment 
    = inputEnv.getRootElement();
            MbElement elementKey 
    = elementInputEnvironment.getFirstElementByPath("Variables/BOKey");
            String strKey 
    = elementKey.getValueAsString();
            
            
    return strKey;
        }
        
        
    /**
         * getValue
         * get value from output environment
         * 
         * 
    @param outputEnv
         * 
    @return
         * 
    @throws Exception
         
    */
        
    private byte[] getValue(MbMessage outputEnv) throws Exception{
            MbElement elementOutputVariables 
    = outputEnv.getRootElement().getFirstElementByPath("Variables");
            MbElement elementValue 
    = elementOutputVariables.getFirstElementByPath("Value");
            
    byte[] byteInputObject = (byte[])elementValue.getValue();
            
            
    return byteInputObject;
        }

        
    /**
         * copyMessageHeaders
         * 
    @param inMessage
         * 
    @param outMessage
         * 
    @throws MbException
         
    */
        
    private void copyMessageHeaders(MbMessage inMessage, MbMessage outMessage) throws MbException {
                MbElement outRoot 
    = outMessage.getRootElement();

            
    // iterate though the headers starting with the first child of the root element
            MbElement header = inMessage.getRootElement().getFirstChild();
            
    while (header != null && header.getNextSibling() != null// stop before the last child of the body
            {
                
    // copy the header and add it to the out message
                outRoot.addAsLastChild(header.copy());
                
                
    // move along to next header
                header = header.getNextSibling();
            }        
        }
    }

    源碼下載:
    http://m.tkk7.com/Files/heyang/SimpleCacheInJavaComputeNode_01.rar
    posted on 2011-09-27 10:00 何楊 閱讀(987) 評論(0)  編輯  收藏 所屬分類: WMB
    主站蜘蛛池模板: 在线免费一区二区| 亚洲高清视频一视频二视频三| 免费在线观看一级片| 国产高清视频免费在线观看| 日本黄页网址在线看免费不卡| 亚洲成av人在线观看网站| 亚洲一线产区二线产区区| 亚洲一区无码中文字幕乱码| 亚洲人成网站在线观看播放动漫 | 777成影片免费观看| 无码人妻AV免费一区二区三区| a级午夜毛片免费一区二区| 西西人体免费视频| 久久精品免费电影| 免费国产黄网站在线观看| 麻豆高清免费国产一区| 97免费人妻无码视频| 成人免费毛片视频| 国产免费AV片无码永久免费| 亚洲国产精品尤物YW在线观看| 国产成人精品亚洲精品| 亚洲国产无套无码av电影| 亚洲AV成人片色在线观看高潮| 亚洲视频在线观看免费| 亚洲一卡2卡4卡5卡6卡在线99| 亚洲成a人片在线观看天堂无码| 又大又硬又粗又黄的视频免费看| 日韩成人毛片高清视频免费看| 中文字幕手机在线免费看电影| 玖玖在线免费视频| 免费看成人AA片无码视频羞羞网| 成人免费无遮挡无码黄漫视频| 免费乱码中文字幕网站| 国产日韩亚洲大尺度高清| 亚洲日韩在线视频| 亚洲国产精华液2020| 亚洲精品视频免费 | 亚洲AV日韩综合一区尤物| 免费国产黄网站在线看| 久久精品国产这里是免费| 免费在线观看的网站|