Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > Resource injection > GridResourcesContext.java
GridResourcesContext.java
Added by link, last edited by link on Sep 11, 2008  (view change)
Labels: 
(None)


Context class for adding data in storage.
Context store all data in ArrayList by default. Only one context object will be initialized during task deployment.

Full Source Code

GridResourcesContext.java
package org.gridgain.examples.resources;

import org.gridgain.grid.resources.*;
import org.gridgain.grid.logger.*;
import org.gridgain.grid.*;
import java.util.*;

public class GridResourcesContext {
    /** */
    @GridLoggerResource
    private GridLogger log = null;

    /** */
    @GridInstanceResource
    private Grid grid = null;

    /** */
    @GridLocalNodeIdResource
    private UUID nodeId = null;

    /** */
    private List<String> storage = null;

    /** */
    private String storageName = null;

    /** */
    private boolean started = false;

    /**
     *
     */
    @SuppressWarnings("unused")
    @GridUserResourceOnDeployed
    private void deploy() {
        assert log != null;
        assert grid != null;
        assert nodeId != null;

        GridNode node = grid.getLocalNode();

        // Get storage parameters from node attributes.
        storageName = (String)node.getAttributes().get("storage.name");

        if (storageName == null) {
            storageName = "test-storage";
        }

        storage = new ArrayList<String>();

        started = true;

        if (log.isInfoEnabled() == true) {
            log.info("Starting context.");
        }
    }

    /**
     *
     */
    @SuppressWarnings("unused")
    @GridUserResourceOnUndeployed
    private void undeploy() {
        assert log != null;
        assert grid != null;
        assert nodeId != null;

        if (log.isInfoEnabled() == true) {
            log.info("Stopping context.");

            log.info("Storage data: " + storage);
        }
    }

    /**
     * Add data in storage.
     *
     * @param data Data to add.
     * @return Return <tt>true</tt> if data added.
     * @throws Exception If error occurred.
     */
    public boolean sendData(String data) throws Exception {
        if (started == false) {
            return false;
        }

        assert storage != null;

        StringBuilder builder = new StringBuilder();

        builder.append("Data [storageName=").append(storageName).
            append(", nodeId=").append(nodeId.toString()).
            append(", date=").append(new Date()).
            append(", data=").append(data).
            append(']');

        storage.add(builder.toString());

        return true;
    }
}

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