<This functionality is not available in the Freeware version of Concordia>
Figure 4 shows how the DBCollaboratorAgent can be extended to use a proxy.
1: /* DBCollaboratorAgentWithProxy.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 collaborating database access agent that uses a proxy. 10: * 11: */ 12: import java.sql.*; 13: import java.io.*; 14: import java.rmi.*; 15: 16: import COM.meitca.concordia.*; 17: import COM.meitca.concordia.proxy.*; 18: import COM.meitca.concordia.collaborate.*; 19: import COM.meitca.concordia.event.*; 20: 21: public class DBCollaboratorAgentWithProxy extends CollaboratorAgent { 22: private String itsRegion; 23: private AgentGroupProxy itsProxy; 24: private CollaborationResult combinedResult = null; 25: 26: public DBCollaboratorAgentWithProxy(CachedObjectFactory factory, String groupName, Class groupClass, String region) 27: throws RemoteException, IOException, AgentGroupException, ProxyRetryException, 28: IllegalAccessException, InstantiationException, SecurityException { 29: 30: itsRegion = region; 31: itsProxy = AgentGroupProxy.makeProxy(factory, groupName, groupClass); 32: addGroup(itsProxy); 33: } 34: 35: 36: public void queryDatabase() { 37: try { 38: // Load the JDBC-ODBC Bridge Driver 39: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 40: 41: // Create a URL specifying an ODBC data source name. 42: // This URL indicates an ODBC data source named demo. 43: String url = "jdbc:odbc:demo"; 44: 45: // Connect to the database at that URL. 46: Connection con = DriverManager.getConnection(url); 47: 48: // Execute a SELECT statement 49: Statement stmt = con.createStatement(); 50: 51: ResultSet rs = stmt.executeQuery("SELECT DISTINCTROW Regions.Region, Sum([UnitPrice]*[Quantity]) AS Total " + 52: "FROM (Customers INNER JOIN (Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) ON Customers.CustomerID = Orders.CustomerID) INNER JOIN Regions ON Customers.Country = Regions.Country " + 53: "GROUP BY Regions.Region " + 54: "HAVING (((Regions.Region)='" + itsRegion + "')) " + 55: "ORDER BY Regions.Region;"); 56: 57: // Retrieve the result. 58: rs.next(); 59: 60: QueryResult result = new QueryResult(); 61: 62: //pull the results out of the Statement 63: result.region = rs.getString(1); 64: result.sales = rs.getFloat(2); 65: 66: stmt.close(); 67: con.close(); 68: 69: // collaborate the results via the proxy 70: combinedResult = (CollaborationResult)(collaborate(itsProxy, (Object)result)); 71: 72: } catch (java.lang.Exception ex) { 73: ex.printStackTrace(); 74: } 75: } 76: 77: 78: /* 79: * Report query results to console using System.out.println 80: * statements. This method could also report the results 81: * using an AWT GUI, or had the results off to another 82: * Java class for manipulation or reporting. 83: */ 84: public void reportResults() { 85: System.out.println("Maximum sales was in the " + combinedResult.highest.region + " Region"); 86: System.out.println("With sales of $" + combinedResult.highest.sales); 87: 88: System.out.println("Other regions were"); 89: 90: for(int i=0; i<combinedResult.others.length; i++) { 91: System.out.println("\tRegion: " + combinedResult.others[i].region); 92: System.out.println("\tSales: $" + combinedResult.others[i].sales); 93: } 94: } 95: 96: 97: }
Figure 4 - A Collaborating Database Agent Using a Proxy
The agent in the above example only differs slightly from that in Figure 1. The only changes occur in the constructor (lines 26-33) and in the call that performs the collaboration (line 70).
Instead of being passed an AgentGroup reference, the constructor takes three additional arguments: a CachedObjectFactory reference, a String containing the name of the group to be constructed, and a Class object describing an agent group derived from AgentGroupImpl. It passes these arguments to the AgentGroupProxy's static makeProxy method (line 31) and joins the agent group by calling the CollaboratorAgent's addGroup method (line 32), passing it the proxy object returned by the call to makeProxy.
The call to the collaborate method is also performed via the proxy instead of directly on the agent group.