Make your own free website on Tripod.com

Remoting JDO objects using RMI

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................JDK 1.2 or higher (I use JDK 1.3.1)
.........................................PE:JTM - The Productivity Environment for JavaTM (from HYWY Software Corporation)

 

Note: This article assumes that the reader has already created simple bank project using PE:J and JDO.

The Transfer Money Object Interaction Diagram is as shown below:

The Transfer Money logic is as follows:

  1. The IIOP Client (IIOPClient.java) obtains a reference to the TellerRemote (TellerRemote.java) remote object from Teller Server (TellerServer.java).

  2. The IIOP Client (IIOPClient.java) invokes the transferMoney() method of the TellerRemote (TellerRemote.java) object.

  3. The TellerRemote (TellerRemote.java) invokes the TransferMoney() method on the JDO Client (Teller.java).

  4. The JDO Client (Teller.java) obtains a configured PersistenceManagerFactory from the JDOAdapter.

  5. The JDO Client (Teller.java)  uses the configured PMF to get a PersistenceManager.

  6. The JDO Client (Teller.java)  uses the Persistence Manager to obtain a Transaction object.

  7. The JDO Client (Teller.java) begins a new Transaction using the Transaction object.

  8. The JDO Client (Teller.java) invokes the credit method on the Checking object.

  9. The JDO Client (Teller.java) then invokes the debit method on the Savings object.

  10. The JDO Client (Teller.java) then commits the transaction by invoking the method in the Transaction object.

  11. The JDO Client (Teller.java) then closes the Persistence Manager.

  12. The JDO Client (Teller.java) then closes the JDOAdapter.

 

 

com/hywy/samples/remote/client/IIOPClient.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
/**
* The following example illustrates an RMI-IIOP Client
*
* author: Gopalan Suresh Raj
* Copyright (c), 2002. All Rights Reserved.
* URL: http://gsraj.tripod.com/
* email: gopalan@gmx.net
*/

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// To Run
// C:\java\jdk1.3\bin\java -classpath .;C:\PEJ\lib\jdo.jar;C:\PEJ\lib\j2ee.jar;C:\PEJ\lib\pej.jar; com.hywy.samples.remote.client.IIOPClient
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package com.hywy.samples.remote.client;

import java.util.Properties;
import java.util.ArrayList;
import java.util.Iterator;

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import com.hywy.pej.persistence.oidserver.Oid;

import com.hywy.samples.remote.TellerRemote;
import com.hywy.samples.remote.TellerRemoteImpl;
import com.hywy.samples.remote.iiop.TellerServer;

/**
* This Client talks to the Bank Teller to get banking services
*
* Pre-requisites: You will need to have the COS naming server
* and the TellerRemote Server up and running before you can
* invoke them from this client.
*
* @author Gopalan Suresh Raj
*/

public class IIOPClient {

  static final String CONTEXT_NAME = "java.naming.factory.initial";
  static final String IIOP_STRING  = "com.sun.jndi.cosnaming.CNCtxFactory";

  static final String URL_NAME = "java.naming.provider.url";
  static final String IIOP_URL_STRING  = "iiop://localhost:1000";

  /**
   * Entry Point to this Application
   */

  public static void main( String[] args ) {

    try {
      // Create the IIOP Initial Context
      Properties iiopProperties = new Properties();
      iiopProperties.put( TellerServer.CONTEXT_NAME,
                          TellerServer.IIOP_STRING );
      iiopProperties.put( TellerServer.URL_NAME,
                          TellerServer.IIOP_URL_STRING );
      InitialContext iiopContext = new InitialContext( iiopProperties );

      // Obtain a reference to the Servant Object
      TellerRemote teller = (TellerRemote) PortableRemoteObject.narrow( iiopContext.lookup ( "TELLER" ),
                                                                        TellerRemote.class );

      /////////////////////////////
      // Invoke the Servant
      /////////////////////////////

      // try creating new accounts
      try {
        System.out.println("Create Checking Account for Gopalan Suresh Raj");
        teller.createAccount("Gopalan Suresh Raj", 10000000, true);
      }
      catch (Exception exception) {
        exception.printStackTrace();
      }
      try {
        System.out.println("Create Checking Account for Athul Suresh Raj");
        teller.createAccount("Athul Suresh Raj", 20000000, true);
      }
      catch (Exception exception) {
        exception.printStackTrace();
      }

      try {
        System.out.println("Create Savings Account for Gopalan Suresh Raj");
        teller.createAccount("Gopalan Suresh Raj", 910000000, false);
      }
      catch (Exception exception) {
        exception.printStackTrace();
      }

      try {
        System.out.println("Create Savings Account for Athul Suresh Raj");
        teller.createAccount("Athul Suresh Raj", 2000000000, false);
      }
      catch (Exception exception) {
        exception.printStackTrace();
      }

      Iterator iterator = null;
      Object oid = null;

      // List of Checking accounts
      ArrayList checkingAccountList = teller.listAllCheckingAccounts();
      System.out.println("The size of checkingAccountList is :"+checkingAccountList.size());
      iterator = checkingAccountList.iterator();
      System.out.println("_______________________________________");
      System.out.println("     List of Checking Accounts");
      System.out.println("_______________________________________");
      while(iterator.hasNext()) {
        oid = iterator.next();
        if(oid != null) {
          System.out.println("Checking Account Number :"+oid.toString());
        }
      }
      System.out.println("_______________________________________");

      // List of Savings accounts
      ArrayList savingsAccountList = teller.listAllSavingsAccounts();
      System.out.println("The size of savingsAccountList is :"+savingsAccountList.size());
      iterator = savingsAccountList.iterator();
      System.out.println("_______________________________________");
      System.out.println("     List of Savings Accounts");
      System.out.println("_______________________________________");
      while(iterator.hasNext()) {
        oid = iterator.next();
        if(oid != null) {
          System.out.println("Savings Account Number :"+oid.toString());
        }
      }
      System.out.println("_______________________________________");

      Long longValue = null;
      // Try deleting the first 2 accounts
      if((checkingAccountList.size() > 0)&&(savingsAccountList.size() > 0)) {
        try {
          Oid checkingOid = (Oid)checkingAccountList.get(0);
          if(checkingOid != null) {
            System.out.println("Delete Checking Account Number :"+checkingOid.toString());
            teller.deleteAccount(checkingOid.toLong());
            checkingAccountList.remove(checkingOid);
            System.out.println("The size of checkingAccountList is now :"+checkingAccountList.size());
          }
          Oid savingsOid = (Oid)savingsAccountList.get(0);
          if(savingsOid != null) {
            System.out.println("Delete Savings Account Number :"+savingsOid.toString());
            teller.deleteAccount(savingsOid.toLong());
            savingsAccountList.remove(savingsOid);
            System.out.println("The size of savingsAccountList is now :"+savingsAccountList.size());
          }
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }

      // Try transfering money from one account to the other
      if((checkingAccountList.size() > 0)&&(savingsAccountList.size() > 0)) {
        try {
          Oid fromOid = (Oid)checkingAccountList.get(0);
          Oid toOid = (Oid)savingsAccountList.get(0);
          if((fromOid != null) && (toOid != null)) {
            System.out.println("Transfer Money - 100 from Checking Account :"+
                               fromOid.toString()+
                               " to Savings Account :"+
                               toOid.toString());
            boolean result = teller.transferMoney(fromOid.toLong(),
                                                  toOid.toLong(),
                                                  100);
            if(result == true) {
              System.out.println("The Money Transfer was a SUCCESS");
            }
            else {
              System.out.println("The Money Transfer was a FAILURE");
            }
          }
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }
    }
    catch ( Exception exception ) {
      exception.printStackTrace ();
    }

  }
}

 

click here to go to
My JDO HomePage...

click here to go to
My Advanced Java Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since February 26,2002.

Last Updated : Feb 26, 2002

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2002, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.