1 Reply Latest reply: Apr 3, 2013 3:25 PM by jschellSomeoneStoleMyAlias RSS

    Using RMI to wrap a process that is not thread safe

    1000830
      I have a multi-threaded application but one function (A) I need to call, that is provided by a third party, is not thread safe. I need to make parallel calls to function (A) so my only option is to start multiple process that I can call from each of my main applications threads.

      To do this I created a RMI interface between a the client and the remote process. On the server side I start multiple processes each assigned a different port. On the client side I have a queue manager that connects to the processes I started and builds a queue of them. It passes the client to the threads as needed so they can function in parallel.

      I am new to Java development and have the following questions:
      1. Each of the server processes require an init function to be run before providing the service. How do I setup the server processes to continue to run in memory listening for a a service request after running the init processes.
      2. Is there a better implementation of what I am trying to do?

      Thanks,
        • 1. Re: Using RMI to wrap a process that is not thread safe
          jschellSomeoneStoleMyAlias
          so my only option is to start multiple process
          Presumably you really do me a "process" in that the OS will be running multiple instances of the executable.
          Each of the server processes require an init function to be run before providing the service. How do I setup the server processes to continue to run in memory listening for a a service request after running the init processes.
          Your request handling proceeds as follows.
          1. Is the process already in the collection (a static collection you keep somewhere.) If so get it.
          2. If no, then create it, call init() put it in the collection.
          3. Now run the request with the process which you will have either from step 1 or 2.
          2. Is there a better implementation of what I am trying to do?
          You way is probably best. There are different ways to do it and other considerations - those follow.

          You only need one port. Your caller(s) shouldn't care how many are running. They just send a request and get a response. Your server hands off the request to the next available process.

          Presuming processing takes a long time otherwise why not just queue up the requests and run for one process?

          You can provide configuration values to
          1. Start a minimum number on server start up. This is advisable if init takes a while.
          2. Stop some if they are idle for a set period of time
          3. Have a maximum after which requests are rejected or queued (last part could be configured as well.)

          The process might just stop or might return an error that indicates it should be stopped. If so code for that and then remove it from the collection.

          Insure that the server shuts down gracefully and it MUST stop the running processes.

          Log the following
          - When you start a process
          - When you stop a process. And why it was stopped: idle or error
          - When you get a request.
          - How long it took to process the request
          - If the request result was an error or not.