File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Strange RMI - Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Strange RMI - Error" Watch "Strange RMI - Error" New topic
Author

Strange RMI - Error

Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
Hello everybody,

I have a strange RMI Error:

I startup my server.

I connect 1..x Clients.

I shutdown the server.


The Clients still work: The changes are persisted in the file and the changes made by clients 1 can be seen by Client 2.

How can that be ?
Shouldn´t I get some Information that the Server is down, not connectable or so ?


Visit me at http://liferay-blogging.blogspot.com
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4919
    
  10

Hi Daniel,

That is indeed strange RMI behavior you should indeed expect some kind of RemoteException.

How do you stop/shutdown your server? Can you connect a client after server shutdown?

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
Well ... this I am going to find out

But I found out something else yesterday:

I made the mistake of creating the RandomAccessFile on the Client. Means: Client gets Reference to Business Interface, and Business Interface, upon first access, creates the DAO which then accesses the RandomAccessFile.

I now switched it to first creating everything on the Server and then publishing it by RMI.


BUT :

Whatever I do: The DAO is null on the Client. There is no "NotSerializableException" - nothing. It´s just as if the DAO got filtered out.


My structure is like that (sorry for trying to write UML with text ;) )


BusinessInterface <- BusinessImpl - DB <- Data - DAO <- FileSystemDAO - RandomAccessFile


Does anyone of you have an idea why the DAO may be null when the BusinessInterface is retrieved on the client ?
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
Problem solved !!!


Problem 1:

I created the DAO on the Client -> even without a Server the two Clioents were still workingh on the File. The File was the Server ...


Problem 2:

My Business Implementation didn´t extend UnicastRemoteObject. This led to a strange error where only some objects were null (don´t ask me why).

Now everything is fine
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4919
    
  10

Hi Daniel,

This is how my structure looks:
And it is the BusinessServiceImpl which gets exported and is exposed through RMI.

You don't talk directly with the RMI registry, but through a stub (client) and a skeleton (server). These files are generated using the RemoteBusinessService. From Java 5 this is done automatically (you can also generate them with rmic tool, what I did when I submitted my assignment). More info about RMI basics can be found here.

Kind regards,
Roel
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4919
    
  10

Daniel Breitner wrote:My Business Implementation didn´t extend UnicastRemoteObject.
This is not needed, because you are using inheritance and your business implementation is not a (specialized version of the) UnicastRemoteObject. And if you wanted to extend your Business implementation from another class, you will have a problem too (because Java doesn't support multiple inheritance as you know).

You could use exportObject method in UnicastRemoteObject (before exposing it through RMI) to have same functionality as extending from the UnicastRemoteObject.

Daniel Breitner wrote:Now everything is fine
Well done!


Kind regards,
Roel
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
Roel De Nijs wrote:Hi Daniel,

And it is the BusinessServiceImpl which gets exported and is exposed through RMI.

You don't talk directly with the RMI registry, but through a stub (client) and a skeleton (server). These files are generated using the RemoteBusinessService.
Kind regards,
Roel



On question to that :

When I publish my BusinessInterface by RMI it would mean that all my methods have to throw a RemoteException.

When I want to use this Interface in my Controller, it would then have to catch those Exceptions.

I would even have to catch them if I was working locally (no network connection).

Is this allowed when my assignment says "must not use any network functionality at all when running locally" ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4919
    
  10

Hi Daniel,

My solution looks like this:So that's valid code and you are not using network functionality, because you simply get an instance of LocalBusinessServiceImpl (instead of a stub of RemoteBusinessServiceImpl). Other ranchers have created another LocalBusinessService, extending the BusinessService redefining each method without the RemoteException in the throws-clause.

Kind regards,
Roel
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
But wouldn´t that mean that you have to change your BusinessService interface when you want to switch to another networking approach ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4919
    
  10

Hi Daniel,

With the approach (I described in my previous post) you can do without a problem:but of course you have to catch the RemoteException too (which is not a problem, because my client is totally unaware of the mode it is running in). I use one client for both local and networked mode, keeps my code simple and clean.
If you want to use LocalBusinessService you will of course have to make distinction between local and network business service, which will clutter up your code (but you don't have to catch the RemoteException).
In the end it is a decision you have to make depending on your complete architecture. If you have just 1 client for both modes, I don't think there is no added values in using a LocalBusinessService, because you will have a call to the find-method which is invoked on either the local business service implementation or a stub of the remote business service implementation

Kind regards,
Roel
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
What I meant was:

If your BusinessInterface already specified RemoteException, then this means that if you want to switch a NON-RMI networking approach, you´d also have to change your BusinessInterface
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4919
    
  10

Ah ok, now I get your question If you want to use another networking approach (which is not RMI) you'll have indeed to make changes to your business interface (or create a complete new interface). So you are spot-on with your assumption!

Kind regards,
Roel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange RMI - Error
 
Similar Threads
Enthuware doubt : Buisness Delegate
RMI Web Server?
Implementaion of the interface provided by Sun to handle local and remote connections
Can rmi server and client be in one java process
sending notification/events to client of RMI