• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unreferenced

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use this interface java.rmi.server.Unreferenced to handle lost clients. I start up my server and then my client but when I close my client, the method i.e unreference) is not being invoked.

I just put a simple print system to see if the method invoked even after waiting 15 mins, the method is not invoked. By the way my Remote class implements this interface.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi GD,

Sometimes it helps if you create a very simple test application which will only test the issue you have a problem with. If it still has a problem, then you can post the code for that problem here and hopefully have a solution posted.

One possibility is that you only have one RemoteObject with which you have both implemented Unreferenced and bound to the RMI Registry. If you have done that, then the unreferenced method will never be called, since the RMI Registry will always maintain a reference to it.

To make that a little clearer, here is some code that will show Unreferenced being called. Contradicting my first sentence, this program is actually designed to show three things - when unreferenced is called, when the remotely called method completes, and when the finalizer is called. It is important to realise that just because unreferenced has been called does not mean that the method has completed.



For the purposes of this test, I have set the lease value to 10 seconds. I recommend against doing this in your real assignment or in the real world.

I have only created one source file (called ClientDied.java) which contains all the interfaces and classes for both the client and server. While I recommend against this in real life, this is useful for just copying and pasting from my computer to this thread and from this thread back to your PC.

Since everything is in one file, compiling this is as simple as:

Open one command prompt, and start the application (java ClientDied) and you should see the server start. In another command prompt run the same program again, and it will attempt to start the server, and after that fails, the client will start - simultaneously you should see the information in the server window telling us what is going on.

The results of running the client are:

Of course your date / time strings will be different.

The more interesting results are on the server side, which looks similar to:

The interesting information is:
  • The client stopped at 12:34:23
  • Unreferenced ws called at 12:34:34 (as expected with our lease value of 10 seconds)
  • The remote method continued to run until it's normal completion at 12:34:45
  • Finalize was called some time after that

  • Hope some of this helps.

    Regards, Andrwe
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi GD,

    One more comment - I have basically just dumped a lot of information in the above post, without going into many details. If there is anything at all that is confusing, please ask.

    Regards, Andrew
     
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Andrew Monkhouse:

    For the purposes of this test, I have set the lease value to 10 seconds. I recommend against doing this in your real assignment or in the real world.



    Hi,

    Are you allowed to set this value in the assignment? I've got Unreferenced to work, but using the default value (whatever that is?) it doesn't seem to get called (unless its a very long time). How did everyone else implement the lease value for Unreferenced?

    Cheers.
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Matt,

    Are you allowed to set this value in the assignment? I've got Unreferenced to work, but using the default value (whatever that is?) it doesn't seem to get called (unless its a very long time). How did everyone else implement the lease value for Unreferenced?



    The default lease value is 10 minutes.

    Personally I think that you do not need to worry about this. I have seen candidates pass with very high marks when they have not done any form of dead client handling. So I believe that it is quite acceptable to write that handling dead clients is beyond scope in the design decisions documents.

    However when someone (such as the original poster) raises a question, I normally try to answer it rather than simply stating that it is out of scope. Unfortunately this sometimes gives people the (mistaken) impression that I feel that Unreferenced is required (or that Max feels that WeakHashMap is the solution).

    As to what I would recommend for you: I would not set the lease value. If you wanted to, you could make a comment in your instructions stating that the lease value is not explicitly set, so the default of 10 minutes will apply before client death is handled. That way the assessor will know what the default operation will be.

    Regards, Andrew
     
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Andrew ,
    after reading your above material,i want to ask you some questions:
    1. if the client haven't accomplish the startClient function and died(i mean the client machine crash.suppose the server is running on another machine).so ,what will happend to the server?
    2. if the client accomplish the startClient but unaccomplish the run function and died.also,what will happend to the server?
    3. under the above two circs,would the server side's unreferenced function still be calld?why?
    long for your replay.thanks in advance!
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Andrew,

    Thanks for the reply. I've been in two minds whether Unreferenced would be required. On the one hand I feel that it is beyond the scope of the project, however on the other I feel that by not implementing it I could be marked down for having a memoryleak in the code. The clarity of my code also suffers if it is implemented. I guess I can make a decision when I'm nearing the end of the project. Thanks again though.

    Matt.
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Joe

    1. if the client haven't accomplish the startClient function and died(i mean the client machine crash.suppose the server is running on another machine).so ,what will happend to the server?



    If the startClient method does not execute then the client will never connect to the server.

    2. if the client accomplish the startClient but unaccomplish the run function and died.also,what will happend to the server?



    If the daemon thread does not go into run mode before the 8 seconds elapses before the the client app dies then the client will never connect to the server.

    3. under the above two circs,would the server side's unreferenced function still be calld?why?



    In both cases you have mentioned, the client has not connected to the server in any way. So the client never had a reference to an instance of MyServer, so Unreferenced will never be called.

    If you look at the code for the run() method:

    If the client stops anytime before line 6 then the client will not have a reference to an instance of MyServer. So Unreferenced will not be called.

    Once line 6 has executed, the client has a reference to an instance of MyServer. If ever the client releases that reference (explicitly sets the variable server to null, or exits, or crashes) then Unreferenced will be called the next time the distributed garbage collector runs following the lease expiring.

    Regards, Andrew
     
    joe lin
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Andrew,

    Once line 6 has executed, the client has a reference to an instance of MyServer. If ever the client releases that reference (explicitly sets the variable server to null, or exits, or crashes) then Unreferenced will be called the next time the distributed garbage collector runs following the lease expiring.


    the above is the real things that i want to know.
    thanks for your explanation!
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic