How SingleInstanceService of Jnlp Api works?

感情迁移 提交于 2019-12-10 23:39:12

问题


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 ?


回答1:


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"));



回答2:


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.



来源:https://stackoverflow.com/questions/4668185/how-singleinstanceservice-of-jnlp-api-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!