Jnlp Api的SingleInstanceService是如何工作的?
正如前面提到的,SingleInstanceService 允许在 Java Web Start 下启动的应用程序将自身注册为单例,并在用户尝试启动它们的新实例时传递新的参数集。
它是如何运作的?
我们将侦听器注册到该服务一次,它就不允许它创建另一个实例。但基本上我不知道它是如何工作的。
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 绑定,它如何不允许另一个实例?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很旧,但没有答案。
我发现这篇文章对我有帮助。
基本上
你正在运行的java进程会调用
newActivation()
方法,您可以做您喜欢的事情,打开另一个窗口,显示错误对话框等。请注意,如果您调用System.exit()
它将结束你的单例运行进程。我建议也注册一个关闭挂钩以进行删除......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 callSystem.exit()
it will end your singleton running process. I recommend registering a shutdown hook for removal as well...如果您留意日志,您会发现该机制的提示。
如果您启动应用程序(同时激活单实例侦听器),您将看到一条日志,表明它在半随机端口打开了一个服务器套接字!
当第二个实例启动时,在执行任何操作之前,它会尝试连接到同一端口(这次作为客户端)。如果连接成功,则它知道另一个实例已经在运行,因此它只是传递前一个实例的参数(可能通过同一连接,对此不确定)
如果无法连接,则它知道它是第一个实例(或者前一个已关闭)并且它会正常启动应用程序。
这是一个众所周知的强制应用程序单实例的技巧。你当然可以自己实现,只是 SingleInstance 能力已经为你预先实现了。
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.