Search code examples
javarmi

RMI interface implementation in ServerClass or seperate ImplementationClass


I have read the oracle tutorial about RMI implementation and the chapter about RMI in the book: "Beginning Java 8 APIs, Extensions, and Libraries - Swing, JavaFX, JavaScript, JDBC, and Network Programming APIs."

In the oracle tutorial the implementation of the Remote interface happens directly in the server class whereas in the book, and many other questions on this forum, the author uses a separate implementation class for the interface that gets instantiated in the server class.

One of my questions is whether it is good practice to implement the Remote interface directly in the server class.

In my case I have to be able to suspend and resume the server through the client. I have implemented both ways to do that and they do work.

The interface:

public interface MyRemote extends Remote {

  boolean isSuspended() throws RemoteException;

  void resumeServer() throws RemoteException;

  void suspendServer() throws RemoteException, InterruptedException;
}

To suspend the server I just set a boolean suspended; in the ServerClass

Working with an additional RemoteImplementation class, I can't access that variable. I could call a static method from the ServerClass like ServerClass.suspendSrv() from the RemoteImplementation class.

When implementing the MyRemote interface directly in the ServerClass I can access the variable directly by the implemented methods.

The static way kinda strucks me since I could call those methods without even having a server running and there might be some other issues I can't think of right now.

Finally there is just one question remaining:

Are there any cons to not implement the MyRemote interface directly in the ServerClass?

I really do appreciate your ideas!


Solution

  • Whatever suits you: it doesn't matter to RMI.

    I've always wondered why so many RMI examples use a separate main class to start the server. It's really pointless unless you have several remote objects to be started at once, which is unusual. Mostly you only have one, and you acquire any others via the first one. And putting it into examples gives the false impression that you have to do it that way. You don't.