Remember the DBAccessProgram? In Figure 3 the DBAccessProgram has been extended to use events.
1: /* DBAccessProgram.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: */ 9: 10: package examples.DeveloperGuide.events; 11: 12: import java.net.URL; 13: import java.sql.*; 14: import java.util.*; 15: import java.io.*; 16: import java.rmi.*; 17: 18: import COM.meitca.concordia.Agent; 19: import COM.meitca.concordia.event.*; 20: import COM.meitca.concordia.security.*; 21: 22: 23: /* 24: * A program which accesses an ODBC database (via JDBC). 25: * This example accesses the "demo" database. This example requires 26: * JDBC, ODBC and the JDBC-ODBC bridge. 27: * 28: */ 29: public class DBAccessProgram implements EventHandler { 30: 31: // This object contains the results of the SQL query made by the agent. 32: private QueryResult itsResult; 33: 34: // This string specifies the sales region the agent is interested in 35: private String itsRegion; 36: 37: // This agent's event queue. 38: private EventPost eventQueue; 39: 40: // A connection to the Event Manager 41: private EventManagerConnection eventManagerConnection; 42: 43: /* 44: * Constructs a DBAccessProgram 45: */ 46: public DBAccessProgram(String region) throws EventException { 47: itsRegion = region; 48: itsResult = new QueryResult(); 49: 50: try { 51: eventQueue = (EventPost)new EventQueueImpl(this); 52: Class eventClass[] = {DBUpdateEvent.class}; 53: eventManagerConnection = new EventManagerConnection(); 54: eventManagerConnection.makeConnection(false); 55: eventManagerConnection.registerEvents(eventClass, eventQueue); 56: } catch (Exception e) { 57: throw new EventException(e.toString() + e.getMessage()); 58: } 59: } 60: 61: /* 62: * Executes a SQL query on a database. This method performs a 63: * simple SELECT query on an odbc database. The results of the 64: * query are stuffed into a QueryResult object. 65: */ 66: public void queryDatabase() { 67: 68: try { 69: 70: // Load the JDBC-ODBC Bridge Driver 71: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 72: 73: // Create a URL specifying an ODBC data source name. 74: // This URL indicates an ODBC data source named demo. 75: String url = "jdbc:odbc:demo"; 76: 77: // Connect to the database at that URL. 78: Connection con = DriverManager.getConnection(url); 79: 80: // Execute a SELECT statement 81: Statement stmt = con.createStatement(); 82: ResultSet rs = stmt.executeQuery("SELECT DISTINCTROW Regions.Region, Sum([UnitPrice]*[Quantity]) AS Total " + 83: "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 " + 84: "GROUP BY Regions.Region " + 85: "HAVING (((Regions.Region)='" + itsRegion + "')) " + 86: "ORDER BY Regions.Region;"); 87: 88: // Retrieve the result. 89: rs.next(); 90: 91: //pull the results out of the Statement 92: itsResult.region = rs.getString(1); 93: itsResult.sales = rs.getFloat(2); 94: 95: stmt.close(); 96: con.close(); 97: } catch (Exception ex) { 98: // Post a DBAcessProgramFailedEvent. 99: try { 100: eventManagerConnection.postEvent(new DBAccessProgramFailedEvent(ex)); 101: } catch (Exception ex2) { 102: } 103: } 104: } 105: 106: /* 107: * Report query results to console using System.out.println 108: * statements. This method could also report the results 109: * using an AWT GUI, or had the results off to another 110: * Java class for manipulation or reporting. 111: */ 112: public void reportResults() { 113: System.out.println("Sales for region " + itsResult.region + ":"); 114: System.out.println("Sales = " + itsResult.sales); 115: System.out.print("\n"); 116: 117: } 118: 119: /* 120: * The event handler. 121: */ 122: public void handleEvent(EventType event) throws EventException { 123: 124: if (event instanceof DBUpdateEvent) { 125: System.out.println("Database has been updated"); 126: queryDatabase(); 127: } 128: } 129: 130: public static void main(String args[]) 131: throws Exception { 132: 133: System.setSecurityManager(new InsecurityManager()); 134: DBAccessProgram app = new DBAccessProgram("Europe"); 135: app.queryDatabase(); 136: app.reportResults(); 137: System.exit(0); 138: } 139: 140: }
Figure 3 - The Database Access Program
This program is very similar to the original database access program. However, its constructor (lines 46-59) contains a few additions. It first creates an EventQueueImpl object for asynchronous event notification (line 51). Then it creates a direct connection to the Event Manager (lines 53-54). The argument passed to makeConnection() indicates that a direct connection will be used to communicate with the Event Manager (rather than a proxy). Finally, the constructor registers to receive events of type DBUpdateEvent (line 55).
The queryDatabase() method also posts a DBAccessProgramFailedEvent whenever it fails to access the database (lines 97-103).
The event handler (lines 122-128) queries the database whenever it receives a DBUpdateEvent.