Timouts for RMI  
Author Message
Volker Raum





PostPosted: 2007-3-7 18:57:00 Top

java-programmer, Timouts for RMI Hi all,
i know there are several posts, and a google search found lots of hits on this topic.
My Problem is: Nothing works...

My intension is very simple:
Use Rmi to transmit data from a client to a server.
In principle it works fine. My Problem are the RMI-timeouts. They are too long.

I have to take into account that some "unclever Guy" pulled the network plug.
My Client still wants to send data to the Server (call a servermethod via rmi).
I call the method and this thread is blocked. Fine. Network is still broken.

Depending on the OS of the client (windows, linux) the Timeouts vary.
In Windows its about 20 Sec => Exception. In Linux? Years? It looks like the method never returns
with an error.

Is there a way to set the timeout for calling a method.
(The time after the methodcall returns an error when the server isn't reachable)


I tried:
a) Setting the RMISocketFactory creating my own clientsockets having the SoTimeout set... didnt work.
b) setting the Systems sun.rmi.transport.connectionTimeout ... didnt work

Hope you can help me.
 
Esmond Pitt





PostPosted: 2007-3-8 9:42:00 Top

java-programmer >> Timouts for RMI Volker Raum wrote:
>
> Is there a way to set the timeout for calling a method.
> (The time after the methodcall returns an error when the server isn't
> reachable)
>
>
> I tried:
> a) Setting the RMISocketFactory creating my own clientsockets having the
> SoTimeout set... didnt work.

It should have. You must have done something wrong.

> b) setting the Systems sun.rmi.transport.connectionTimeout ... didnt work

That's a timeout enforced at the client on idle connections. Nothing to
do with what you want.

> Hope you can help me.

http://java.sun.com/j2se/1.5.0/docs/guide/rmi/javarmiproperties.html
http://java.sun.com/j2se/1.5.0/docs/guide/rmi/sunrmiproperties.html
sun.rmi.transport.tcp.handshakeTimeout
sun.rmi.transport.tcp.readTimeout - this is not documented for some
reason, but it is the timeout for reading the remote method reply in
milliseconds.
 
Volker Raum





PostPosted: 2007-3-8 14:45:00 Top

java-programmer >> Timouts for RMI Thanx a lot for your answer. To be exact i call the following method
public static void configureSockets (final int timeout)
{
try
{
RMISocketFactory.setSocketFactory(new RMISocketFactory()
{
public Socket createSocket(String host, int port) throws IOException
{
Socket socket = new Socket(host, port);
socket.setSoTimeout(timeout);
socket.setSoLinger(false, 0);
return socket;
}

public ServerSocket createServerSocket(int port) throws IOException
{
return new ServerSocket(port);
}
});
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}

One question remains. When to call the Method? Before any Naming.lookup() i think.
What i didnt mention is... I create the RMI Registry within my application with

Registry registry = java.rmi.registry.LocateRegistry.createRegistry(registryPort);

Calling the configureSockets method before or after didnt work.

So what do i do wrong?

Greets,
Volker



Esmond Pitt schrieb:
> Volker Raum wrote:
>>
>> Is there a way to set the timeout for calling a method.
>> (The time after the methodcall returns an error when the server isn't
>> reachable)
>>
>>
>> I tried:
>> a) Setting the RMISocketFactory creating my own clientsockets having
>> the SoTimeout set... didnt work.
>
> It should have. You must have done something wrong.
>
>> b) setting the Systems sun.rmi.transport.connectionTimeout ... didnt work
>
> That's a timeout enforced at the client on idle connections. Nothing to
> do with what you want.
>
>> Hope you can help me.
>
> http://java.sun.com/j2se/1.5.0/docs/guide/rmi/javarmiproperties.html
> http://java.sun.com/j2se/1.5.0/docs/guide/rmi/sunrmiproperties.html
> sun.rmi.transport.tcp.handshakeTimeout
> sun.rmi.transport.tcp.readTimeout - this is not documented for some
> reason, but it is the timeout for reading the remote method reply in
> milliseconds.
 
 
Esmond Pitt





PostPosted: 2007-3-8 17:25:00 Top

java-programmer >> Timouts for RMI You shouldn't be using RMISocketFactory at all. It's been deprecated for
about 8 years.

Have a look at RMIClientSocketFactory and
UnicastRemoteObject.UnicastRemoteObject(port, RMIClientSocketFactory,
RMIServerSocketFactory) and
UnicastRemoteObject.exportObject(port, RMIClientSocketFactory,
RMIServerSocketFactory).

OTOH RMI may be interfering with the timeouts after you create the
sockets. Have you tried the system properties I mentioned?
 
 
Volker Raum





PostPosted: 2007-3-12 20:15:00 Top

java-programmer >> Timouts for RMI Uh, got that code from a Website. Thanx for the hint.
I tried the System properties. No result.

Do you have a little example Code for me that shows how a client connects to the server via
Unicast Remote including using its own Socket Factory ?
My code goes something like that

...
String name = "rmi://"+hostname + ":" + port+"/"+servicename ;
Object service = Naming.lookup(name) ;
...

Thanx
Volker



Esmond Pitt schrieb:
> You shouldn't be using RMISocketFactory at all. It's been deprecated for
> about 8 years.
>
> Have a look at RMIClientSocketFactory and
> UnicastRemoteObject.UnicastRemoteObject(port, RMIClientSocketFactory,
> RMIServerSocketFactory) and
> UnicastRemoteObject.exportObject(port, RMIClientSocketFactory,
> RMIServerSocketFactory).
>
> OTOH RMI may be interfering with the timeouts after you create the
> sockets. Have you tried the system properties I mentioned?
 
 
Esmond Pitt





PostPosted: 2007-3-13 7:06:00 Top

java-programmer >> Timouts for RMI Volker Raum wrote:
> I tried the System properties. No result.

What do you mean by 'no result'?

> Do you have a little example Code for me that shows how a client
> connects to the server via
> Unicast Remote including using its own Socket Factory ?

It doesn't. The server defines the client socket factory when it calls
the superclass constructor of UnicastRemoteObject, and the CSF is
serialized to the client along with the stub. There are perfectly
adequate samples in the JDK.
 
 
Volker Raum





PostPosted: 2007-3-13 19:05:00 Top

java-programmer >> Timouts for RMI Dear Esmond,

thank you for your help. Your last words gave me the hint.
I programmed the stuff completely different.
I found a good example in a Sun Tutorial.
I now do it as they say (incl. a soTimeout) and .... BINGO i get the timeout.

AGAIN...
thank you very much



Esmond Pitt schrieb:
> Volker Raum wrote:
>> I tried the System properties. No result.
>
> What do you mean by 'no result'?
>
>> Do you have a little example Code for me that shows how a client
>> connects to the server via
>> Unicast Remote including using its own Socket Factory ?
>
> It doesn't. The server defines the client socket factory when it calls
> the superclass constructor of UnicastRemoteObject, and the CSF is
> serialized to the client along with the stub. There are perfectly
> adequate samples in the JDK.