Solved

rmi timeouts

Posted on 2006-06-16
15
574 Views
Last Modified: 2013-12-29
How can I set a timeout for RMI clients so that they do not wait seemingly forever when attempting to contact a dead server?  
0
Comment
Question by:abcast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 5

Expert Comment

by:tbboyett
ID: 16924075
You can keep a Date as a Global Variable associated with each client in say a Vector of Objects:
Date lastContactCheck = new Date();

Then what you would do is create a simple thread that checks how long it has been since it last updated.
public class CheckThread extends Thread {
   public void run() {
      while (true) {
         Date date = new Date();

         // start looping through Clients and grabbing the lastCheckDate
         if(date.getTime() - lastContactCheck.getTime() > 400000) {
            // notify User or log them out
         }
         // end loop

         try {
              sleep(60000); // sleep for a minute
         }
         catch(InterruptedException ex) {
              // Do something about exception
         }
      }
   }
}

// Then on your server have a method that would actually update the lastCheckDate
public synchronized void updateCheckDate(int client_id) throws Exception {
    // retrieve the object for that client and update it
    lastCheckDate = new java.util.Date();
}

// start the CheckThread on the server when you start the server
CheckThread check = new CheckThread();
check.start();

// Now you need to create a thread for the client to periodically call the method to update the lastCheckDate
public class updateCheckDateThread extends Thread{

    public updateCheckDateThread() {
        super();
    }

    public void run() {
        while (true) {
            try {
                // call updateCheckDate method on server
            } catch (Exception e) {
                // do something with exception
            }

            try {
                sleep(30000); // sleep for 30 seconds or so
            } catch (InterruptedException ex) {
                // do something with the exception
            }
        }
    }
}

// Now just simply start the thread when the client logs in
updateCheckDateThread updatecheck = new updateCheckDateThread();
updatecheck.start()


Basically, what this will do is for each Client that starts it will update the date by calling a method on the server.  if the server doesn't update the date then you have a dead server.  You would just need to set the times that are best for your application
0
 

Author Comment

by:abcast
ID: 16924092
...but if the server is unresponsive, I still have a client, which is consuming a thread, that is stuck waiting for a timeout for an undetermined amount of time.  Isn't there some sort of timeout property for RMI that actually works?
0
 
LVL 5

Accepted Solution

by:
tbboyett earned 168 total points
ID: 16924128
you could end the thread by setting the boolean to false
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 5

Expert Comment

by:tbboyett
ID: 16924138
Or you could try something like what they discuss here
http://www.jguru.com/faq/view.jsp?EID=332524
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16933534
>> when attempting to contact a dead server

If the server is down, connecting would throw an exception.
0
 

Author Comment

by:abcast
ID: 16934505
>>If the server is down, connecting would throw an exception.

In a perfect world, yes.  In reality, no.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 166 total points
ID: 16934580
It depends on your network configuration - if the network time out value is large then it will take some time, but sooner or later it has to throw.
0
 

Author Comment

by:abcast
ID: 16934597
>>It depends on your network configuration - if the network time out value is large then it will take some time, but sooner or later it has to throw.

True.  But the time to timeout has been variable in practice.  Up to several hours.  This is a problem for me.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16934711
Try the Socket.connect () method overload which takes a timeout parameter:

http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html
0
 

Author Comment

by:abcast
ID: 16934719
How do I expose a socket object when calling a remote method on an RMI server?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16934748
You can create your own factory:

RMISocketFactory.setSocketFactory ( new RMISocketFactory () {
      public Socket createSocket ( String host, int port ) throws IOException
      {
            Socket socket = new Socket ( host, port ) ;
            socket.setSoTimeout ( timeoutMillis ) ;
            socket.setSoLinger ( false, 0 ) ;
            return socket ;
      }

      public ServerSocket createServerSocket ( int port ) throws IOException
      {
            return new ServerSocket ( port ) ;
      }
} ) ;
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16934751
Or you can try setting the "sun.rmi.transport.tcp.readTimeout" property.
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 166 total points
ID: 16945937
Hi,

check out this...

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.omg.CORBA.ORB;
import org.omg.CORBA.COMM_FAILURE;
import org.omg.CORBA.CompletionStatus;

import com.sun.corba.se.connection.EndPointInfo;
import com.sun.corba.se.connection.GetEndPointInfoAgainException;
import com.sun.corba.se.connection.ORBSocketFactory;
import com.sun.corba.se.internal.core.IOR;
import com.sun.corba.se.internal.iiop.EndPointImpl;
import com.sun.corba.se.internal.ior.IIOPProfileTemplate ;
import com.sun.corba.se.internal.ior.IIOPAddress ;
import com.sun.corba.se.internal.orbutil.MinorCodes;

public class OwnSocketFactory
    implements
        ORBSocketFactory
{
    public OwnSocketFactory()
    {
    }

    public ServerSocket createServerSocket(String type, int port) throws
            IOException
    {
            if (! type.equals(ORBSocketFactory.IIOP_CLEAR_TEXT)) {
                 throw new COMM_FAILURE(
                "DefaultSocketFactory.createServerSocket only handles " +
                    "IIOP_CLEAR_TEXT, given: " + type,
                    MinorCodes.DEFAULT_CREATE_SERVER_SOCKET_GIVEN_NON_IIOP_CLEAR_TEST,
                    CompletionStatus.COMPLETED_NO);
            }
                                   
        ServerSocket sc = new ServerSocket(port);
            return sc;
    }

    public EndPointInfo getEndPointInfo(ORB orb,
                                        IOR ior,
                                        EndPointInfo endPointInfo)
    {
        IIOPProfileTemplate temp = ior.getProfile().getTemplate() ;
            IIOPAddress primary = temp.getPrimaryAddress() ;

            return new EndPointImpl(ORBSocketFactory.IIOP_CLEAR_TEXT,
                                primary.getPort(),
                                primary.getHost().toLowerCase());
    }

    public Socket createSocket(EndPointInfo endPointInfo) throws
            IOException,
            GetEndPointInfoAgainException
    {
        Socket sc = new Socket(endPointInfo.getHost(), endPointInfo.getPort());
        try {
            sc.setSoTimeout(30000);  // <<---- set the timeout for the socket  
                                    // Here set to 30 seconds
        } catch (Exception e) {
            System.err.println(e);
        }
       
            return sc;
    }
}
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4856776

R.K
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question