Tuesday, April 7, 2009

R&D Source Code


Sample text
package com.dfn.jbcg.session;


import com.dfn.jbcg.cache.CacheFactory;

import javax.ejb.Stateless;

/**
 * com.dfn.mtr.session.MTRCacheFacadeBean
 */
@Stateless(name = "CacheFacade")
public class CacheFacadeBean implements CacheFacade {

    public void addToCache(String fullyQualifiedName, Object key, Object value) {
        CacheFactory cacheFactory = new CacheFactory();
        cacheFactory.addToCache(fullyQualifiedName, key, value);
    }

    public Object loadFromCache(String fullyQualifiedName, String key) {
        CacheFactory cacheFactory = new CacheFactory();
        return cacheFactory.loadFromCache(fullyQualifiedName, key);
    }

    public void removeFromCache(String fullyQualifiedName){
        CacheFactory cacheFactory = new CacheFactory();
        cacheFactory.removeFromCache(fullyQualifiedName);
    }
}


-----------------------------------------------------------------

package com.dfn.jbcg.session;

import javax.ejb.Remote;

/**
 * com.dfn.mtr.session.MTRCacheFacade
 */
@Remote
public interface CacheFacade {
    public void addToCache(String fullyQualifiedName, Object key, Object value);

    public Object loadFromCache(String fullyQualifiedName, String key);

    public void removeFromCache(String fullyQualifiedName);
}


-----------------------------------------------------------------

package com.dfn.jbcg.cache;

import org.apache.log4j.Logger;
import org.jboss.cache.*;
import org.jboss.cache.config.ConfigurationException;

/**
 * com.dfn.mtr.cache.MTRCacheFactory
 */
public class CacheFactory {

    Logger logger = Logger.getLogger("CacheFactory.class");

    private static Cache cache;
    private String configFileClassPath = "META-INF";
    private String configFileName = "cache-configuration.xml";    

    private Cache getCache() {
        if (cache == null) {
            try {
                org.jboss.cache.CacheFactory factory = new DefaultCacheFactory();
                cache = factory.createCache(configFileClassPath + "/" + configFileName);

                MBeanServer mBeanServer = MBeanServerLocator.locate();
                ObjectName on = new ObjectName("jboss.cache:service=MTRCache");

                JmxRegistrationManager jmxManager = new JmxRegistrationManager(mBeanServer, cache, on);
                jmxManager.registerAllMBeans();

                cache.start();
            }
            catch (ConfigurationException e) {
                logger.error(e.getMessage());
            }
            catch (MalformedObjectNameException e) {
                logger.error(e.getMessage());
            }
        }
        return cache;
    }

    public void addToCache(String fullyQualifiedName, Object key, Object value) {
        Node rootNode = getCache().getRoot();
        Fqn parentNode = Fqn.fromString(fullyQualifiedName);

        Node childNode = rootNode.addChild(parentNode);
        childNode.put(key, value);
        logger.info(fullyQualifiedName + " added to the cache.");
    }

    public Object loadFromCache(String fullyQualifiedName, String key) {
        Node rooteNode = getCache().getRoot();

        Fqn fqn = Fqn.fromString(fullyQualifiedName);
        Node nde = rooteNode.getChild(fqn);
        if (nde==null || nde.equals("")){
            return null;
        }
        return nde.get(key);
    }

    public Object removeFromCache(String fullyQualifiedName) {
        Node rooteNode = getCache().getRoot();

        Fqn fqn = Fqn.fromString(fullyQualifiedName);
        return rooteNode.removeChild(fqn);
    }
}

-----------------------------------------------------------------

package com.dfn.jbcg.cache;

import java.io.Serializable;

/**
 * com.dfn.mtr.cache.CacheMockObject
 */
public class CacheMockObject implements Serializable{
    private int objectId;
    private String objectName;

    public CacheMockObject(int objectId, String objectName){
        this.objectId = objectId;
        this.objectName = objectName;
    }

    public int getObjectId() {
        return objectId;
    }

    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public String toString(){
        return "Object Id = " + this.objectId + "\n" +
        "Object Name = " + this.objectName;
    }
}

Labels: ,

R&D Project Surcture

Labels:

Online Resource

Monday, April 6, 2009

Welcome to JBoss Cache

What is JBoss Cache?

JBoss Cache is a tree-structured, clustered, transactional cache. It can be used in a standalone, non-clustered environment, to cache frequently accessed data in memory thereby removing data retrieval or calculation bottlenecks while providing "enterprise" features such as JTA compatibility, eviction and persistence.

JBoss Cache is also a clustered cache, and can be used in a cluster to replicate state providing a high degree of failover. A variety of replication modes are supported, including invalidation and buddy replication, and network communications can either be synchronous or asynchronous.

When used in a clustered mode, the cache is an effective mechanism of building high availability, fault tolerance and even load balancing into custom applications and frameworks. For example, the JBoss Application Server and Red Hat's Enterprise Application Platform make extensive use of JBoss Cache to cluster services such as HTTP and EJB sessions, as well as providing a distributed entity cache for JPA.

JBoss Cache can - and often is - used outside of JBoss AS, in other Java EE environments such as Spring, Tomcat, Glassfish, BEA WebLogic, IBM WebSphere, and even in standalone Java programs thanks to its minimal dependency set. 

Labels: ,