Agents: Not just for Bond anymore

Learn what agents are and how to create them using IBM's Aglet Workbench

1 2 3 Page 3
Page 3 of 3
    // doJob() is called when this Slave arrives at its worksite.
    public void doJob() {
        factor(numberToFactor);
    }
    // This is a (horribly inefficient) algorithm for factoring numbers.
    public boolean factor(long num) {
        for (double t = 2; t < num; t++) {
            // Insert an artificial delay for simulation purposes.
            try {
                Thread.sleep(1000);
            } catch (Exception e) { e.printStackTrace(); }
            if ((num / t) == Math.round(num / t)) {
                factor((long)(t));
                factor((long)(num / t));
                return false;
            }
        }
        factors.addElement(new Long((long)num));
        return true;
    }

As indicated by the comments, doJob() will be invoked when the Laborer reaches the work location assigned to it by its master aglet. To illustrate this assignment, let's walk through the code for Foreman, the master aglet. As with all managers, Foreman's primary duty is to delegate tasks to his organization. In our simulation, a Foreman will spawn a new Laborer at each new Timer tick (Timers are discussed fully in the previous examples). The event handler for Timer ticks follows:

        if (msg.kind.equals("Timer")) {
            if (loads == null) {
                return true;
            }
            String leastLoadedServer = "";
            int lowestLoad = 10000;
            for (Enumeration enum = loads.keys(); enum.hasMoreElements();) {
                String server = (String)enum.nextElement();
                int load = ((Integer)loads.get(server)).intValue();
                if (load < lowestLoad) {
                    leastLoadedServer = server;
                    lowestLoad = load;
                }
                System.out.println(server + ": " + load);
            }
            if (numberToFactor > MAXTOFACTOR) {
                return true;
            }
            System.out.println("Dispatching laborer to: " + leastLoadedServer + ", factoring " + numberToFactor);
            try {
                Slave.create(null,
                             "Laborer",
                             getAgletContext(),
                             this,
                             new SeqItinerary(new URL(leastLoadedServer)),
                             new Long(numberToFactor));
                numberToFactor++;
            } catch (Exception e) { e.printStackTrace(); }
            return true;
        }

This rather lengthy event handler first determines the least-loaded server (more on this in a moment). It then creates and spawns a new Laborer to this server. In this manner, the Foreman balances load across the server pool by dispatching new Laborers to the lightest-loaded worksites. The full source for Foreman is available in Foreman.java.

To determine the server with the lightest load, the Foreman relies on another of its lieutenants, the LoadGatherer. LoadGatherer is also a slave aglet, but its task is quite different from Laborer. Instead of factoring numbers, LoadGatherer visits each server in the server pool and counts the number of active laborers at that location. Upon completing a roundtrip, LoadGatherer returns to the Foreman's location and reports his findings via the message-passing mechanism used throughout these examples. The full source code for LoadGatherer can be found in LoadGatherer.java.

Several enhancements could be made to this simplistic tutorial on aglet-based multiprocessing. For one, the example could be optimized dramatically by enabling Laborers to share information on previously factored numbers. The load-balancing mechanism could be improved by enabling the Foreman to redistribute spawned Laborers if load conditions change dramatically among the server pool.

Pointers to the source for all of the classes used in this example follow:

Conclusion

Armed with the techniques demonstrated in this article, the Java developer can begin exploring the world of network mobile agents. Agent technology is relatively young, and its practical potential has yet to be fully realized. As such, there is plenty of room for innovation and creativity in this area of systems engineering.

Bret Sommers is a senior associate with Cambridge Technology Partners, an international systems consulting firm based in Cambridge, MA. Bret's primary interests lie in distributed object and intelligent agent technologies, two fields he became fascinated with while studying at Berkeley. Bret also serves as a co-editor of Digital Espresso, a weekly summary of the traffic appearing in the Java mailing lists and newsgroups. Bret is currently building Java business systems with Sun Microsystems.

Learn more about this topic

1 2 3 Page 3
Page 3 of 3