aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: RMI and Mutlithreading 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 "NX: RMI and Mutlithreading" Watch "NX: RMI and Mutlithreading" New topic
Author

NX: RMI and Mutlithreading

Flavio Nobili
Ranch Hand

Joined: Jan 28, 2004
Posts: 58
Hello to all,
I've started working on the SCJD assignment, and found some problems using RMI. Multithread access to the database must be used, doesn't it?
But, is an instance of a remote object, gotten from the server (RMI), a thread?

I've built an simple example using RMI with a remote class that have just one method which prints a number on the screen infinitely. And when I made two clients to run that method printing different numbers, only one number was printed. The same happened when I tried to use different instances for each client.
So, how can I build a multithread database access using RMI???

Regards,
Flavio.


Java Developer
SCJP, SCJD, SCWCD, SCBCD
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Flavio,
Welcome to the ranch.
It's hard for me to tell without looking at your code. My best guess is that your clients are using the rmi server object directly instead of a stub to it. Does your code do something like this to get a stub ?


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Flavio Nobili
Ranch Hand

Joined: Jan 28, 2004
Posts: 58
In an remote object I have the following code:


This object is gotten from another object, a factory:


Than, on the client side, I run two clients, one executing the remote method passing a number, and the other passing another number:


But, the first execution will never let the second one runs, printing the first number infinitely. So, it's not a multithread execution.
Regards,
Flavio.
Xie Ruchang
Ranch Hand

Joined: Dec 25, 2003
Posts: 160
Hi,
You are not giving the other thread a CHANCE to execute, put a sleep statement in your infinite loop and you will see a difference.
Best Regards
Flavio Nobili
Ranch Hand

Joined: Jan 28, 2004
Posts: 58
Ok, you are right. I've put the thread's sleep method now, but even then only the first remote execution runs.
I've built this example just to lern how does RMI work. And it seems to not implement multithreading executions automatically. Is it right?
Regards,
Flavio.
Xie Ruchang
Ranch Hand

Joined: Dec 25, 2003
Posts: 160
Hi,
No, RMI automatically handles the multithreading for its methods exposed via the interface. This is a strong point for using RMI. In my assignment, I didn't use RMI. I implemented a multithreaded server myself to do fine control of the server like on-the-fly shutdown and start-up, tracking client IP and port#. Think I overkilled in doing this.
Best Regards
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
I assume your 2 clients are executing in different threads or processes.
Is this correct ?
Flavio Nobili
Ranch Hand

Joined: Jan 28, 2004
Posts: 58
Hi,
I took it from the RMI specification:

Thread Usage in Remote Method Invocations
A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. The RMI runtime makes no guarantees with respect to mapping remote object invocations to threads. Since remote method invocation on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread-safe.

I think it explains what is happening.

Thanks,
Flavio.
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Hi, Fl�vio, I don't know what's matter in your segment, now I'm also working on RMI, here's a very simple example like yours, it display the string every client send to the server.
the remote interface:
the implement class:
the server:
the client:
Every time you pass a string to main method, you can get different result!
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Hi, Ken
I have a question need your help. ---- In our assignment, you know, require to use JRMP RMI and shouldn't use automatic class loader, this means we may not use policy file at all, exact?
As I know, to run the RMI server, there are several way:
a) as the example above, must use rmiregistry.exe, host:identified, protocol: rmi.
b)as Andrew's example, LocateRegistry.createRegistry(1234);Naming.rebind("//:1234/factory", new ConnectionFactoryImpl()); don't use rmiregistry.exe, host:null, port: 1234, protocol: null.
c) and sun support example in SCJD:BrokerModel brokerModel = new BrokerModelDbImpl(dbHost);Naming.rebind("//localhost/TheBroker", brokerModel); don't test, host:identify, port:null, protocol:null.
I don't know these three ways which is best and which was you selected ? Thanks a lot.
[ January 31, 2004: Message edited by: Leo Tien ]
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Hi leo,
I chose method #2 as I didn't want the user to have to do anything with rmiregistry.exe and I did want the port no. to be configurable.
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Shouldn't we take care the host information ? Thanks Ken.
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Leo,
Take a look at the Javadocs for Naming and LocateRegistry classes. IMO the docs aren't particularly well written in that they don't make it clear that Naming simply provides static convenience methods for working with an existing registry running on some machine at some port.
LocateRegistry can be used to programmatically create a registry running on a specific port into which named RMI server objects can be bound. This registry, is created on the machine on which it is invoked therefore there is no need to specify the host in the createRegistry methods.
To access any particular rmi server object, any client will need to know:
1. the host (if not localhost)
2. the port no. (if not the default 1099)
3. the name the rmi server object is bound to

I hope that clarifies my previous comment.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Hi Fl�vio
I took it from the RMI specification:
Thread Usage in Remote Method Invocations
A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. The RMI runtime makes no guarantees with respect to mapping remote object invocations to threads. Since remote method invocation on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread-safe.

I think it explains what is happening.

No - this is a totally different issue. As everyone here is telling you, RMI should be setting up the multiple threads for you.
I build a complete application out of the snippets you provided and tested them, and I saw that both threads were indeed running. Perhaps you could compare it to your program and see what I did differently? Or you could post your complete code here so that we can look at it.
My code (note that this is all in a single file called Flavio.java - I am too lazy to create and compile all those separate files ):

And when I run it, I get:

If I take out the sleep() statement, then I might get 50 instances of '0' before I see the first instance of '1', however I do get them eventually.
Regards, Andrew
[ February 01, 2004: Message edited by: Andrew Monkhouse ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Flavio Nobili
Ranch Hand

Joined: Jan 28, 2004
Posts: 58
Hi Andrew,
Thanks for the help. It works this way, I must have done something wrong.

Regards,
Fl�vio.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: RMI and Mutlithreading