• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

Java RMI

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!
  • 6
  • 6
  • 5
2 Solutions
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.

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.
amp834Author Commented:
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)
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
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.
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
amp834Author Commented:
>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 is the server, the machine on wants to discover this information w/o having to know about .15.
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
amp834Author Commented:
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)

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).  
amp834Author Commented:
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.
Don't know about stndard techniques. And from some talks on the web, I assume that
thwere is probably no widespread systeme for that:

 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
amp834Author Commented:
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?
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.
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.
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
amp834Author Commented:
Thanks, again, for your help, insights, and suggestions.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 6
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now