开发者

How SingleInstanceService of Jnlp Api works?

As it is mentioned that SingleInstanceService allow applications launched under Java Web Start to register themselves as singletons, and to be passed in new parameter sets when user attempts to launch new instances of them.

How it works ?

We register listners to the service once and it won't allow it to create another instance.but basiclly how it works that i am not getting.

SingleInstanceService sis; 
    ... 

    try { 
        sis = (SingleInstanceService)ServiceManager.lookup("javax.jnlp.SingleInstanceService");
    } catch (UnavailableServiceException e) { sis=null; }

    ...


    // Register the single instance listener at the start of your application

    SISListener sisL = new SISListener();
    sis.addSingleInstanceListener(sisL);

    ...


    // Remember to remove the listener before your application exits

    sis.removeSingleInstanceListener(sisL);
    System.exit(0);


    // Implement the SingleInstanceListener for your application

    class SISListener implements SingleInstanceListener {
        public void newActivation(String[] params) {

            // your code to handle the new arguments here

        开发者_开发问答    ...
        }
    }

what i want to know is that how it won't allow another instance once we have bind our application with SingleInstanceListener ?


This is old but there is no answer.

I found this post that helped me.

http://tech.chitgoks.com/tag/singleinstancelistener/

Basically your running java process will have the newActivation() method called, and you can do what you like, open another window, show an error dialog, etc. Note that if you call System.exit() it will end your singleton running process. I recommend registering a shutdown hook for removal as well...

 Runnable r = new Runnable()
 {

            public void run()
            {
                SingleInstanceService       isis  = null;
                try { 
                    isis = (SingleInstanceService) ServiceManager.lookup("javax.jnlp.SingleInstanceService");
                    isis.removeSingleInstanceListener(this);
                }
                catch(Throwable t)
                {
                    //...
                }
            }
 }; 
        Runtime.getRuntime().addShutdownHook(new Thread(r, "shutdown singleton instance"));


If you pay some attention to the logs you will find hints of the mechanism. If you start the application (while having activating the single instance listener) you will see a log that it says it opens a server socket at a semi-random port!

When a second instance is launched, before doing anything, it tries to connect to the same port (as a client this time). If the connection is a success, then it knows that another instance is already running and so it just passes the former the arguments (probably via the same connection, not sure about this)

If it cannot connect, it knows that it is the first instance (or the previous one has closed) and it launches the application as normal.

This is a well known trick to enforce single instances of applications. You can of course implemented it yourself, it is just that the SingleInstance capability has it pre-implemented for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜