• 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

RMI method invocation return value

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,

i am testing my rmi functionality and have the following problem:

i start my server:

my client connects:

then when i invoke some void method like bookRoom:

there is no problem, but when i should receive value:

my list stays null??? so i become later a NullPointerException, if i try to pick up the result.
(the method public List<Room> searchRoom(...) returns a proper list, i've checked it.)
The Room object implements Serializable

What could be the problem?
If someone wants, i can post here the complete exception trace.

Thanks.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Radi!

Champion, it's kindda weird, because if you are able to invoke a method that returns void without any problem, then you should also be able to invoke a method that returns something. By the way, you may consider always verifying if the arguments that you receive in your business methods are null; if so, you can throw an IllegalArgumentException. For instance, if these services are to be used out of the scope of this application, then they're already prepared. This makes your code stronger and more reusable!
Now, back to your problem... could you please post the stack trace for us to have an idea of what's going on? Meanwhile, I'll try to think about what could be wrong here.
 
Radi Hadzhiyski
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Wow i found something...but what it is, i dont know
my business method finds the Room objects and puts them into the list
(i've checked that, the list i not empty when i invoke the method local)
in my gui i have something like result = remoteObj.searchRoom(...)
now i see that result contains my Room objects, but when i use roomObj.getHotelName a become
the Exception, so my Room object is empty, but actually thats my search method:

 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it looks like there's something wrong with your search algorithm, but the server is retrieving data!
Now, your searchRooms() method is a business method, right? You may consider throwing only a business exception and not throwing RemoteException, Because this would imply that the remote server is being used, and whoever is using it would know that a remote server is being used. If you use only your business interface and not include infrastructure details (such as RemoteException), you could promote more abstraction and more reusability.
 
Radi Hadzhiyski
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi

i found the problem

it was because i have Logger that is transient, so i need to override the readObject by de-serialization.
So this problem is gone now, but:

about my BusinessLayer:

ich have BusinessLaye that is implemented by BusinessLayerImpl
and when i start networked client i have BusinessLayer obj = Naming.lookup....
and when i start the standalone mode i have BusinessLayer obj = new BusinessLayerImpl();

the other way is to have two interfaces + 2 Impl classes that do the same thing,
except the remote one extends UniCast... and the local doesn't extends UniCast.. and the
interface of the local doesn't extends Remote (so the methods of the local interface dont have to throw RemoteException).

But then i will have 2 identical interface+class relation, except the RemoteExceptions. Is that better?
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Radi!

But then i will have 2 identical interface+class relation, except the RemoteExceptions. Is that better?



Well, not really

It would be better to have only one business interface, and have two implementations of it. I'd just add some mechanism to retrieve the implementation to be used by the client of this interface (i.e. a factory), so that the client does not know where the data is being retrieved, and then instead of saying "BusinessLayer obj = Naming.lookup...", you'd get the implementation from this factory, because if you are saying "Naming.lookup", then the client of this interface knows that the data is coming from a remote server. If you could say "BusinessLayer obj = BusinessLayerFactory.getInstance()", then you could promote more abstraction and more reusability. This way, if there's a third implementation of it in the future, and you decide to use it, then the code of the client of the BusinessLayer interface does not have to be changed.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radi,

My solution had 1 BusinnessLayer-interface and 2 implementations, 1 for local use and 1 for network use.

Sample code looks like this:


in your client you will be able to do something like this:

I believe such an approach keeps your code easy and simple.

Kind regards,
Roel

[edit] oops, Roberto was a tiny bit faster
[edit] added required BusinessServiceRemote-interface (remark Radi)
 
Radi Hadzhiyski
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,

is it Ok, when i have the same like you Roel, but with one more interface like this:

 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:[edit] oops, Roberto was a tiny bit faster



But that's ok, because... well, you know what they say about great minds

Just one observation on Roel's code:



Hum... the business interface could throw only its own exceptions... having a method throwing RemoteException implies that the data is being retrieved from a remote server. One thing that could be done here is:



This way, the client of the BusinessService interface does not know where the data is being retrieved from! You can also create an hierarchy of exceptions, each exception for a particular problem.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radi,

I was wrong I had indeed (like you showed) 2 interfaces, because the remote interface has to extend Remote, otherwise you will have a ClassCastException. I tried all the different possibilities when i was experimenting with the RMI solution of my assignment. The thread can be found here. So you definitively need 2 interfaces (BusinessLayer and RemoteBusinessLayer). I will update my previous post too, so other javaranchers won't be mislead).

@Roberto: about your observation on my code. If you have a thick client, you can have a business layer at the client, wrapping the remote exceptions in the business exception (like you demonstrated with your code snippet). If you (like I did) are making a thin client, your business layer will be the server and so you are required to have RemoteException in the throws-clause of each method, otherwise you will have trouble running your server: RemoteException("remote object implements illegal remote interface")

Kind regards,
Roel
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Hi Radi,
My solution had 1 BusinnessLayer-interface and 2 implementations, 1 for local use and 1 for network use.
Sample code looks like this:


I'm thinking about this approach!
However, I see a problem! The client must to catch a exception that never will be thrown and never would do sense to it.

So, I think that would be better way following :

So, ww can do:

and...


What do you think, about it??
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Johnny,

Did you try if your provided solution compiles? I don't think so, because my compiler gives me following error

And I don't have to use a compiler, because I know that wasn't a valid method override, and rules about valid method overrides and overloads is one of the key points of the SCJP-exam (in my opinion).

And in my code I have following lines (like in the example code above):

So you don't know which service will be retrieved. Because it is determined at runtime, depending on the program argument is used: "alone" or none argument (= "networked"). So my client code for networked and stand-alone mode is exactly the same, not seperate like you suggested.

Kind regards,
Roel
 
Johnny Barbosa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Did you try if your provided solution compiles?



Sorry, I forget to edit the code correctly! Sorry, again! I edit and did the correction.
Yes, I understood your point of view. But, I think that are clients totally differents (alone and connected), which would must to keep controls different.
If you catch RemoteExceptions in code that never would need them, you code would be semantically incorrect.

However, in each approach there is a cost to pay.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Johnny,

Agreed that if the 2 clients (network and stand-alone) are different another approach would be more appropriate, but in this assignment they are completely the same. I speak of course for my provided solution, but depending on the instructions the only difference would be the configuration settings you have to enter.

And there is yet another approach possible:

Your local implementation will implement BusinessServiceLocal and the network implementation will implement BusinessServiceRemote, I think that 's quiet obvious



Kind regards,
Roel
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on RMI now and having this familiar problem with how to handle RemoteException. As it is not thrown the supplied Sun interface DBMain I cannot throw it in my RMI Data Implementation class.

I found this old thread and saw Roel's and others suggestions to have 2 interfaces BusinessService and BusinessServiceRemote each of which are implemented by differemt classes one for local and one for remote.

What I cant wrap my head around is exactly where these Business services fit in with the Sun interface DBMain and required implementation Data.java? I currently have Data class implementing my own interface ExtendedDbMain which extends DBMain and my Connector class returns a data object.

If Roel or someone with similar solution or implementation has some advice to point me in the right direction on this would be appreciated

Leah
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leah,

I'm kind of living on this forum, so here's the answer. It's pretty easy. This is where the BusinessService (could) come into play:

GUI --> BusinessService --> Data

So the Gui invokes the business service methods. And the business service invokes the methods from the sun interface you have to implement (and thus has a reference to your Data class).

Hope it helps.
Kind regards,
Roel
 
Leah Knowles
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel

Yes noticed you are here a lot which is great for us "learners".

I thinking I'm slowly getting it. So would it be accurate to say that the Business Service implementation is referencing Data class via composition?

Also are you using some connector class to pass a reference of your business Service Implementation back to the GUI? Sorry if asking you divulge too much detail about your implementation.

Leah
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leah Knowles wrote:Yes noticed you are here a lot which is great for us "learners".


Just 6-7 months ago, I was also a "learner"

Leah Knowles wrote:So would it be accurate to say that the Business Service implementation is referencing Data class via composition?




Leah Knowles wrote:Also are you using some connector class to pass a reference of your business Service Implementation back to the GUI?


To get a reference to my business service implementation I took the same approach as in Andrew's book, so yes indeed I used 2 connector classes.

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still a learner. When I saw Philip Wadler's workshop in this year's ECOOP, I noticed that there's still a very long road ahead...
 
reply
    Bookmark Topic Watch Topic
  • New Topic