aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Implementing Lock/Unlock using Event delegator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Implementing Lock/Unlock using Event delegator" Watch "Implementing Lock/Unlock using Event delegator" New topic
Author

Implementing Lock/Unlock using Event delegator

Jeff Shen
Ranch Hand

Joined: Sep 04, 2002
Posts: 31
I have read the book "java2Certification". The Author of this book suggested use the Event Delegator or the Massage subscription to implement the Lock/Unlock on the server. Is this appraoch is samilar like Michael mentioned the ServerService to do the client registery?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jeff,
It can be implemented that way. That puts more work on the server, which is not necessarily a bad thing since that would allow clients to go about their other business while waiting for a lock. I took care of it on the client, creating a new thread for each booking operation (which is the only way that lock and unlock were ever called in my design). Either way when the the lock is acquired (or in my case when the whole booking operation completed) the caller should be notified, either thru a Remote interface if implemented on the server or by using the Observer pattern on the client.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jeff Shen
Ranch Hand

Joined: Sep 04, 2002
Posts: 31
Thanks Michael
I have another question regarding the Sercer site ConnectionFactory. I am using RMI. Should the ConnectionFactory just return a reference of the RemoteData or create a new thread for each client.
Regards,
Jeff.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

"Jeff"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jeff,
It should just return a remote connection object. RMI will take care of threading for any RMI (Remote) object. You may (and probably will) have several threads running on your server but those will have nothing to do with RMI itself.
Hope this helps,
Michael Morris
Jeff Shen
Ranch Hand

Joined: Sep 04, 2002
Posts: 31
Thanks Michael
A question about close() the local database. Since the Data class implements the DataInterface and I prefer do not have the close() method in the DataInterface then how can I close the local database.
Regards,
Junfeng
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jeff,
Well you can just ungracefully exit. You might want to set whatever object(s) hold(s) a reference to Data to null, which should cause the finalize method in Data to close the connection to the RandomAccessFile, assuming that the gc collects it before exit. Of course if you can set an object to null that holds a reference to Data you could just as easily call data.close() in that object's finalize method.
Hope this helps,
Michael Morris
[ September 04, 2002: Message edited by: Michael Morris ]
Jeff Shen
Ranch Hand

Joined: Sep 04, 2002
Posts: 31
Thanks a lot Michael,
I still have something unclear.
1)What is the advantage to use the LockManager instead Lock/Unlock method in the RemoteData object directly.
2)If I use the the LockManager should my "bookFlight" method from Client directly instantiaed the LockManager and use the lock/unlock method or should the "bookFlight" method call the lock/unlock of the RemoteData and then let lock/unlock method call the LockManager.
Regards,
Jeff
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jeff,

1)What is the advantage to use the LockManager instead Lock/Unlock method in the RemoteData object directly.

Using a lock manager is a more object oriented approch to locking. You delegate record locking to it which makes more sense than letting Data take care of it, at least from an OO point of view. You don't even need to do anything in Data's lock and unlock methods if you use a lock manager, you can just leave them empty.

2)If I use the the LockManager should my "bookFlight" method from Client directly instantiaed the LockManager and use the lock/unlock method or should the "bookFlight" method call the lock/unlock of the RemoteData and then let lock/unlock method call the LockManager.

Definitely the latter. That way the client is oblivious to the fact that there is even a lock manager involved. As far as it know, it's just calling lock and unlock on Data's public interface. Besides, if you instanitate LockManager on the client, then all clients would have their own different lock manager. How would client A's lock manager know what records were locked in client B's?
Hope this helps,
Michael Morris
Jeff Shen
Ranch Hand

Joined: Sep 04, 2002
Posts: 31
Again all the ansers help me so much.
I can understand and implement the client site ConnectionFactory, however I am not sure what is the server site ConnctionFactory (I am using RMI) and how to implement it.
Regards,
Jeff
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jeff,

I can understand and implement the client site ConnectionFactory, however I am not sure what is the server site ConnctionFactory (I am using RMI) and how to implement it.

I really don't know why you need a ConnectionFactory on the client since all you need is a single object for local access. On the server the ConnctionFactory creates new remote objects for all requesting clients. It will usually have a method named getConnection(). The ConnctionFactory is bound to the RMI registry so that clients can find it and then get their remote connection. Of course the getConnection() method will return a stub to a remote DataAccess (or whatever you call your public Data interface) implementation.
Hope this helps,
Michael Morris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing Lock/Unlock using Event delegator