Boss Database Access Agent

Remember the collaborating database agent discussed in the collaboration chapter? Suppose multiple DBCollaboratorAgents query different databases to obtain sales figures and share their results. A boss agent could be responsible for handing out work. It launches the agents and later obtains the results of their collaboration.

Figure 4 illustrates this boss agent, the DBBossCollaboratorAgent.


  1: /*  DBBossCollaboratorAgent.java

  2:  *  Copyright © 1997-1998 Horizon Systems Laboratory,

  3:  *  Mitsubishi Electric Information Technology Center America.

  4:  *  All rights reserved.

  5:  *

  6:  *  CONFIDENTIAL AND PROPRIETARY PROPERTY OF MITSUBISHI ELECTRIC ITA.

  7:  *

  8:  *  DESCRIPTION

  9:  *  A boss agent for collaborative database access agent.

 10:  *

 11:  */

 12: package examples.DeveloperGuide.events;

 13: 

 14: import java.sql.*;

 15: import java.rmi.*;

 16: 

 17: import COM.meitca.concordia.*;

 18: import COM.meitca.concordia.collaborate.*;

 19: import COM.meitca.concordia.event.*;

 20: 

 21: public class DBBossCollaboratorAgent extends CollaboratorAgent {

 22:     // Names of database servers

 22:     private String			    itsFirstDBServer;

 24:     private String			    itsSecondDBServer;

 25:     private String			    itsThirdDBServer;

 26: 

 27:     // Collaboration results

 28:     private CollaborationResult combinedResult;

 29: 

 30:     public DBBossCollaboratorAgent(String first, String second, String third)

 31:         throws RemoteException {

 32: 

 33:         makeEventHandler(false);

 34:         itsFirstDBServer = first;

 35:         itsSecondDBServer = second;

 36:         itsThirdDBServer = third;

 37:     }

 38: 

 39: 

 40:     /*

 41:      * Launch agents to perform collaboration and collect the

 42:      * results of the collaboration.

 43:      */

 44:     public void performCollaboration() {

 45: 

 46:         try {

 47:             DBAccessAgentGroup	group = new DBAccessAgentGroup();

 48: 

 49:             // add ourselves to the group

 50:             addGroup(group);

 51: 

 52:         	String relatedClasses[] = {"examples.DeveloperGuide.events.QueryResult",

 53:         	    "examples.DeveloperGuide.events.CollaborationResult",

 54:         	    "examples.DeveloperGuide.events.DBAccessFailedEvent",

 55:         	    "examples.DeveloperGuide.events.DBUpdateEvent"};

 56: 

 57:             DBCollaboratorAgent agent1 = new DBCollaboratorAgent(group, "North America");

 58:             Itinerary		itinerary1 = new Itinerary();

 59:             itinerary1.addDestination(new Destination(itsFirstDBServer, "queryDatabase"));

 60:             agent1.setItinerary(itinerary1);

 61:              agent1.setHomeCodebaseURL(getHomeCodebaseURL());

 62: 		    agent1.setRelatedClasses(relatedClasses);

 63: 

 64:             DBCollaboratorAgent agent2 = new DBCollaboratorAgent(group, "South America");

 65:             Itinerary		itinerary2 = new Itinerary();

 66:             itinerary2.addDestination(new Destination(itsSecondDBServer, "queryDatabase"));

 67:             agent2.setItinerary(itinerary2);

 68:             agent2.setHomeCodebaseURL(getHomeCodebaseURL());

 69: 		    agent2.setRelatedClasses(relatedClasses);

 70: 

 71:             DBCollaboratorAgent agent3 = new DBCollaboratorAgent(group, "Europe");

 72:             Itinerary		itinerary3 = new Itinerary();

 73:             itinerary3.addDestination(new Destination(itsThirdDBServer, "queryDatabase"));

 74:             agent3.setItinerary(itinerary3);

 75:             agent3.setHomeCodebaseURL(getHomeCodebaseURL());

 76: 		    agent3.setRelatedClasses(relatedClasses);

 77: 

 78:             // launch the Agents...

 79:             agent1.launch();

 80:             agent2.launch();

 81:             agent3.launch();

 82: 

 83:             synchronized(Thread.currentThread()) {

 84: 

 85:                 try {

 86:                     Thread.currentThread().wait(100);

 87:                 } catch (InterruptedException e) {}

 88:             }

 89: 

 90:             combinedResult = (CollaborationResult)collaborate(group, null);

 91:         } catch (Exception e) {

 92:             System.err.println(e.getMessage());

 93:             e.printStackTrace();

 94:         }

 95:     }

 96: 

 97: 

 98:     /*

 99:      * Report query results to console using System.out.println

100:      * statements.  This method could also report the results

101:      * using an AWT GUI, or had the results off to another

102:      * Java class for manipulation or reporting.

103:      */

104: 	public void reportResults() {

105: 		System.out.println("Maximum sales was in the " + combinedResult.highest.region + " Region");

106: 		System.out.println("With sales of $" + combinedResult.highest.sales);

107: 

108: 		System.out.println("Other regions were");

109: 

110: 		for(int i=0; i<combinedResult.others.length; i++) {

111: 			System.out.println("\tRegion: " + combinedResult.others[i].region);

112: 			System.out.println("\tSales: $" + combinedResult.others[i].sales);

113: 		}

114: 	}

115: 

116: 

117:     /*

118:      * The event handler.

119:      */

120:     public void handleEvent(EventType event)

121:         throws EventException {

122: 

123:         if (event instanceof DBAccessFailedEvent)

124:             System.out.println("Agent failed to access database");

125:     }

126: 

127: }


Figure 4 - The Boss Database Access Agent

When the DBBossCollaboratorAgent is launched, it executes the performCollaboration method on its home machine (i.e., it does not travel anywhere). The constructor creates an asynchronous event handler for this agent (line 33). The performCollaboration method launches three DBCollaboratorAgents to obtain sales figures for North America, South America, and Europe (lines 44-95). Before creating the agents, the DBBossCollaboratorAgent instantiates a new DBAccessAgentGroup and joins the group (lines 47-50). It then passes the group to the constructor of each agent it creates (lines 57, 64, 72) and these agents also join the group during initialization.

The DBBossCollaboratorAgent also contains an event handler that logs all failed database accesses (lines 123-124).