为什么有的类的start方法里会判断是否有读写锁?
发布日期:2022-02-10 11:36:50 浏览次数:35 分类:技术文章

本文共 2298 字,大约阅读时间需要 7 分钟。

如题:我们经常会看到有的类的start方法里面会有getLock()等类似的代码?这是为什么呢?什么地方需要用到这样的lock
在本人的工作中发现,比较常见的一类现象是:需要一直让某类程序长跑,即便出现异常,程序自动终止,那除了处理这些异常(比如
将对应的消息放到ExceptionMessage的Queue里面去,然后重新启动程序去处理之前的任务,当然在程序异常终止的时候,是会丢掉
部分消息。那在这种情况下,我们采用的逻辑是创建一个无限的for循环,在其中每五到十分钟调用一次脚本文件,脚本文件负责启动
线程去处理任务(比如外界传入的消息等)。但是,我们希望同一台机器上不要同时有同一脚本文件的两份实例在运行,那么这个时候
getLock()就派上用场了,它在脚本启动的时候判断是否有lock,如果有则不启动,否则启动。具体代码如下:
public void start(){
logger.info("start to transmit message from topic:{}", topicName);
StartUpLock lock = null;
try {
            lock = getMsgTransmiterlock(lockFileName);
} catch (RuntimeException ex) {
           logger.error("failed to get lock. lockFileName:{}", lockFileName, ex);
           return;
}
if (null == lock) {
           logger.info("message transmiter has been started. lockFileName:{}", lockFileName);
           return;
}
            }
private StartUpLock getMsgTransmiterlock(String fileName) {
        StartUpLock lock = new StartUpLock(new File(lockFileName));
        if (lock.lock()) {
            return lock;
        } else {
            return null;
        }
         }
    public class StartUpLock {
    private final File file;
    private FileLock lock = null;
    private RandomAccessFile raf = null;
    private FileChannel fc = null;
    private static Logger logger = LoggerFactory.getLogger(StartUpLock.class);
        
    public StartUpLock(File file) {
        this.file = file;
        
    }
    
    public boolean lock(){
        if(this.lock != null){
            logger.warn("have already gotten lock");
            return true;
        }
            
        try {
            if( !file.exists() ){
                File dir = file.getParentFile();
                if( !dir.exists() ) dir.mkdirs();
                file.createNewFile();
            }
            
            this.raf = new RandomAccessFile(this.file, "rw");
            this.fc = raf.getChannel();
            
            this.lock = fc.tryLock();
            if( this.lock == null ){
                logger.warn(" file have been loked, cannot get lock");
                this.close();
                return false;
            }
            
            return true;
            
        } catch (FileNotFoundException e) {
            logger.error("file not found", e);
            this.close();
            return false;
        } catch (IOException e) {
            logger.error("file io error", e);
            this.close();
            return false;
        }
    }    
    
    public void close(){        
        if( this.lock != null ){
            try {
                this.lock.release();                
            } catch (IOException e) {
                logger.error("file io error", e);
            }finally{
                this.lock = null;
            }
        }    
        
        if( this.fc != null ){
            try {
                this.fc.close();
            } catch (IOException e) {
                logger.error("file io error", e);
            }finally{
                this.fc = null;
            }
        }
        
        if( this.raf != null ){
            try {
                this.raf.close();
            } catch (IOException e) {
                logger.error("file io error", e);
            } finally{
                this.raf = null;
            }
        }
    }    
}

转载地址:https://blog.csdn.net/courage89/article/details/8836163 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:windows下配置python环境变量
下一篇:JAVA处理日期时间常用方法: java.util.Calendar

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月16日 20时21分05秒