空间站

北极心空

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks
很巧合,今天恰好有朋友写信与我讨论这个问题,他是从网上看到关于这个问题的一篇文章,该文章提出使用socket来解决该问题。下面是我回信中的一些片断:

关于让Java程序只运行一个实例的问题,其实质是JVM之间通信的问题,因此自然会想到使用Socket来传递信号,但就像文章中提到的那样,如果程序在开始运行时ServerSocket监听的端口已经被其它程序占用,那么程序的运行就会受到影响。而且我个人认为仅仅为了判断程序运行的实例而开一个端口来侦听,有点杀鸡用牛刀的感觉。
我这里有一个方案可供选择,其原理是使用文件锁来锁定一个代表程序运行实例的文件,当程序启动时,首先锁定该文件,后继启动的实例一旦发现该文件已被锁定则提示出错信息。(为什么不直接使用文件而是使用文件锁来判断呢?即:在程序启动的时候生成一个文件而在程序退出时删除这个文件,只要判断该文件的存在与否就可以判断实例的运行情况。这是因为我们不能确保删除文件的操作一定能被执行到,程序是可能被强制关闭或异常退出的,而文件锁不同,它是作为系统资源分配给JVM的,一旦JVM当掉,其资源会一并被操作系统回收,因此对文件的锁定也会被消除。)

下面是该方案的源码。

import java.nio.channels.*;
import java.io.*;
import javax.swing.JOptionPane;

//应用实例控制类
public class InstanceControl
{
  FileLock lock
=null;
  
//判断该应用是否已启动
  public boolean isRunning()
  {
    
try
    {
      
//获得实例标志文件
      File flagFile=new File("instance");
      
//如果不存在就新建一个
      if(!flagFile.exists())flagFile.createNewFile();
      
//获得文件锁
      lock=new FileOutputStream("instance").getChannel().tryLock();
      
//返回空表示文件已被运行的实例锁定
      if(lock==null)return false;
    }
catch(Exception ex){ex.printStackTrace();}
    
return true;
  }

  
public static void main(String[] args)
  {
    InstanceControl ic
=new InstanceControl();
    
if(ic.isRunning())
      JOptionPane.showMessageDialog(
null,"已存在该程序的实例!","提示",JOptionPane.OK_OPTION);
    
else
      MainClass.main(args);            
  }
}

下面是这篇文章的原文


   让Java程序只运行一个实例                                                              
                                            出自:http://developer.ccidnet.com 梁邦勇 2003年01月12日 18:04                                            
一个程序可以在内存里面存在多个运行实例,比如,你可以打开多个微软的Word程序。但是,有些时候我们需要控制程序运行的实例只有一个,也就是说,该程序同一时
刻在内存里面运行的只有一个实例。这样当这个程序在内存中已经存在一个运行实例而用户又再次运行了该程序的时候,有两种结果,第一种结果是结束目前的运行实  
例,打开新运行的实例;第二种就是让新运行的实例退出,原有的运行实例继续运行。                                                                        
                                                                                                                                                      
                                                                         原理                                                                        
                                                                                                                                                      
                                                                                                                                                      
因为任何时候只有一个实例,所以在实现这种功能的时候必须借助只能被独享的资源。如果我们的程序是基于某个平台的,那么就可以借助操作系统的内核对象来完成,
比如Windows操作系统就提供了CreateMutex这个API来创建一个独享的内核对象。但是因为要考虑平台无关,Java程序的实例控制不应该使用系统的内核对象来完成,那  
么我们就必须找到其它的、可以独享的资源。实际上,一台机器无论是在什么操作系统上,网络端口都是独享的,也就是说基于网络端口这个独享的原理,我们可以很方
便地让我们的Java程序实现在内存里面只有一个运行实例这个功能,而且这个功能的实现是与平台无关的。                                                      
                                                                                                                                                      
                                                                         实现                                                                        
                                                                                                                                                      
                                                                                                                                                      
我们先来看看第一种情况是如何实现的,也就是说如果系统中已经存在运行实例的话,那么结束原有的运行实例,让新实例运行。这个实现实例控制的Java类也是一个线
程,具体的实现如下:                                                                                                                                
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
  import java.net.*;                                                                                                                                  
  public class InstanceControl extends Thread {                                                                                                      
   public void run() {                                                                                                                                
    try{                                                                                                                                              
     Socket sock = new Socket("127.0.0.1",22222);                                                                                                    
  //创建socket,连接22222端口                                                                                                      
    }                                                                                                                                                
    catch (Exception e)                                                                                                                              
    {}                                                                                                                                                
    try{                                                                                                                                              
     ServerSocket server = new ServerSocket(22222);//创建socket,在22222端口监听                                                                      
     server.accept(); //等待连接                                                                                                                      
     server.close(); //有连接到来,也就是说有新的实例                                                                                                
     System.exit(0); //这个实例退出                                                                                                                  
    }catch (Exception e)                                                                                                                              
    {                                                                                                                                                
     e.printStackTrace();                                                                                                                            
    }                                                                                                                                                
   }                                                                                                                                                  
  }                                                                                                                                                  
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
下面这个Java程序的程序入口是没有实例控制功能的:                                                                                                    
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
  public class ProgramMain {                                                                                                                          
   public static void main(String argv[])                                                                                                            
   {                                                                                                                                                  
    mainFrame frame = new mainFrame();                                                                                                                
   }                                                                                                                                                  
  }                                                                                                                                                  
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
现在想加入实例控制,只需要添加两行代码,添加后代码如下所示:                                                                                        
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
  public class ProgramMain {                                                                                                                          
   public static void main(String argv[])                                                                                                            
   {                                                                                                                                                  
    InstanceControl ic = new InstanceControl();                                                                                                      
    ic.start();                                                                                                                                      
    mainFrame frame = new mainFrame();                                                                                                                
   }                                                                                                                                                  
  }                                                                                                                                                  
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
在这个基础上,要实现第二种情况,也就是已经有实例运行的情况下,新的实例退出,保持原有的运行实例,就只需要一点小的改动了。具体的实现如下:            
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
  import java.net.*;                                                                                                                                  
  public class InstanceControl2 extends Thread {                                                                                                      
   public void run() {                                                                                                                                
    try{                                                                                                                                              
     Socket sock = new Socket("127.0.0.1", 22222);//创建socket,连接22222端口                                                                          
     System.exit(0); //连接成功,说明有实例存在,则退出                                                                                              
    }catch (Exception e)                                                                                                                              
    {}                                                                                                                                                
    try{                                                                                                                                              
     ServerSocket server = new ServerSocket(22222);//创建socket,连接22222端口                                                                        
     while (true)                                                                                                                                    
     {                                                                                                                                                
      server.accept(); //接受连接请求                                                                                                                
     }                                                                                                                                                
    }catch (Exception e)                                                                                                                              
    {                                                                                                                                                
     e.printStackTrace();                                                                                                                            
    }                                                                                                                                                
   }                                                                                                                                                  
  }                                                                                                                                                  
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
这个类的使用方法和第一种情况的那个类是一样的,只需要在原有的代码上加入两行代码即可:                                                                
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
  InstanceControl2 ic = new InstanceControl();                                                                                                        
  ic.start();                                                                                                                                        
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                                         扩展                                                                        
                                                                                                                                                      
                                                                                                                                                      
上面的程序也许有一个小bug,就是如果程序在开始运行时ServerSocket监听的端口已经被其它程序占用,那么程序的运行就会受到影响。所以程序的端口应该尽量取得  
大一些,在这种情况下其它程序占用这个程序使用的端口的概率是可以忽略不计的。同时,还可以做两种扩展,第一种是把端口写在配置文件中,可通过读配置文件得到
端口,这样就能够在其它程序占用目前端口的情况下改变这个程序使用的端口。还有一种是在运行的时候用两个InstanceControl类分别在两个端口监听,只要有一个    
InstanceControl类得到连接就做出响应,这样两个端口都被其它程序占用的概率就更加的微乎其微了。                                                          
                                   
posted on 2006-12-11 11:46 芦苇 阅读(515) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: