aspose file tools*
The moose likes Distributed Java and the fly likes Problem with pass-by-reference ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Problem with pass-by-reference ?" Watch "Problem with pass-by-reference ?" New topic
Author

Problem with pass-by-reference ?

Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
hi everybody,

when i m trying to get the list of clients(vectors or linkedList/arraylist/List) from server object in my application logic remote object to update the clients. i m getting 0 size of list and i can'T iterate through that.

is there some typical procedure/restriction in RMI to handle this problem.

this is the code:



this is class where i wanted to access the same list :



i m calling the register() from some client(swing GUI) and sending the client as argument to add in that list.
after adding to the list i m calling getClientname() from messageImpl and it should show one element in messageimpl class but it is not.
i tried lot of ways to handle this like vector/ lists and custom class "serverengine" also but i m still getting 0 elements in the list in the messageimpl class.
load of thanks for any help.
jibby
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Yes, Java uses pass-by-reference. But not with RMI though. In RMI, all method arguments and return values are serialized/deserialized. In essence a copy is made.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Hi,

Welcome to JavaRanch!

First, I have to say this code sample is terribly hard to follow. You've got so many methods and variables with similar names, redundant member variables, some static, some not, and random letter-case. It's almost physically painful to read. You seriously need to pick a coding convention and learn to follow it!

Second, as Rob says, objects in RMI may (or may not) be serialized and copied when passed as remote method arguments. Plain, unexported objects are copied; servant objects are passed by (remote) reference, though.

I suspect -- I don't know for sure, as you're not showing all your code (and it would hurt my brain to read it if you did!) -- but I suspect that the problem is what Rob's getting at: your MessageImpl class in the client is ultimately looking at the static variable in a local copy of the ServerServicesImpl class, not the remote instance which actually has a callback registered. In the client, is your instance of MessageImpl a remote object, or a local object?


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
Too difficult a question for "beginning". Moving thread.
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
great thanks for reply and welcoming Ernest Friedman-Hill

although messageImpl is remoteobject but that is instantiating in client like this:



i m really lost in this problem and that is reflecting on code also that i started hit and try approach .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Jibby Lala wrote: thanks for reply and welcoming Ernest Friedman-Hill

although messageImpl is remoteobject but that is instantiating in client like this:



OK, that's the problem. Although MessageImpl implements UnicastRemoteObject, your client is not using it as a remote object, but rather creating its own local copy. That local copy on the client doesn't have access to the static variable on the server, which is in another JVM altogether, or maybe even on another computer.

For this to work, the client would need to be talking remotely to a MessageImpl that was exported from the server.
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
as far as my understanding

you are saying to instantiate messageimpl like this :



and bind that to RMI also :



am i right ?
thnks

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Yes, but in the server JVM, not the client one; the client one would have to use Naming.lookup() to find it.

I realize this may be completely incompatible with what you're trying to implement, but the fact remains that for an object to be able to have direct access to a static variable in the server VM, then it has to physically exist there, in the server VM.

If MessageImpl objects are going to be sent back and forth between the client and server, then while the object is in the server, the code that tries to access the server variable should indeed work. But while the object is on the client, it obviously won't work.
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
Now if i want now another object/objects like Message then i have to do the same procedure for every object ,there isn't any other way to do that and that will not lead me to this horrible problem. and in which i don't need to repeat this whole RMI procedure for every object.
thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

So far, I've been trying to help you solve your immediate problem, without talking about your overall design. Maybe I should have talked about the design first, because the problem you're having shouldn't really come up.

A simple chat system would have just two remote interfaces: a server class with just a few methods like this:



and a Callback interface like



The "message" class could contain just a String to hold the message text, or it could include fancier stuff like a list of client names to send a private chat message to, icons, etc. Message is just a plain old Java class though -- it's not remote. It also needs to be totally self contained. It knows nothing about Callbacks, and nothing about ChatServers. On the contrary, it's the ChatServer and Client implementations that call methods on Messages, extract data from them, and operate on them. Messages need to be sent back and forth over the wire so they should not include references to any other objects or any machine-dependent paths or resources.

You'd have a server class that implements the server interface, and is exported via Naming.rebind(). The client would call connect(). The implementation of connect() would save remote references to callbacks in a non-static List member. sendMessage() would loop over those callbacks and send the Message to each Callback by calling receive() and passing the Message to it. sendMessage() might examine the Message to decide which clients to send the message to. disconnect() could search the list and remove the Callback.

If you want your clients to get other information from the server, then just add methods to the ChatServer class. For example, getUsers() would return a list of the names of the connected users; the server could get this list by iterating over the Callback list and calling a method like getName() on each Callback. There's never a need for Message or Callback or any other class to access the server's private data directly; just add remote methods that the client can call as needed.




Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
well first i just wanted to give you BIG HUG for such a commendable volunteering,

i didn't get that exactly because you are not the mentioning serialized object what i wanted to confirm as replacement of remoteobject,

The "message" class could contain just a String to hold the message text, or it could include fancier stuff like a list of client names to send a private chat message to, icons, etc. Message is just a plain old Java class though -- it's not remote. It also needs to be totally self contained. It knows nothing about Callbacks, and nothing about ChatServers. On the contrary, it's the ChatServer and Client implementations that call methods on Messages, extract data from them, and operate on them. Messages need to be sent back and forth over the wire so they should not include references to any other objects or any machine-dependent paths or resources.


secondly, yes i looked at this model or similar what you sent at several tutorial and example, but my goal was to come one with some new model and to achieve DECOUPLING(the client list should sent tO logic and logic do what ever it wants and then call updateclient () method in it and that' what i tried to do ),

The chat applicaiton was just a really simple use case for that to saw the implications, I have to keep in mind other applcaition which could have lengthy logic, i saw some tutorial in which they were using more then two remote interfaces,so i was trying to come up with similar model like one for server stuff as you said, one for application logic like the "messageimpl" remote object and one for callback but as we saw there is flaw in that because the business logic would consist of several objects and it seem infeasible to implement remote interface and formatlities for every object.


what if i made messageimpl class not remote and instead make it serializable? would it work without the problem what i faced and as desired decoupling requirement.

thanks
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
well, it will also work by just creating the remote instance of ServerServicesImpl in messageimpl class and making message class serializable instead of remote.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with pass-by-reference ?
 
Similar Threads
Help manipulating Swing default ButtonModel remotely. (NPE on the second client)
Serialized object or Remote object ?
Implementing multiuser interactive animation with RMI callbacks.
Very New (obviously) - Program terminates
Difference between ArrayList and Vector