Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket timeouts for RMI  RSS feed

 
Gayatri Shende
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does RMI have a timeout period within which a remote invocation must return? Can I change this timeout period programatically for a specific remote method invocation?




Thanks
Gayatri
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. There is a way.
You can implement a custom RMI socket factory that sets the socket timeout to the desired value.
This socket factory can be specified while exporting/creating a remote object.

This tutorial tells the details to write and use a custom socket factory.

You can use the below code for setting the socket timeout value.
(This is not the complete code but just the createServerSocket() method of RMIServerSocketFactory. timeOutPeriod is the time out period that you would wish to set. Similarly you can write a client socket factory for client rmi sockets)

 
Gayatri Shende
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks . I tried it. But dont know what the actual problem is. I created the custom socket and set the timout to 5 sec. Now if there is no response from server for 5 sec, client should time out. But it does not

here is my code: any hints what is going wrong ?


public static void main(String[] args)throws EncException,Exception {
.
.

.

RMISocketFactory.setSocketFactory(new TimeoutFactory(5));

Object obj = stub.remotemethod();
// If no response from server for 5 sec, it should time out, but it does not.



}


class TimeoutFactory extends RMISocketFactory {
private int timeout;
public TimeoutFactory(int timeout) {
this.timeout = timeout;
}

public Socket createSocket(String host, int port) throws IOException {
Socket ret = getDefaultSocketFactory().createSocket(host, port);
System.out.println("timeout set");
ret.setSoTimeout(timeout * 1000);
return ret;
}
public ServerSocket createServerSocket(int port) throws IOException {
return getDefaultSocketFactory().createServerSocket(port);
}
}



Am i placing the code for setting timeout at wrong place ?


Thanks
Gayatri
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you have specified should work provided:

  • You set the socket factory before looking up the stub.
  • The server object while getting exported does not define a client socket factory.

  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!