java single instance_如何确保只能执行一个java程序的一个实例?

该博客介绍了一种使用Java的ManagementFactory类检查同一台机器上是否存在相同应用程序的多个实例的方法。通过获取运行时MXBean并解析进程ID,然后利用MonitoredHost和MonitoredVm接口查找具有相同命令行参数的其他JVM进程,从而判断是否为第二个应用实例。如果发现存在其他实例,程序将显示消息并终止,否则启动应用程序。
摘要由CSDN通过智能技术生成

您可以使用ManagementFactory对象.从

here:

import sun.management.ConnectorAddressLink;

import sun.jvmstat.monitor.HostIdentifier;

import sun.jvmstat.monitor.Monitor;

import sun.jvmstat.monitor.MonitoredHost;

import sun.jvmstat.monitor.MonitoredVm;

import sun.jvmstat.monitor.MonitoredVmUtil;

import sun.jvmstat.monitor.MonitorException;

import sun.jvmstat.monitor.VmIdentifier;

public static void main(String args[]) {

/* The method ManagementFactory.getRuntimeMXBean() returns an identifier with applcation PID

in the Sun JVM, but each jvm may have you own implementation.

So in anothers jvm, other than Sun, this code may not work., :(

*/

RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();

final int runtimePid = Integer.parseInt(rt.getName().substring(0,rt.getName().indexOf("@")));

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

// If exists another instance, show message and terminates the current instance.

// Otherwise starts application.

if (getMonitoredVMs(runtimePid))

{

new MainFrame().setVisible(true);

} else

JOptionPane.showMessageDialog(null,"There is another instance of this application running.");

}

});

}

The getMonitoredVMs(int processPid) method receives as paramter the

current application PID, and catch the application name that is called

from command line, for example, the application was started from

c:\java\app\test.jar path, then the value variable is

“c:\java\app\teste.jar”. This way, we will catch just application name

on the line 17 of the code below. After that, we search JVM for

antoher process with the same name, if we found it and the application

PID is different, it means that is the second application instance.

private static boolean getMonitoredVMs(int processPid) {

MonitoredHost host;

Set vms;

try {

host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));

vms = host.activeVms();

} catch (java.net.URISyntaxException sx) {

throw new InternalError(sx.getMessage());

} catch (MonitorException mx) {

throw new InternalError(mx.getMessage());

}

MonitoredVm mvm = null;

String processName = null;

try{

mvm = host.getMonitoredVm(new VmIdentifier(String.valueOf(processPid)));

processName = MonitoredVmUtil.commandLine(mvm);

processName = processName.substring(processName.lastIndexOf("\\") + 1,processName.length());

mvm.detach();

} catch (Exception ex) {

}

// This line is just to verify the process name. It can be removed.

JOptionPane.showMessageDialog(null,processName);

for (Object vmid: vms) {

if (vmid instanceof Integer) {

int pid = ((Integer) vmid).intValue();

String name = vmid.toString(); // default to pid if name not available

try {

mvm = host.getMonitoredVm(new VmIdentifier(name));

// use the command line as the display name

name = MonitoredVmUtil.commandLine(mvm);

name = name.substring(name.lastIndexOf("\\")+1,name.length());

mvm.detach();

if ((name.equalsIgnoreCase(processName)) && (processPid != pid))

return false;

} catch (Exception x) {

// ignore

}

}

}

return true;

}

The javax.jnlp.SingleInstanceService provides a set of methods for applications to register themselves as singletons, and to register listener(s) for handling arguments passed in from different instances of applications.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值