The Smart Stub example
This example shows how one can create a smart client stub to cache information
returned from a server object. This is useful to minimize the time
it takes to service a request, particularly if processing time is lengthy.
From this example, you will learn how to:
-
Implement a Smart Stub by deriving from the Client's generated stub code
-
Implement a simple cache within the smart stub that caches data from the
server object
Directory Contents
-
Bank.idl
IDL interface for the 'Bank' object.
-
Client.java
This is the Bank client program which was discussed in the
Bank example. In this class,
the client invokes setStubClass() to make use of the
smart stub.
-
Server.java
This is the Bank Server program which was discussed in the
Bank example.
-
SmartAccount.java
This is the implementation of the smart stub for the Account object.
In this class, instead of first sending a request for the balance,
the code checks to see if one has been obtained from a previous request
and cached. if it has not received the balance, then it sends a request
to the server by calling super.balance(), which causes the
base class (the default stub) to do the remote invocation.
-
SmartAccountManager.java
This is the smart implementation of the AccountManager which caches
all accounts opened so far in a dictionary. When a client asks for an
account to be opened for the first time, the smart Account manager
stores the name of the client in a dictionary. The second time a client
asks to open the same account, the manager looks up the name in its
dictionary.
-
Makefile (vbmake.bat on Windows)
Used to build all the test targets.
Building this example
Typing make(vbmake on Windows) in the smart_stub subdirectory will build the following classes for the examples described above:
Compilation Errors and Warnings
You will see the following compilation errors when you type make(vbmake on Windows) in the smart_stub subdirectory or when you explicitly compile SmartAccountManager.java using JDK1.0.2:
prompt>javac SmartAccountManager.java
./Bank/AccountHelper.java:25: Identifier expected.
_stubClass = Bank._st_Account.class;
^
./Bank/AccountHelper.java:25: '}' expected.
_stubClass = Bank._st_Account.class;
^
./Bank/AccountHelper.java:25: Identifier expected.
_stubClass = Bank._st_Account.class;
^
./Bank/_st_AccountManager.java:46: Method read(
org.omg.CORBA.portable.InputStream)
not found in class Bank.AccountHelper.
_result = Bank.AccountHelper.read(_input);
^
4 errors
*** Error code 1
The above error occurs because the class that is being used by this example is not part of JDK1.0.2. The solution to this problem is to upgrade to JDK1.1.0 or higher.
Running this example
To run the examples, first make sure that the VisiBroker Smart Agent (osagent
executable) is running on your network. Then start the Server
using the command:
prompt> vbj Server &
(start vbj Server on Windows)
// make the Server run in the background
Next, query the balance in a user's account using the command
prompt> vbj Client John
or
prompt vbj Client
// Uses a default name
Return to the top-level examples page.