Page #491 (36.4. RMI vs. Socket-Level Programming)

 
[Page 1253]

36.5. Developing Three- Tier Applications Using RMI

Three-tier applications have gained considerable attention in recent years , largely because of the demand for more scalable and load-balanced systems to replace traditional two-tier client/server database systems. A centralized database system does not just handle data access, it also processes the business rules on data. Thus, a centralized database is usually heavily loaded because it requires extensive data manipulation and processing. In some situations, data processing is handled by the client and business rules are stored on the client side. It is preferable to use a middle tier as a buffer between client and database. The middle tier can be used to apply business logic and rules, and to process data to reduce the load on the database.

A three-tier architecture does more than just reduce the processing load on the server. It also provides access to multiple network sites. This is especially useful to Java applets that need to access multiple databases on different servers, since an applet can only connect with the server from which it is downloaded.

To demonstrate , let us rewrite the example in §36.3.1, "Example: Retrieving Student Scores from an RMI Server," to find scores stored in a database rather than a hash map. In addition, the system is capable of blocking a client from accessing a student who has not given the university permission to publish his/her score. An RMI component is developed to serve as a middle tier between client and database; it sends a search request to the database, processes the result, and returns an appropriate value to the client.

For simplicity, this example reuses the StudentServerInterface interface and StudentServerInterfaceClient class from §36.3.1 with no modifications. All you have to do is to provide a new implementation for the server interface and create a program to register the server with the RMI. Here are the steps to complete the program:

1.
Store the scores in a database table named Score that contains three columns : name , score , and permission . The permission value is 1 or 0, which indicates whether the student has given the university permission to release his/her grade. The following is the statement to create the table and insert three records:

   create table   Scores (name varchar(   20   ), score   number   , permission number);   insert into   Scores values ('   John   ',   90.5   ,   1   );   insert into   Scores values ('   Michael   ',   100   ,   1   );   insert into   Scores values ('   Michelle   ',   100   ,     ); 

2.
Create a new server implementation named Student3TierImpl in Listing 36.5. The server retrieves a record from the Scores table, processes the retrieved information, and sends the result back to the client.

Listing 36.5. Student3TierImpl.java
(This item is displayed on pages 1253 - 1255 in the print version)
 1   import   java.rmi.*; 2   import   java.rmi.server.*; 3   import   java.sql.*; 4 5    public class   Student3TierImpl   extends   UnicastRemoteObject  6    implements   StudentServerInterface  { 7  // Use prepared statement for querying DB  8   private   PreparedStatement pstmt; 9 10  /** Constructs Student3TierImpl object and exports it on  11  * default port.  12  */  

[Page 1254]
 13   public   Student3TierImpl()   throws   RemoteException { 14  initializeDB();  15 } 16 17  /** Constructs Student3TierImpl object and exports it on  18  * specified port.  19  * @param port The port for exporting  20  */  21   public   Student3TierImpl(   int   port)   throws   RemoteException { 22   super   (port); 23  initializeDB();  24 } 25 26  /** Load JDBC driver, establish connection and create statement */  27   protected void   initializeDB() { 28   try   { 29  // Load the JDBC driver  30  // Class.forName("oracle.jdbc.driver.OracleDriver");  31  Class.forName(   "sun.jdbc.odbc.JdbcOdbcDriver"   );  32 System.out.println(   "Driver registered"   ); 33 34  // Establish connection  35  /*Connection conn = DriverManager.getConnection  36  ("jdbc:oracle:thin:@drake.armstrong.edu:1521:orcl",  37  "scott", "tiger"); */  38  Connection conn = DriverManager.getConnection  39  (   "jdbc:odbc:exampleMDBDataSource"   ,   " "   ,   " "   );  40 System.out.println(   "Database connected"   ); 41 42  // Create a prepared statement for querying DB  43 pstmt = conn.prepareStatement( 44   "select * from Scores where name = ?"   ); 45 } 46   catch   (Exception ex) { 47 System.out.println(ex); 48 } 49 } 50 51  /** Return the score for specified the name  52  * Return -1 if score is not found.  53  */  54    public double   findScore(String name)   throws   RemoteException  { 55   double   score =   -1   ; 56   try   { 57  // Set the specified name in the prepared statement  58  pstmt.setString(   1   , name);  59 60  // Execute the prepared statement  61  ResultSet rs = pstmt.executeQuery();  62 63  // Retrieve the score  64   if   (rs. next ()) { 65   if   (rs.getBoolean(   3   )) 66 score =  rs.getDouble(   2   )  ; 67 } 68 } 69   catch   (SQLException ex) { 70 System.out.println(ex); 71 } 72 

[Page 1255]
 73 System.out.println(name +   "\'s score is "   + score); 74   return   score; 75 } 76 } 

Student3TierImpl is similar to StudentServerInterfaceImpl in §36.3.1 except that the Student3TierImpl class finds the score from a JDBC data source instead from a hash map.

The table named Scores consists of three columns, name , score , and permission , where permission indicates whether the student has given permission to show his/her score. Since SQL does not support a boolean type, permission is defined as a number whose value of 1 indicates true and indicates false .

The initializeDB() method (lines 27 “49) loads the appropriate JDBC driver, establishes connections with the database, and creates a prepared statement for processing the query.

The findScore method (lines 54 “75) sets the name in the prepared statement, executes the statement, processes the result, and returns the score for a student whose permission is true .

3.
Write a main method in the class RegisterStudent3TierServer (Listing 36.6) that registers the server object using StudentServerInterfaceImpl , the same name as in Listing 36.2, so that you can use StudentServerInterfaceClient, created in §36.3.1, to test the server.

Listing 36.6. RegisterStudent3TierServer.java
 1   import   java.rmi.registry.*; 2 3   public class   RegisterStudent3TierServer { 4   public static void   main(String[] args) { 5   try   { 6 StudentServerInterface obj =   new   Student3TierImpl(); 7 Registry registry = LocateRegistry.getRegistry(); 8 registry.rebind(   "StudentServerInterfaceImpl"   , obj); 9 System.out.println(   "Student server "   + obj +   " registered"   ); 10 }   catch   (Exception ex) { 11 ex.printStackTrace(); 12 } 13 } 14 } 

4.
Follow the steps below to run this example.

4.1. Start RMI Registry by typing " start rmiregistry " at a DOS prompt from the book directory.

4.2. Start the server RegisterStudent3TierServer using the following command at C:\book directory:

 C:\book>  java RegisterStudent3TierServer  

4.3. Run the client StudentServerInterfaceClient as an application or applet. A sample run is shown in Figure 36.6.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net