• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

NX: RMI and Mutlithreading

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Flavio Nobili
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume your 2 clients are executing in different threads or processes.
Is this correct ?
 
Flavio Nobili
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't we take care the host information ? Thanks Ken.
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Flavio Nobili
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
Thanks for the help. It works this way, I must have done something wrong.

Regards,
Fl�vio.
 
Let's go to the waterfront with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic