Solved

rmi timeouts

Posted on 2006-06-16
15
569 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 78
Spring Framework HTTPSession management 1 36
diffSum example 4 37
use lov values 2 50
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

777 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