The Artima Developer Community
Sponsored Link

Make Room for JavaSpaces, Part V
Make Your Compute Server Robust and Scalable
by Susan Hupfer
First Published in JavaWorld, June 2000

<<  Page 7 of 8  >>

Advertisement

Worker Interacts with Spaces, Round-Robin Style

Here is an outline of the code for the revamped Worker:



public class Worker {
    private ServiceItem[] spaceServices;
    private int numSpaces;
    . . .
    public Worker() {
        ServiceMatches serviceMatches = SpaceAccessor.getSpaces();
        spaceServices = serviceMatches.items;
        numSpaces = serviceMatches.totalMatches;
        . . .
    }
    public void startWork() {
        ServiceItem spaceService;
        JavaSpace space;
        for (int i=0; ; i++) {
            spaceService = spaceServices[i % numSpaces];
            space = (JavaSpace)spaceService.service;
            // . . . take task from that space, execute, write
            // . . . result to that space
        }
    }
}

Like the revised master, the worker makes a call to getSpaces to obtain an array of available JavaSpace services. Armed with that, the worker loops indefinitely. At each iteration i, the worker concentrates its efforts on the space (i % numSpaces) in the array of spaces: It takes any task from that space, computes it, and returns the result to that space. In effect, the worker visits the spaces in round-robin fashion, taking and performing a task from each in turn.

To try out the new compute server, start up more than one JavaSpace service (if you're unsure how to do this, refer to "The Nuts and Bolts of Compiling and Running JavaSpaces Programs" listed in Resources). Then run a master process and one or more worker processes. From the output, you should be able to get a sense of how the master distributes tasks and how each worker visits the spaces in a round-robin way.

<<  Page 7 of 8  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us