• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serving RMI without lookup - working, but why?

 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another developer in my group has demonstrated serving up dozens of RMI objects with only one lookup. I look at the code and it is extremely simple. I have a hard time believing that it works. Can someone explain it to me?
He sets up an RMI object on the client and server the normal way. Let's call it RemoteControl. Inside of that is a method called getUserControl(). Inside that method is the line "return new UserControl();"
That's it! I would think it would serialize the server side implementation and the client wouldn't have a matching object, so it would croak. Instead, using this object on the client does make all the server stuff work off of the server.
Why? How?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These type of classes which instantiate new objects in RMI methods are called 'factory classes' and is a general industry practice. This way you avoid creating too many RMI objects on the server side.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, factory classes mean that you only have to register a single object with the rmiregistry. A factory class has, as you describe, at least one method that returns a server object. As this is an RMI server object, it must implement at least one interface that extends Remote. The clever bit depends on how RMI handles remote method parameters and return types: if a Remote object is returned from an RMI remote method call, what's actually returned to the caller is a stub for that object (if the returned object was Serializable, the caller would get a serialized copy instead). So when the client calls the remote getUserControl() method, it gets back a stub for the remote UserControl created on the server, just like it would get back from the rmiregistry by calling lookup. The client can then use this stub to call methods on the UserControl. Ta da!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic