wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes FBN RMI Design Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "FBN RMI Design Question" Watch "FBN RMI Design Question" New topic
Author

FBN RMI Design Question

Mario Zott
Greenhorn

Joined: Feb 02, 2003
Posts: 21
I just started the sjcd (Flight by night - exam) and decided to use RMI. So I came up with the following design and would be very thankful for your oppinion:
1. created an Interface called DataClient (implements all public methods of Data)
2. created an Interface called DataRemote
(extends DataClient and Remote)
3. created a Class called DataRemoteImpl
(implements DataRemote and extends UnicastRemoteObject)
4. created a Class called DataAdapter
(implements DataClient and provides Threadsafe access to Data)
5. DataRemoteImpl works with DataAdapter
6. created FactoryClass called DataConnector
(2 methods getRemote getLocal - returns DataClient objects)
7. so the gui only has to work with DataClient Objects
Hope this is a good idea???
Qusay Jaafar
Ranch Hand

Joined: May 06, 2002
Posts: 127
This is the design of the book "The Sun Certified Java Developer Exam with J2SE 1.4" by Habibi.
by the way, I asked Habibi about (your DataConnector) he said that it relates to Adapter pattern. You said it is a Factory pattern. I didn't satisfied that DataConnector related to the Adapter pattern. It works as a Factory more than an Adapter.
If you decide to use ClientID, so you need some little modification in this design.
Good Luck
Cheers


Qusay
Mario Zott
Greenhorn

Joined: Feb 02, 2003
Posts: 21
yes that's true - design is from this book ..... but i am wondering about the DataAdapter (Thread-safe) - Class - is it a better way to implement thread-safety in DataRemoteImpl and implement DataClient in Data (means do not use this way of abstraction between RMI - databaselevel)...
by the way - if i use DataAdapter (in local mode) all methods provide the lock/unlock feature, but it's not necessary...??

oh one more question on my mind: what about the lock/unlock implementation ... is it ok only to synchronize on an LinkedList and add RecordNumbers


and for locking the database wait while lockedRecords is empty and then add all records
think this is too simple..?
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Originally posted by Mario Zott:
by the way - if i use DataAdapter (in local mode) all methods provide the lock/unlock feature, but it's not necessary...??

Hmmm... Maybe I'm being paranoid, but did you see anything in the requirements indicating that someone could not be running a local client on the same machine as the FBN server and accessing the same copy of the database as the remote clients?


SCJP, SCJD, SCEA 5 "Any sufficiently analyzed magic is indistinguishable from science!" Agatha Heterodyne (Girl Genius)
Mario Zott
Greenhorn

Joined: Feb 02, 2003
Posts: 21
hey Burk,
in my specs it says:
Additionally, the program must be able to work in a non-networked mode. In this mode, the database and user interface run in the same VM and no networking is performed, and no sockets should be created. The user must be able to select the operating mode, although it is acceptable that the program stay in one mode once it has started up.
i think it means "EITHER in local or networked mode" ....
assuming i am wrong - if the rmiserver runs and i start a local client on the same server - 2 jvm's are running and also 2 instances of Data (DataAdapter) ... so how can you check if a record is locked?? (only place would be the database file itself) ...
one question about the locking: i can't figure out how a client can be identified 100% (Threads don't work - RMI makes no guarantee, ip don't work because of proxy, ...) do we really have to change method signatures an create client id's at the client side?? - or am i missing something?
Brian Blignaut
Ranch Hand

Joined: Jan 08, 2003
Posts: 61
The approach I took with implementing lock and unlock is as follows.
The spec states that when in local mode, the client and the database run in the same VM. Therefore the client who locked the record is the only one who can unlock the record as he is the only client connected to the database. Therefore I left the implementation of lock and unlock blank in my data class and chose instead to implement them in my remote client object (i.e The unique remote object that each client connecting to the remote database receives) To do the actual locking I used a LockController. When a request to lock a record is received the remote client object then simply makes a call to the Lock controller which then blocks etc. This way I have not modified the method signatures, but the locking is implemented.
Also there is no requirement to protect against to clients running in local mode against the same database file and I would not even consider trying to find a way to fix this, In fact I don't think there is a way.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Mario,
Mine says the same thing. But what it doesn't say is that someone using the system in standalone mode isn't also running the rmi/sockets based server on the same system. And I wouldn't put it past a tester/QA person/exam asser to try it.
As it stands though, I have no solution to the problem. If the client and the server are in separate jvms (even on the same box) there's no way to tell - unless you ignore the spec and have the client connect via RMI/sockets which, I think is an automatic failure.
So, since there's no solution, I'm just going to put it in my design docs as an assumed "you're not gonna do this" and keep going... for now.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You can do this. File.createNewFile() is defined to be atomic, which means you can use it to create lock files. For more sophisticated locking, JDK 1.4 has java.nio.channels.FileLock which can lock even parts of a file.
But all of this is out of scope IMHO. Don't do it.
- Peter
Mario Zott
Greenhorn

Joined: Feb 02, 2003
Posts: 21
thanx for your help...
i think now i'am on the right way....
i created a ConnectionFactory which contains a unique HashMap with Data Objects (so you can use different Data objects), and a method getRemoteConnection(String dbname) which returns DataRemoteImpl objects. The DataRemoteImpl's get the reference to the single data object - so every client has his own DataRemoteImpl which works with the unique data - and is used to identify the client. DataRemoteImpl will also do the locking part. DataRemoteImpl extends UnicastRemoteObject and Implements DataRemote which itself extends Remote and DataClient .... is this the right way to implement RMI-Factory-Pattern ??
BTW whats the difference beetween implementing DataRemote in DataRemoteImpl or implementing Remote and DataCLient directly in DataRemoteImpl ?? i always get a RemoteException doing it this way..
Kevin Cao
Ranch Hand

Joined: Jun 19, 2002
Posts: 55
Hi Mario,
I am reading Habibi's book right now, it is an excellent book, and I have read lots of the discussions about lock/unlock as well. One thing I found a little bit uncomfortable about the design patetrn from the book is this:
We are assuming that clients will not attempt to release a DVD that they did not lock

note: DVD here is same as Data object in scjd, so I will use "Data" in the following
The lock/unlock is implemented in data, and it is pretty much same as people do from this froum.In unlock(), it will not check who is the owner of this record and release it from tracking collection( a vector here). But he use a Adapter class that wraps the data class and doing lock-read-write-unlock sequence.

But what if the code is broken before da.unlock(recono), since getData() is not an atomic operation. So can we synchronize every method in this adapter class? Peter, Mark, Max... I really like to hear you guys comments.
Kevin
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: FBN RMI Design Question
 
Similar Threads
NX CONTRACTORS: A bit confused about RMI, please check design
Extending Data Class
adding exceptions in the interface provided by Sun
Did anybody's program can work both locally and remotely at the same time?
NX: URLy Bird 1.3.1 Connection Factory Design