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
- GridGain Home Path Resource
- Grid Instance Resource
- Job Id Resource
- Job Context Resource
- Load Balancer Resource
- Local Node Id Resource
- Logger Resource
- Marshaller Resource (starting from 2.1.0)
- MBean Server Resource
- Spring Application Context Resource
- Spring Bean Resource (starting from 2.1.0)
- Task Session Resource
- User Resource
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 ![]()
public class MyGridJob implements GridJob { ... @GridExecutorServiceResource private ExecutorService execSvc; ... }
or
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 ![]()
public class MyGridJob implements GridJob { ... @GridHomeResource private String home; ... }
or
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 ![]()
![]()
public class MyGridJob implements GridJob { ... @GridInstanceResource private Grid grid; ... }
or
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 ![]()
public class MyGridJob implements GridJob { ... @GridJobIdResource private UUID jobId = null; ... }
or
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 ![]()
![]()
public class MyGridJob implements GridJob { ... @GridJobContextResource private GridJobContext jobCtx; ... }
or
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 ![]()
![]()
![]()
public class MyGridJob implements GridJob { ... @GridLoadBalancerResource private GridLoadBalancer balancer = null; ... }
or
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 ![]()
public class MyGridJob implements GridJob { ... @GridLocalNodeIdResource private UUID locNodeId = null; ... }
or
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 ![]()
![]()
public class MyGridJob implements GridJob { ... @GridLoggerResource private GridLogger log = null; ... }
or
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 ![]()
![]()
public class MyGridJob implements GridJob { ... @GridMarshallerResource private GridMarshaller marshaller = null; ... }
or
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 ![]()
public class MyGridJob implements GridJob { ... @GridMBeanServerResource private MBeanServer mbeanSrv = null; ... }
or
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 ![]()
public class MyGridJob implements GridJob { ... @GridSpringApplicationContextResource private ApplicationContext springCtx = null; ... }
or
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 ![]()
public class MyGridJob implements GridJob { ... @GridSpringResource(resourceName = "bean-name") private transient MyUserBean rsrc = null; ... }
or
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:
<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 ![]()
public class MyGridJob implements GridJob { ... @GridTaskSessionResource private GridTaskSession taskSes = null; ... }
or
public class MyGridJob implements GridJob { ... private GridTaskSession taskSes = null; ... @GridTaskSessionResource public void setTaskSession(GridTaskSession taskSes) { this.taskSes = taskSes; } ... }
User Resource
@GridUserResource ![]()
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 ![]()
![]()
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 ![]()
public class MyGridJob implements GridJob { ... @GridUserResource private transient MyUserResource rsrc = null; ... }
or
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:
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. ... } }
