Rob
Rob

Reputation: 2666

Java RMI, an interface is published as remote on the webserver, while it shouldn't

I'm having a nasty problem, but first let me explain the context.

The project is just a simple project so I get familiar with RMI. The project is a stockmarket server and a client that pulls data about the funds from the server.

I've divided the project in 3 java projects. The server (having MockStockMarket and Fund), the client (having GUI classes and a class to talk to the server: BannerController) and a project with the interfaces which both the client and server need (IStockMarket and IFund).

I want my bannerController to talk with the StockMarket so that the bannercontroller gets the funds. This is done using getFunds() : ArrayList.

As you can see, StockMarket should be Remote, and the Fund should be Serializable.

The problem is, for some reason when I use the following code:

IStockMarket market = new MockStockMarket();
Naming.rebind("rmi://localhost/StockMarket", market);

Both IStockMarket (as intended) AND IFund (not as intended) become remote. Which is not what I want.

For the record: Fund implements IFund, which extends Serializable (so nothing remote) and MockStockMarket extends UnicastRemoteObject and implements IStockMarket, which extends Remote.

Here is a screenshot for the Webserver publishing both interfaces: http://imageshack.us/m/194/4755/rmibothinterfacespublis.png.

For the soure code: https://rapidshare.com/files/2085773800/stockmarket.zip

Upvotes: 0

Views: 648

Answers (3)

Rob
Rob

Reputation: 2666

Oke I found it on the oracle site: http://download.oracle.com/javase/tutorial/rmi/implementing.html.

The webserver is publishing my IFund (non remote) interface because it is passed through a RMI method. The my client needs the IFund to use the passed object. I thought this was enough for RMI to work.

What I didn't know is that the client ALSO needs downloads the class implementation so it can deserialize the object and use the methods of the copied object. For this to work you have to use a securitymanager on the client side. Which is very easy:

if (System.getSecurityManager() == null) 
{
    System.setSecurityManager(new SecurityManager());
}

Upvotes: 0

user207421
user207421

Reputation: 311023

Both IStockMarket (as intended) AND IFund (not as intended) become remote.

No they don't. Objects only 'become remote' by being exported, and interfaces don't 'become remote' at all. IFund is needed by the client, presumably because it appears in the IStockMarket interface. You appear to be using the codebase feature. From the point of view of codebase the Registry is a client too. So the Registry downloaded IFund.class and IStockMarket.class. That doesn't make IFund 'become remote' in any way shape or form.

Upvotes: 1

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23238

Binding an RMI service to a port is different than the Web Server publishing files. The screenshot which you have attached shows that your IStockMarket.class and IFund.class files are exposed as HTTP resources which doesn't have anything to do with "binding" a RMI service. Feel free to add more details to the question if my interpretation is wrong here and I'll try answering them.

Upvotes: 1

Related Questions