R
Rennie deGraaf
I want to create a long-running RMI server, which makes an object
available to remote hosts, and does nothing else. However, the server
consistently exists with no exception or error after >=60 seconds. My
best guess is that the program is running off the end of main(), and my
object is getting garbage collected 60 seconds later.
Is there a better way to prevent my server from exiting than waiting on
a dummy object or going into a sleep loop?
Here's (a reduced version of) my code:
RemoteInterface.java:
public interface RemoteInterface extends java.rmi.Remote
{
public String doRequest() throws java.rmi.RemoteException;
}
RMIServer.java
import java.rmi.*;
import java.rmi.server.*;
public class RMIServer extends UnicastRemoteObject implements
RemoteInterface
{
public RMIServer() throws RemoteException
{
super();
}
public String doRequest()
{
System.out.println("executing");
return "done";
}
public static void main(String[] args)
{
try {
// create a remote object and register it
RMIServer obj = new RMIServer();
Naming.rebind("rmi://localhost:1099/object", obj);
}
// throws if the URL to the rmiregistry is invalid
catch (java.net.MalformedURLException e) {
System.err.println("Malformed URL: " + e);
}
catch (RemoteException e) {
System.err.println("RMI error: " + e);
}
}
}
Rennie
available to remote hosts, and does nothing else. However, the server
consistently exists with no exception or error after >=60 seconds. My
best guess is that the program is running off the end of main(), and my
object is getting garbage collected 60 seconds later.
Is there a better way to prevent my server from exiting than waiting on
a dummy object or going into a sleep loop?
Here's (a reduced version of) my code:
RemoteInterface.java:
public interface RemoteInterface extends java.rmi.Remote
{
public String doRequest() throws java.rmi.RemoteException;
}
RMIServer.java
import java.rmi.*;
import java.rmi.server.*;
public class RMIServer extends UnicastRemoteObject implements
RemoteInterface
{
public RMIServer() throws RemoteException
{
super();
}
public String doRequest()
{
System.out.println("executing");
return "done";
}
public static void main(String[] args)
{
try {
// create a remote object and register it
RMIServer obj = new RMIServer();
Naming.rebind("rmi://localhost:1099/object", obj);
}
// throws if the URL to the rmiregistry is invalid
catch (java.net.MalformedURLException e) {
System.err.println("Malformed URL: " + e);
}
catch (RemoteException e) {
System.err.println("RMI error: " + e);
}
}
}
Rennie