Dashboard > GridGain User Guide > Table Of Contents > Resources Injection
Resources Injection
Added by ghost, last edited by morpheus on Dec 29, 2008  (view change)
Labels: 
(None)


Overview

Resource is a GridGain internal object or user defined one (either by Spring or set up manually) that is relevant to the context like task session for the task and job, current node id or grid instance. There is fixed numbers of GridGain internal resources that can be injected into the job, task or SPIs. Prior to their initialization and availability all resources that have corresponding annotations will be injected into the task/job/SPI. Both, field and method based injection are supported. The following grid resources can be injected:

Executor Service Resource

Executor service is a Java java.util.concurrent.ExecutorService that GridGain uses to pool threads and execute jobs, process incoming messages and P2P requests.
It can be injected through the @GridExecutorServiceResource Javadoc annotation. It can be injected into the tasks, jobs and SPIs. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
...
    @GridExecutorServiceResource
    private ExecutorService execSvc;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private ExecutorService execSvc = null;
    ...
    @GridExecutorServiceResource
    public void setExecutor(GridExecutorService execSvc) {
        this.execSvc = execSvc;
    }
    ...
}

GridGain Home Path Resource

GridGain home path is a Java String that points to the installation directory (gives user value of environment variable named GRIDGAIN_HOME or Java system property with the same name). One should use @GridHomeResource Javadoc annotation to inject this resource. It can be injected into the tasks, jobs and SPIs. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridHomeResource
    private String home;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private String home = null;
    ...
    @GridHomeResource
    public void setGridGainHome(String home) {
        this.home = home;
    }
    ...
}

Grid Instance Resource

Grid instance is a Grid Javadoc for executing and deploying tasks, sending messages, etc... Prior to task/job execution resources will be injected into it and one could use grid to obtain local node or remote nodes for example. Use @GridInstanceResource Javadoc annotation to inject Grid resource. It can be injected into grid tasks and grid jobs. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridInstanceResource
    private Grid grid;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private Grid grid = null;
    ...
    @GridInstanceResource
    public void setGrid(Grid grid) {
        this.grid = grid;
    }
    ...
}

Job Id Resource

This resource is deprecated in favor of Job Context resource.

This resource can only be injected into Grid Jobs and not Grid Tasks. It injects unique job execution ID of type java.util.UUID into an instance of Grid Job. @GridJobIdResource Javadoc annotation can be used to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridJobIdResource

    private UUID jobId = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private UUID jobId = null;
    ...
    @GridJobIdResource
    public void setJobId(UUID jobId) {
        this.jobId = jobId;
    }
    ...
}

Job Context Resource

Context attached to every job executed on the grid. Note that unlike GridTaskSession Javadoc , which distributes all attributes to all jobs in the task including the task itself, job context attributes belong to a particular job only and do not get sent over network unless a job moves from one node to another. This resource can only be injected into Grid Jobs and not Grid Tasks or SPIs. @GridJobContextResource Javadoc annotation can be used to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridJobContextResource
    private GridJobContext jobCtx;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private GridJobContext jobCtx = null;
    ...
    @GridJobContextResource
    public void setJobContext(GridJobContext jobCtx) {
        this.jobCtx = jobCtx;
    }
    ...
}

Load Balancer Resource

Load balancer can be injected into grid tasks only. Specific implementation for grid load balancer is defined by GridLoadBalancingSpi Javadoc which is provided to grid via GridConfiguration Javadoc . @GridLoadBalancerResource Javadoc annotation can be used to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridLoadBalancerResource
    private GridLoadBalancer balancer = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private GridLoadBalancer balancer = null;
    ...
    @GridLoadBalancerResource
    public void setLoadBalancer(GridLoadBalancer balancer) {
        this.balancer = balancer;
    }
    ...
}

Local Node Id Resource

This resource injects local node ID of type java.util.UUID into an instance of Grid Job, task or SPI. Node that is is the ID of a node your code is executed on which is not necessarily ID of a node that started the execution. @GridLocalNodeIdResource Javadoc annotation can be used to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridLocalNodeIdResource
    private UUID locNodeId = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private UUID locId = null;
    ...
    @GridLocalNodeIdResource
    public void setLocNodeId(UUID locNodeId) {
        this.locNodeId = locNodeId;
    }
    ...
}

Logger Resource

Grid logger is provided to grid via GridConfiguration Javadoc . It can be injected into grid tasks, grid jobs, and SPI's. Use @GridLoggerResource Javadoc annotation to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridLoggerResource
    private GridLogger log = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private GridLogger log = null;
    ...
    @GridLoggerResource
    public void setLogger(GridLogger log) {
        this.log = log;
    }
    ...
}

Marshaller Resource

Injects marshaller to the task/job/SPI. Marshaller allows to serialize/deserialize data the same way as Grid does. Marshaller can be configured via GridConfiguration Javadoc . Use @GridMarshallerResource Javadoc annotation to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridMarshallerResource
    private GridMarshaller marshaller = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private GridMarshaller marshaller = null;
    ...
    @GridMarshallerResource
    public void setMarshaller(GridMarshaller marshaller) {
        this.marshaller = marshaller;
    }
    ...
}

MBean Server Resource

Injects MBean server into the task, job or SPI. MBean server is the same as Grid uses to register its own MBeans. Use @GridMBeanServerResource Javadoc annotation to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridMBeanServerResource
    private MBeanServer mbeanSrv = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private MBeanServer mbeanSrv = null;
    ...
    @GridMBeanServerResource
    public void setMBeanServer(MBeanServer srv) {
        this.srv = srv;
    }
    ...
}

Spring Application Context Resource

Injects Spring ApplicationContext resource. When GridGain starts using Spring configuration, the Application Context for Spring Configuration is either created or passed to the startup routine. It can be injected into grid tasks, grid jobs, and SPI's. @GridSpringApplicationContextResource Javadoc annotation is to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridSpringApplicationContextResource
    private ApplicationContext springCtx = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private ApplicationContext springCtx = null;
    ...
    @GridSpringApplicationContextResource
    public void setApplicationContext(MBeanServer springCtx) {
        this.springCtx = springCtx;
    }
    ...
}

Spring Bean Resource

Injects any custom resources declared in provided Spring ApplicationContext. It can be injected into grid tasks and grid jobs. Use it when you would like, for example, to inject something like JDBC connection pool into tasks or jobs - this way your connection pool will be instantiated only once per task and reused for all executions of this task. You can inject other resources into your user resource. User resources may contain fields or setters with other resource annotations . The resource will be picked up from provided Spring ApplicationContext by name value. Note, that injected spring bean must be declared in Spring ApplicationContext on every grid node where they get accessed. Use @GridSpringResource Javadoc annotation to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridSpringResource(resourceName = "bean-name")
    private transient MyUserBean rsrc = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {


    ...
    private transient MyUserBean rsrc = null;




    ...
    @GridSpringResource(resourceName = "bean-name")
    public void setMyUserBean(MyUserBean rsrc) {
        this.rsrc = rsrc;
    }
    ...
}

on Spring side it would look like following:

Spring MyUserBean description
<bean id="bean-name" class="my.foo.MyUserBean" singleton="true">
    ...
</bean>

Task Session Resource

Injects GridTaskSession into the job or task, but not in SPI. Task session gives a simple way to set task/job attributes. Use @GridTaskSessionResource Javadoc annotation to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridTaskSessionResource
    private GridTaskSession taskSes = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private GridTaskSession taskSes = null;
    ...
    @GridTaskSessionResource
    public void setTaskSession(GridTaskSession taskSes) {
        this.taskSes = taskSes;
    }
    ...
}

User Resource

@GridUserResource Javadoc injects any custom user resource into grid tasks or grid jobs. Use it when you would like, for example, to use something like JDBC connection pool from your tasks or jobs - this way your connection pool will be instantiated only once per task and reused for all executions of this task.

The resource will be created based on the resourceClass value. If resourceClass is not specified, then the field type or setter parameter type will be used to infer the class type of the resource. Set resourceClass to a specific value if the class of resource cannot be inferred from field or setter declaration (for example, if field is an interface).

User resource will be instantiated once on every node where task is deployed. Basically there will always be only one instance of resource on any grid node for any given task class. Every node will instantiate it's own copy of user resources used for every deployed task (see GridUserResourceOnDeployed Javadoc and GridUserResourceOnUndeployed Javadoc annotation for resource deployment and undeployment callbacks).

User resources are never serialized (they get instantiated) and should always be declared as transient.
The scope of user resource is a grid task. If you have 2 different grid tasks using the the same user resource class, then 2 instances of user resource will be created (per-node scope will be introduced in 2.2 release).

Use @GridUserResource Javadoc annotation to inject this resource. Here is how injection would typically happen:

Variable injection
public class MyGridJob implements GridJob {
    ...
    @GridUserResource
    private transient MyUserResource rsrc = null;
    ...
}

or

Method injection
public class MyGridJob implements GridJob {
    ...
    private transient MyUserResource rsrc = null;
    ...
    @GridUserResource
    public void setMyUserResource(MyUserResource rsrc) {
        this.rsrc = rsrc;
    }
    ...
}

where resource class can look like this:

MyUserResource class
public class MyUserResource {                   
    ... 
    // Inject logger (or any other resource).
    @GridLoggerResource                         
    private GridLogger log = null;              
                   
    // Inject grid instance (or any other resource).
    @GridInstanceResource
    private Grid grid = null;

    // Deployment callback.
    @GridUserResourceOnDeployed
    public void deploy() {
        // Some initialization logic.
        ...
    }

    // Undeployment callback.
    @GridUserResourceOnUndeployed
    public void undeploy() {
        // Some clean up logic.
        ...
    }
}

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