1

I am using RMI in my project, we have a RMI Server and RMI Client. Client is in webapplication, so we are storing server reference in common constants.

I want to know, how expensive is it to do lookup for server in registry everytime we need server instance. or is it better to do look up once and store it in some constant.

If i store it in constant then problem begins if RMI server is restarted. because that constant variable holds old reference and it throws java.rmi.NoSuchObjectException: no such object in table

Any Inputs are welcome.

Mahendra
  • 323
  • 1
  • 7

1 Answers1

1

If you want your client application to withstand all server restarts, you should anyway protect it from communication failures, including java.rmi.NoSuchObjectException. Note that it is possible, although not very likely, that a server restart occurs between the lookup and the request execution. In that case, even if you did a lookup before every request, you wouldn't be safe. So the recommendation is as follows:

  1. Do a lookup once and keep the reference. An additional benefit of this would be that your application will be able to discover misconfiguration or other problems on initialization.

  2. If NoSuchObjectException occurs, try to refresh the reference.

Bartosz Klimek
  • 555
  • 2
  • 5
  • `NoSuchObjectException` occurs when we invoke some operation, so in this way i will end up redundant RMI code. – Mahendra Dec 17 '13 at 09:23
  • @Mahendra There is nothing 'redundant' about code that is necessary to make your application work correctly. Don't let arbitrary ideas about aesthetics prevent you from implementing what is necessary: just try to find an elegant way to express it. You can structure this as a do/while loop with a new lookup in the catch block for NoSuchObjectException. – user207421 Dec 17 '13 at 10:24