[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java RMI

Posted on 2011-05-10
17
Medium Priority
?
634 Views
Last Modified: 2012-05-11
I'm looking at Java's RMI and am missing a few key points that hopefully others can answer easily.  (I've read about 20-30 different articles and haven't figured these things yet)

1. When an RMI server program runs on machine A and registers itself, what is the mechanism.  I see a "java.exe" in the task list for it.  Is that the java that ran the server program (so I could set the JVM parameters for the server by tweaking how I invoke server.jar?)

2. Can multiple clients connect to the same server, and if so, for simple servers that run quickly (a time server, for example, or an echo server), does it run serially, or are there locks and things I should be aware of?  

3. For more complex processing, do you create a new thread or a new process and hook up the process to the client?  (some sample code would be useful here)

4. How do you terminate the server if necessary (right now, my best solution is to have a "serverStop" routine as one of the interface routines)


I appreciate any and all help, and will divide the points among all useful answers!
0
Comment
Question by:amp834
  • 6
  • 6
  • 5
17 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 35733982
1. Yes, you start server with normal java command as normla java program
2.yes, mutiple clients can connect, I did connect and had no issue about locking, unless
maybe you want towrite say one file - I didn't have any issues
3.If I needed to write a temp file I made sure it will have specific name - say adding time_stamp
to the name; you can have client comminicate its name, but it was usually not necessary - you
just call the method and it takes care of it - don't know perhaps it starts
separate theread by itself; at least I use it a lot, and never experienced any issues.
4. I just kill the process on unix, but you can say make it check some file from time to time
and change some value there, if you want it more organized.

In general, I had very good experience with RMI  - seems to me much better and easier and
more convenient than web services, and all that stuff.

0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 35734019
1. there is a server and a registry, you can specify whatever options you want when you start up server from command line same as any other java app
2. multiple clients can connect so make sure your server code is thread safe
3. same logic applies here as with any other application, if you need another thread then create one. For example if its a swing client then you'll need another thread to make your rmi calls
4. you can terminate the same way you terminate any java application. your serverStop is a good way.
0
 

Author Comment

by:amp834
ID: 35734097
Thanks for the quick replies, they answer most of my questions.

I now realize I was unclear about #3.  I meant the server needing to create a thread or process.  (The info about the client creating a thread if it's in a swing thread was bonus info!  Thanks).

Also, for #3, can you create a separate process as well?  This leads to question 5 (and possible another question depending on the answer)

5. For something like a database server (or actually, to serialize a full-text indexer, so multiple clients can write to the same "database" simultaneously), I may want to create a separate process for each database (since the clients may write 100's of gigabytes of data), or some other way of balancing the load if the same machine is servicing 5 or 6 databases (and to isolate faults in one from affecting the other).  
Can you guys give some recommendations for this?
(I can have the main server be a "broker" that starts a new RMI server for each database, then would have to manage how to kill that server when it's not in use any more.  This may be too complex or very simple, I don't know)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 35734129
3. still the same, the server is no differnet to any app when it comes to threads.
Be aware that thread is already created for processing each request, thus the need for thread safety
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734158
I was raeding and writing to the  database in RMI server, but I usually opened and closed
connection all inside the method which was called  - didn't try to share connection, don't know if that would be
good.Different clients were doing it simulateneously without problems,
I guess database takes care of it. But I was never raeding or writing gigabytes, etc., so have no
expreience with that, or load balancing and this sophisticated stuff.
0
 
LVL 92

Expert Comment

by:objects
ID: 35734183
you wouldn't create a seperate process for each database. A single process can quite happily write to multiple databases. Load would be no different to accessing a single databsse
0
 

Author Comment

by:amp834
ID: 35740381
>Be aware that thread is already created for processing each request, thus the need for thread safety
This is good to know.  I didn't come across this information anywhere in my readings!

For now, I won't create separate processes.  But if it turns out I need to do so, just for process isolation (so a buggy server code for one client's data doesn't affect another client), I'd like to have a plan.

As far as finding a server on a local network, does RMI provide a way to do this?  (If not, I have another library that does multicasting, and I can use that).
For example, machine on 192.168.1.15 is the server, the machine on 192.168.1.20 wants to discover this information w/o having to know about .15.
0
 
LVL 92

Expert Comment

by:objects
ID: 35742524
it doesn't support discovery. What it uses is the registry that I mentioned above. The client connects to the reqistry to find any servers that are avaiulable
0
 

Author Comment

by:amp834
ID: 35747440
Thanks, I will use another library for discovery.

If this thread is getting too long, I can close this question and start a new one, but it all is related to the RMI.

Now I'm trying to figure out how to have a stateful server:  If the server is allocating server-local resources (File handles, database connections) for a specific client, how can it give that handle information to the client?

For example:
class ServerSession {
    java.io.File dbFile;
   Connection c1;
   //etc. additional state information
}

when a client calls the remote function    OpenDatabase(String filename)

the server creates a ServerSession (for that particular client), opens the resources, and needs to return a handle to this ServerSession.  

The best I can think of doing is to allocate an Object array, stash ServerSession in there, and return the index (an integer) in a data structure to the client.  The client must call with this handle #.  Then I'm left to make sure client1 doesn't use client2's handles, etc., and managing the array.

Are there other/standard solutions to this requirement?  (I could look at Derby's server code to see how they do it, but that would take some time to figure out!  And they are layered over JDBC, which may provide some services to make it easier)

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35747924
I am not sure, there is any specific provision for the session within the RMI.
Therefore, I believe your suggestion is reasonable - to maintain any session specific information on the server
and communicate the handle to client, who would use this handle on the next call, thus ensuring
access to its current state. Once you set up the system where client will uniquely identify itself in each call to the method, I don't see
much danger of one client using another client's session data (other then, some kind of the bug in the code).  
0
 

Author Comment

by:amp834
ID: 35748318
I didn't think there were any provisions within RMI, but that people have developed standard techniques for what seems to be a common requirement.

My method is unsafe because if a client uses an old handle by mistake, it would be writing to another database by mistake, a very difficult bug to find in a distributed system!  I may be able to store the client's ID in the handle to verify that it's the correct client.

There may be a method like with TCP/IP, where I can return a new RMI connection or handle for all subsequent requests to that particular database.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35748602
Don't know about stndard techniques. And from some talks on the web, I assume that
thwere is probably no widespread systeme for that:
http://stackoverflow.com/questions/5363183/rmi-client-tracking

 In general RMI is for some reason (undesrvedly form my point ov view) is
used less than all
this J2EE  stuff, web services, etc. so there are fewer standard techniques around, I guess.

The good thing in my opinion, that in RMI everithing is very transparent and everything both on the sevre and the client
 is in your hands (there is no myterious container with its own internal life) so it is all up to you
to make sure the client will not be using the old handle and you can include as many verifications and checks
for that as you want.

You can probably also somehow figure out something like IP address of the client
but I would not go that route - it would be less reliable; I'd rather invent my own system
to track the clients.
And that's whatbtey are discussing in the link above, even talking about Zookeeper,
which may help to track clients;
maybe you reallly want to have a look
0
 

Author Comment

by:amp834
ID: 35755924
Thanks, I will look at zookeeper.


A stateful server is typical (and it's what I need to do), where the client establishes a connection to a server and starts talking back and forth (like an FTP client would).  

I suppose I could see how stateful EJBs are implemented.  for_yan or objects, do you have any ideas how EJBs are implemented?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35755975
Yes, I was thinking about EJB when I was writing my last post.
EJB requires application server, so you ned JBoss or commercial server like
Weblogic;Tomcat, as far as I know, does not support EJB.
So if this session stuff is most critical, then you want to go that route.
But this is a whole dfferent animal, much difficult in learning, implementation, maintenance, etc.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35756028
RMI, as I understand, was mostly designed to run on another machine some procedure, which you cannot
run on this machine (say, you need to run wopme program on Unix but your client is working on PC),
 hence the name - Remote METHOD Invocation.
They are not even talking about remote objects - only about methods.
So if your session management is something simple that you can organize yourself, you get the
benefit of simplicity and of your own control without any intermediacy and black-boxedness
of the container, but if this  session infrastructure
is most critical, then probably you want to go with EJB.
0
 
LVL 92

Expert Comment

by:objects
ID: 35758733
All you need to do is pass a session id with each call.
The server can use that session id to lookup the session in a map

basically the same as how session management works anywhere
0
 

Author Closing Comment

by:amp834
ID: 35870221
Thanks, again, for your help, insights, and suggestions.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question