Results 1 to 16 of 16
  1. #1
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default java RMI and synchronization

    if I have a remote object that extends UnicastRemoteObject and there are 2 or more processes which call the remote object's methods, how do I prevent race conditions, inconsistency issues, etc when the 2 or more processes call the same method at the same time?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    if I have a remote object that extends UnicastRemoteObject and there are 2 or more processes which call the remote object's methods, how do I prevent race conditions, inconsistency issues, etc when the 2 or more processes call the same method at the same time?
    You'd do it the same as you would do with local methods: synchronize them, synchronization isn't part of the interface definition (it's an 'implementation detail').

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You'd do it the same as you would do with local methods: synchronize them, synchronization isn't part of the interface definition (it's an 'implementation detail').

    kind regards,

    Jos
    but isn't synchronization using the 'synchronized' keyword for threads? in this case there would be 2 client JVMs(processes) and 1 server JVM(processes)

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    but isn't synchronization using the 'synchronized' keyword for threads? in this case there would be 2 client JVMs(processes) and 1 server JVM(processes)
    True, but if two client objects C1 and C2 refer to the same server object S1 and methods are synchronized in object S1 the two Threads started (indirectly) by objects C1 and C2 are executed synchronized i.e. in a mutual exclusive mode.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    True, but if two client objects C1 and C2 refer to the same server object S1 and methods are synchronized in object S1 the two Threads started (indirectly) by objects C1 and C2 are executed synchronized i.e. in a mutual exclusive mode.
    so java rmi spawns individual threads on the server end based on each client call?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    so java rmi spawns individual threads on the server end based on each client call?
    It has to if it wants the remote object to act transparently as if it were a local object. Of course it uses Executors and Futures and minimizes the resources needed by those Threads.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    if my remote object has a login(), logout() and a doWork() method and I want each client to login first before they are allowed to invoke doWork, how do I go about implementing such with multiple clients? since one client can login and another client can piggyback on the first client login to doWork, how can I prevent that?

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    if my remote object has a login(), logout() and a doWork() method and I want each client to login first before they are allowed to invoke doWork, how do I go about implementing such with multiple clients? since one client can login and another client can piggyback on the first client login to doWork, how can I prevent that?
    Your clients need to identify themselves, just as is done in those Servlets, otherwise your remote side can't tell the difference between clients. Given that client identification you can work yourself up from there. Think of RMI as Java's version of RPC.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Your clients need to identify themselves, just as is done in those Servlets, otherwise your remote side can't tell the difference between clients. Given that client identification you can work yourself up from there. Think of RMI as Java's version of RPC.

    kind regards,

    Jos
    hmm, any ideas how I can identify each client?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    hmm, any ideas how I can identify each client?
    There are different scenarios:

    1) the clients make up an id;
    2) the server hands out an id after successful login.

    the problem with 1) is that two ids might not be unique; the disadvantage with 2) is that the server needs to keep track of the ids already handed out.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Is this really a "New to Java" question? If it is, I need to do a lot of work!

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigsonny View Post
    Is this really a "New to Java" question? If it is, I need to do a lot of work!
    It's not really that difficult (read the RMI tutorial) but the op wants to have a notion of logged in clients and clients just 'passing by'; that complicates stuff a bit because RMI is almost the same as remote procedure calling, i.e. it doesn't offer any 'protection', it trusts anything that manages to connect to 'the other side of the wire'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    There are different scenarios:

    1) the clients make up an id;
    2) the server hands out an id after successful login.

    the problem with 1) is that two ids might not be unique; the disadvantage with 2) is that the server needs to keep track of the ids already handed out.

    kind regards,

    Jos
    is it possible to use the thread id to keep track of each client? since I can't get the connecting machine's ip and port as rmi abstracts it out...

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    is it possible to use the thread id to keep track of each client? since I can't get the connecting machine's ip and port as rmi abstracts it out...
    You don't know if it's the same Thread (representing a client) that's consulting your RMI server, so no, a Thread id doesn't make it, you have to keep track of the ids handed out by your server (or made up by your clients).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Let's say I hand out the IDs from the server end, what is to stop a client from spoofing their own ID?

  16. #16
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Track what ID's you've given out, so clients can only spoof ID's that are in use. You also probably want to generate random strings as the IDs. Even with something like 12 characters, it's incredibly difficult to generate one that's in use. 62^12 is a really big number, and that's just with alphanumeric characters.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2011, 12:30 PM
  2. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-07-2011, 07:38 AM
  3. Replies: 1
    Last Post: 05-19-2010, 07:45 AM
  4. Threads and Synchronization
    By ASADUN in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-18-2009, 07:00 AM
  5. Animation Synchronization
    By dreadrocksean in forum Advanced Java
    Replies: 5
    Last Post: 08-08-2008, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •