Advertisements

Monday, February 4, 2013

Enabling Coherence with example

Que tal,

Veamos como habilitar el nodo de Coherence en el cual viene integrado con Weblogic, en esta ocasión usare la versión 11.1.1.5 del producto.

Primero nos dirigirnos al siguiente archivo de coherence en la siguiente ruta:

cd /u01/orasoa/middleware/coherence_3.X/bin
Bien, ahora modificamos el archivo "coherence.sh" usando el comando:
vi coherence.sh

Dentro del archivo buscamos la linea que dice "STORAGE_ENABLED=false"



Hay que modificar el valor "false" por el valor "true".

Nota: Después de actualizar el archivo hay que reiniciar Weblogic.

Vamos a ver un ejemplo en Java de como usar Coherence.

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

import com.eddosoa.coherence.persistence.DataVO;
import com.eddosoa.coherence.persistence.HeaderVO;
import com.eddosoa.coherence.utils.CoherenceUtil;

import java.util.ArrayList;
import java.util.List;

public class CoherenceSample {  
    /**
     * Atributo para ser utilizado por patron de diseño de la instancia
     */
    private static CoherenceEJBBean instance = null;
    
    /**
     *  CONSTANTE con el nomre de la cache a utilizar.
     */
    private static final String CACHE_NAME = "TestCache";  
    
    /**
     * 
     */
    private static NamedCache cache;
  
    /**
     * Bloque que inicializa por unica vez la cache almacenada en Coherence
     */
    static {
        try {
            if (cache == null) {
                CacheFactory.ensureCluster();
                cache = CacheFactory.getCache( CACHE_NAME );
            }
        } catch(Exception ex) {
          System.out.println("Error --> " + ex.getMessage());
        }
    }

      
    /**
    * Metodo que almacena un elemento en Coherence.
    * @param data
    * @return
    */
    public void putCacheValue(DataVO data) {
 
  String key = CoherenceUtil.createKey(data.getHeader()); //Metodo createKey: genera una cadena para crear la llave (usar cualquier nombre)
  cache.put(key , data.getDataInfo());
    }
    
    /**
    *   Metodo que retorna un objeto dado otro objeto para la busqueda
    *   @param data
    *   @return
    */
    public Object getCacheValue(HeaderVO header) {
        ResponseVO result = new ResponseVO();
        if(header != null) {
              if(CoherenceUtil.checkHeaderValues(header)) {                
                String key = CoherenceUtil.createKey(header);
                if(cache.containsKey(key)) {
                  result.setResponse(cache.get(key));
                }                

        }
        return result;
    }      
    
    
    /**
       * Metodo que elimina un objeto almacenado en la cache de coherence dado un header
       * @param header
       * @return
       */
    public void deleteSegment(HeaderVO header){
      ResponseVO result = new ResponseVO();
      if(header != null) {
            if(CoherenceUtil.checkHeaderValues(header)) {
              String key = CoherenceUtil.createKey(header);
              cache.remove(key);
          }
      }
    }
          
  }

Estos son ejemplos de como construir los DTO para coherence en el cual implementamos la interface para los POF(Portable Object Format.
package com.eddosoa.coherence.persistence;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofWriter;
import com.tangosol.io.pof.PortableObject;

import java.io.IOException;
import java.io.Serializable;

public class HeaderVO implements Serializable, PortableObject{
    
  private String docKindOf;
  private String currentPart;
  private String totalParts;
  private String store;
  private String date;
    
    

    public void readExternal(PofReader reader) throws IOException {
      docKindOf   = reader.readString(0);
      currentPart = reader.readString(1);
      totalParts  = reader.readString(2);
      store       = reader.readString(3);
      date        = reader.readString(4);
    }

    public void writeExternal(PofWriter writer) throws IOException {
      writer.writeString(0, docKindOf);
      writer.writeString(1, currentPart);
      writer.writeString(2, totalParts);
      writer.writeString(3, store);
      writer.writeString(4, date);
    }


    public void setDocKindOf(String docKindOf) {
        this.docKindOf = docKindOf;
    }

    public String getDocKindOf() {
        return docKindOf;
    }

    public void setCurrentPart(String currentPart) {
        this.currentPart = currentPart;
    }

    public String getCurrentPart() {
        return currentPart;
    }

    public void setTotalParts(String totalParts) {
        this.totalParts = totalParts;
    }

    public String getTotalParts() {
        return totalParts;
    }

    public void setStore(String store) {
        this.store = store;
    }

    public String getStore() {
        return store;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDate() {
        return date;
    }
}

package com.eddosoa.coherence.persistence;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofWriter;
import com.tangosol.io.pof.PortableObject;

import com.eddosoa.coherence.utils.EnumType;

import java.io.IOException;
import java.io.Serializable;

public class DataVO implements Serializable, PortableObject {
          
    private HeaderVO header;
    private String dataInfo;
    private EnumType type;

    public void readExternal(PofReader reader) throws IOException {
      header   = (HeaderVO)reader.readObject(0);
      dataInfo = reader.readString(1);
      type     = (EnumType)reader.readObject(2);
    }

    public void writeExternal(PofWriter writer) throws IOException {
      writer.writeObject(0, header);
      writer.writeString(1, dataInfo);
      writer.writeObject(2, type);
    }


    public void setHeader(HeaderVO header) {
        this.header = header;
    }

    public HeaderVO getHeader() {
        return header;
    }

    public void setDataInfo(String dataInfo) {
        this.dataInfo = dataInfo;
    }

    public String getDataInfo() {
        return dataInfo;
    }

    public void setType(EnumType type) {
        this.type = type;
    }

    public EnumType getType() {
        return type;
    }
}


gl hf!


No comments:

Post a Comment