Solved

rmi timeouts

Posted on 2006-06-16
15
566 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
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 5

Expert Comment

by:tbboyett
Comment Utility
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
Comment Utility
...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
Comment Utility
you could end the thread by setting the boolean to false
0
 
LVL 5

Expert Comment

by:tbboyett
Comment Utility
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:mayankeagle
Comment Utility
>> when attempting to contact a dead server

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

Author Comment

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

In a perfect world, yes.  In reality, no.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 166 total points
Comment Utility
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
Comment Utility
>>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:mayankeagle
Comment Utility
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
Comment Utility
How do I expose a socket object when calling a remote method on an RMI server?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now