Re: Detect & re-register in Rmi Server

Niclas Hedhman <[email protected]> Tue, 31 Mar 2009 12:39:42 +0800
Newsgroups gmane.comp.java.sun.rmi
Message-ID <[email protected]>
On Mon, Mar 30, 2009 at 3:22 PM, PawanModi <[email protected]> wrote:
> Many thanks for your response. It would be great help if you provide me
> sample code to list if the service is still there & for binding again.

int port = 1099;
try
{
    registry = LocateRegistry.getRegistry( port );
    externalRegistryUsed = true;
}
 catch( Exception e )
{
    // Probably doesn't exist, so let's try and create it.
    registry = LocateRegistry.createRegistry( port );

    // If that fails, the port is probably used. Deploy strategy for that case.
}

// Then Bind your service to the registry.


If the Registry is created inside the same JVM as your service it will
NOT disappear. But, you also need to poll for the external registry at
regular intervals.

if( externalRegistryUsed )
{
    try
    {
        String[] apps = registry.list();
        // Loop through the array and see if your service is in there.
        // IF not, the Registry has been restarted, and you should bind again.
    } catch( RemoteException e )
    {
        // registry is probably gone altogether. Retry a couple of
times with a short delay,
        // If not comes back up, then do a creation of a new Registry
inside this JVM
        // and bind the service to it.
    }
}


Cheers
-- 
Niclas Hedhman, Software Developer
http://www.qi4j.org - New Energy for Java

I  live here; http://tinyurl.com/2qq9er
I  work here; http://tinyurl.com/2ymelc
I relax here; http://tinyurl.com/2cgsug

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff RMI-USERS".  For general help, send email to
[email protected] and include in the body of the message "help".

For a list of frequently asked RMI questions please refer to:
http://java.sun.com/j2se/1.3/docs/guide/rmi/faq.html

To view past RMI-USERS postings, please see:
http://archives.java.sun.com/archives/rmi-users.html