5 Replies Latest reply: Mar 1, 2013 3:44 PM by baftos RSS

    Multiple instances

      How do I avoid more than one instance of my java web start application to start up ?

        • 1. Re: Multiple instances
          A common trick is to open a server socket. The second time that wont work on the same port.
          • 2. Re: Multiple instances
            But if you pick a fixed port for your application, how can you be sure, that no other application is using that port ?
            • 3. Re: Multiple instances
              oajamfibia wrote:
              But if you pick a fixed port for your application, how can you be sure, that no other application is using that port ?
              This is one drawback of the listening port. The other is that it does not handle well multiple simultaneous users.
              Use http://docs.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/examples.html#SingleInstanceService instead.
              • 4. Re: Multiple instances
                The problem with SingleInstanceService is that it doesn't work either. See this message:

                SingleInstanceService does not work.
                • 5. Re: Multiple instances
                  Looks like a JWS bug to me, but I don't cross my fingers that it will be solved soon.

                  I can tell you how my stand-alone (not JWS) application works. It has been formally tested in Windows, Linux and Mac OSX and there are customers (around 100) using it for a few years and we never had a complaint about the single instance.
                  I don't think I can provide code, as it belongs to the company. The idea is a variation on the listening port theme, it's a bit complex and there are some unlikely cases when it may fail.
                  In a nutshell, have a hardcoded list of ports, let's say 20 of them. Try to listen on them all in a loop. If you succeed, don't stop. Once you reach the end of the loop, grab the first one. If it's been taken while you were looping, see bellow.
                  If one of the ports is already taken, it's your application or some other random application. Connect to it. Wait for, let's say, 50 milliseconds. If you receive something that you expect, like your hardcoded application signature, exit, because another instance is running.
                  If you receive nothing or something unexpected, disconnect and keep looping. Cross your fingers that the server you connected to will not go bananas. Now a twist: the signature should also contain the user name, because you want to allow two instances if they run on behaf of different users.

                  A simpler approach can be based on the FileLock class on a file in the user home directory, but there are limitations. Google for FileLock single instance.

                  And yet another. If you target only Windows, EXE4J does it for you, probably by using the very reliable native methods.

                  Edited by: baftos on Mar 1, 2013 4:43 PM