?
Solved

rmi timeouts

Posted on 2006-06-16
15
Medium Priority
?
575 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 672 total points
ID: 16924128
you could end the thread by setting the boolean to false
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 664 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 664 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 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