GridGain™ 4.3.1e
Enterprise "Big Data" Edition

org.gridgain.grid.resources
Annotation Type GridUserResource


@Documented
@Retention(value=RUNTIME)
@Target(value={METHOD,FIELD})
public @interface GridUserResource

Annotates a field or a setter method for any custom resources injection. 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. The following grid resources can be injected:

Refer to corresponding resource documentation for more information.

Resource Class

The resource will be created based on the GridUserResource.resourceClass() value. If If resourceClass is not specified, then field type or setter parameter type will be used to infer the class type of the resource. Set GridUserResource.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).

Resource Life Cycle

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 task class. Every node will instantiate it's own copy of user resources used for every deployed task (see GridUserResourceOnDeployed and GridUserResourceOnUndeployed annotation for resource deployment and undeployment callbacks). For this reason resources should not be sent to remote nodes and should always be declared as transient just in case.

Note that an instance of user resource will be created for every deployed task. In case if you need a singleton resource instances on grid nodes (not per-task), you can use GridSpringApplicationContextResource for injecting per-VM singleton resources configured in Spring.

Examples

Here is how injection would typically happen:
 public class MyGridJob implements GridJob {
      ...
      @GridUserResource
      private transient MyUserResource rsrc;
      ...
  }
 
or
 public class MyGridJob implements GridJob {
     ...
     private transient MyUserResource rsrc;
     ...
     @GridUserResource
     public void setMyUserResource(MyUserResource rsrc) {
          this.rsrc = rsrc;
     }
     ...
 }
 
where resource class can look like this:
 public class MyUserResource {
     ...
     // Inject logger (or any other resource).
     @GridLoggerResource
     private GridLogger log;

     // Inject grid instance (or any other resource).
     @GridInstanceResource
     private Grid grid;

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

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

 

Optional Element Summary
 Class<?> resourceClass
          Optional resource class.
 String resourceName
          Optional resource name.
 

resourceClass

public abstract Class<?> resourceClass
Optional resource class. By default the type of the resource variable or setter parameter will be used.

Default:
java.lang.Void.class

resourceName

public abstract String resourceName
Optional resource name. By default the "dfltName" values will be used.

Default:
"dfltName"

GridGain™ 4.3.1e
Enterprise "Big Data" Edition

GridGain - In-Memory Big Data
Enterprise "Big Data" Edition, ver. 4.3.1e.10112012
2012 Copyright © GridGain Systems
Follow us:   Follow GridGain on Github Join GridGain User Group Follow GridGain on Twitter Follow GridGain on Vimeo