Task Sessions

Ignite provides an API to define a distributed session that exists for a particular task's execution; this distributed session is shared between the jobs and parent task.

The ComputeTaskSession interface defines a set of methods to perform the following distributed session tasks:

  • setAttribute: A job (or the parent task) can set a session attribute; all other jobs within this task and task itself can see the attribute. This method takes a key (possible job id) and a value.
  • getAttribute: Retrieves the session attribute value for a key.
  • waitForAttribute: A job can call this method by passing a key and an expected value. The call is a blocking call, if this attribute is already in session, returns immediately, otherwise waits for the attribute to be set.
  • getJobSiblings: Returns the grid jobs that are executing within the same task.
  • saveCheckpoint: Saves the intermediate state of a job or task to storage. This is similar to a JDBC save point. You can configure the storage by implementing the CheckpointSpi.
  • loadCheckpoint: Loads the saved checkpoint.

The ComputeJobContext  interface defines the context attached to every job. The job context belongs to a job and is not shared between the jobs and the parent task. It can give you the job id and option to set/get attributes local to the job.

The @TaskSessionResource annotation injects a ComputeTaskSession  resource into a job.

The @JobContextResource annotation injects a ComputeJobContext resource into a job.

You can find an example in the code bundle to use the ComputeTaskSession  and ComputeJobContext to synchronize job execution. The ClubExpenseSessionEnabledJob class gives you the implementation. The following code snippet explains the job synchronization:

session.setAttribute(jobContext.getJobId(), "SUMMATION");

for (ComputeJobSibling sibling : session.getJobSiblings()) {
try {
System.err.println(String.format("WAITING for %s to
complete SUMMATION", sibling.getJobId()));
session.waitForAttribute(sibling.getJobId(), "SUMMATION", 100);
System.err.println(String.format("DONE for %s to complete SUMMATION",
sibling.getJobId()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Each job sets the attribute value SUMMATION against its job ID as key, then it loops through the sibling jobs and checks whether they are done with the SUMMATION steps. The waitForAttribute times out after 100 milliseconds.

When we run the job, it prints the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.237.131