RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Feb 12, 2008

Raising Notifications from RMI

Ever wish you could have your RMI application notify you when it's using the distributed garbage collector? Sometimes it's useful to know when a RMI server "kills" an object. Just perform the following steps:
  1. Implement the java.rmi.server.Unreferenced interface in your RMI server.
  2. Provide an implementation for the unreferenced() method.
  3. Specify a value for the java.rmi.dgc.leaveValue system property. This value represents miliseconds (default ten minutes) and it is used to tell how much time must pass without clients interaction before an object is collected.
  4. The client's interaction represents a "lease contract" that must be updated.

What follows is an example of an RMI server that returns the timestamp of a collected object:

import java.rmi.*;
import java.rmi.server.*;
import java.util.Properties;

public class SumImpl extends UnicastRemoteObject implements SumInterface,Unreferenced{

   public SumaImpl()throws RemoteException

   //the SumInterface method
   public int Sum(int a,int b)throws RemoteException{
      return (a+b);  

   //the unreferenced method
   public void unreferenced()
      java.util.Date date=new java.util.Date();
      System.out.println("Unreferenced called..."+date);

   //the finalize method
   public void finalize()throws Throwable
      java.util.Date date=new java.util.Date();
      System.out.println("Finalize called..."+date);

   public static void main(String[] args)
      SumImpl SI=null;
      //create the remote object
         SI=new SumImpl();       
      }catch(java.rmi.RemoteException e)
      }catch(java.rmi.AlreadyBoundException e)
      }catch(java.net.MalformedURLException e)
      }catch(java.rmi.RemoteException e)
The SumInterface is a simple interface that contains a single method, called Sum. Write this interface and a clasic RMI client, and run the RMI server like this:

<HOME>java ?Djava.rmi.dgc.leaveValue=1000 SumImpl
Leonard Anghel
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date