Jnlp Api的SingleInstanceService如何工作? [英] How SingleInstanceService of Jnlp Api works?

查看:67
本文介绍了Jnlp Api的SingleInstanceService如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如前所述,SingleInstanceService允许在Java Web Start下启动的应用程序将自己注册为单例,并在用户尝试启动它们的新实例时传递给新的参数集.

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.

它如何工作?

我们只向服务注册一次列表器,它不允许它创建另一个实例.但是基本上我没有得到它的工作原理.

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

            ...
        }
    }

我想知道的是,一旦我们将应用程序与SingleInstanceListener绑定在一起,它将如何不允许另一个实例?

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

推荐答案

如果您对日志有所关注,则会发现该机制的提示. 如果启动该应用程序(在激活了单实例侦听器的同时),您将看到一条日志,其中显示它在半随机端口上打开了服务器套接字

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!

启动第二个实例后,在做任何事情之前,它会尝试连接到同一端口(这次是作为 client ).如果连接成功,则它知道另一个实例已经在运行,因此它只是将前一个参数传递给了它(可能是通过相同的连接,对此不确定)

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.

这是实施单个应用程序实例的众所周知的技巧.您当然可以自己实现,只是SingleInstance功能已为您预先实现.

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.

这篇关于Jnlp Api的SingleInstanceService如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆