Wednesday, April 30, 2008

Give priorities to your tasks

Making lists is a great way to schedule the time and organize your life. A lot of people and even me make the tasks list every day and I'm sure that almost all of them poll some tasks to the top of this list giving them higher priority.

GridGain does it out-of-the-box and schedule your tasks automatically. This feature is very useful on distributed environment where multiple tasks and jobs are being executed simultaneously. Definitely one need to say what jobs should be run first and which ones could wait. Another words - prioritize them.

Here is a simple example (as you probably noticed we do our best to make it simple for developers and users) that shows how all those tasks and jobs could be prioritized.

Add following to the node configuration file:

config.xml

<property name="collisionSpi">
<bean class=
"org.gridgain.grid.spi.collision.priorityqueue.GridPriorityQueueCollisionSpi">
<property name="priorityAttributeKey" value="GRID_TASK_PRIORITY"/>
</bean>
</property>

This will set up appropriate Collision SPI and says it to look for the given priority key.

Now let's get back to tasks and jobs and give them priority.

MyGridUsualTask.java

public class MyGridUsualTask extends GridTaskSplitAdapter<Object, Object> {
public static final int SPLIT_COUNT = 20;

@GridTaskSessionResource
private GridTaskSession taskSes = null;

@Override
protected Collection<? extends GridJob> split(int gridSize, Object arg)
throws GridException {
...
// Set low task priority (note that attribute name is used by the SPI
// and should not be changed).
taskSes.setAttribute("GRID_TASK_PRIORITY", 5);

Collection<GridJob> jobs = new ArrayList<GridJob>(SPLIT_COUNT);

for (int i = 1; i <= SPLIT_COUNT; i++) {
jobs.add(new GridJobAdapter<Integer>(i) {
...
});
}
...
}
}

and

MyGridUrgentTask.java

public class MyGridUrgentTask extends GridTaskSplitAdapter<Object, Object> {
public static final int SPLIT_COUNT = 5;

@GridTaskSessionResource
private GridTaskSession taskSes = null;

@Override
protected Collection<? extends GridJob> split(int gridSize, Object arg)
throws GridException {
...
// Set high task priority (note that attribute name is used by the SPI
// and should not be changed).
taskSes.setAttribute("GRID_TASK_PRIORITY", 10);

Collection<GridJob> jobs = new ArrayList<GridJob>(SPLIT_COUNT);

for (int i = 1; i <= SPLIT_COUNT; i++) {
jobs.add(new GridJobAdapter<Integer>(i) {
...
});
}
...
}
}

0 comments: