Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > Example Configuration And Setup > GridifyHelloWorldContextFactory.java
GridifyHelloWorldContextFactory.java
Added by architect, last edited by architect on Feb 08, 2007
Labels: 
(None)


/*
 * LICENSE AGREEMENT
 * 
 * GRIDGAIN 1.0 - GRID COMPUTING FOR JAVA.
 * COPYRIGHT (C) 2005-2007 GRIDGAIN SYSTEMS. ALL RIGHTS RESERVED.
 * 
 * THIS IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
 * MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC
 * LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER
 * VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER 
 * VERSION.
 * 
 * THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
 * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  SEE THE 
 * GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
 * 
 * YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
 * LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE 
 * SOFTWARE FOUNDATION, INC., 51 FRANKLIN ST, FIFTH FLOOR, BOSTON, MA  
 * 02110-1301 USA
 */


package org.gridgain.examples.helloworld.gridify.context;

import java.lang.management.*;
import java.lang.reflect.*;
import org.gridgain.grid.*;
import org.gridgain.grid.gridify.*;

/**
 * This context factory will grid-enable methods only if local node's heap
 * memory usage is below 80% of all heap memory available. It also sets
 * timeout of method execution on the grid to 2 seconds.
 *
 * @author 2005-2007 Copyright (C) GridGain Systems. All Rights Reserved.
 * @version 1.0.0-07022007
 */
public class GridifyHelloWorldContextFactory implements GridifyContextFactory {
    /**
     * Creates custom {@link GridifyContext} context.
     * 
     * @see GridifyContextFactory#create(Gridify, Object, Method, Object[])
     */
    public GridifyContext create(Gridify gridify, Object obj, Method method, Object[] params) throws GridException {
        MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        
        // If heap memory for the local node is above 80% of maximum heap
        // memory available, return null which means that method will not
        // be grid-enabled.
        if (heap.getUsed() >= 0.8 * heap.getMax()) {
            return null;
        }
        
        // Initialize context with parameters from 'Gridify' annotation.
        GridifyContextAdapter ctx = new GridifyContextAdapter(gridify);
        
        // We can override any parameter passed in 'Gridify' annotation.
        // As example, let's override timeout parameter.
        ctx.setTimeout(2000); // 2 seconds.
        
        // Non-null context means that method will be grid-enabled.
        return ctx;
    }
}

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators