• 热切换与RoundRobin的简单实例


    HotSwitch经常在一些数据库的主从备份中出现。另一个场景索引的切换。

    下面提一个java模拟的路径切换的示例。采用简单的round robin算法实现:

    import java.io.File;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class TestHotSwitch implements Runnable
    {
    
        File        file;
        File        file2;
        SwitchObj[] sos = new SwitchObj[2];
    
        public TestHotSwitch()
        {
            try
            {
                ExecutorService exec = Executors.newSingleThreadExecutor();
                exec.execute(this);
                exec.shutdown();
            }
            catch (Exception ex)
            {
                System.out.println(ex);
            }
        }
    
        public void init()
        {
            System.out.println("do some init");
            SwitchObj obj1 = new SwitchObj();
            obj1.isUse = true;
            obj1.filepath = "/tmp/res1";
            sos[0] = obj1;
            SwitchObj obj2 = new SwitchObj();
            obj2.isUse = false;
            obj2.filepath = "/tmp/res2";
            sos[1] = obj2;
            file = new File("/tmp/middle");
            file2 = new File("/tmp/dest");
        }
    
        // 主线程模拟每隔2S,一个新的/tmp/middle文件夹生成。main方法可以用cronjob代替。renameTo也可以用mv代替。
        public static void main(String[] args) throws Exception
        {
            TestHotSwitch tf = new TestHotSwitch();
            tf.init();
            for (int i = 0; i < 5; i++)
            {
                if (!tf.file.exists())
                    tf.file.mkdir();
                tf.file.renameTo(tf.file2);
                Thread.sleep(2000);
            }
        }
    
        @Override
        public void run()
        {
            while (true) // block
            {
                try
                {
                    // 每隔1s处理
                    Thread.sleep(1000);
                    // 如果存在,开始应用程序处理。
                    if (file2.exists())
                    {
    
                        for (int i = 0; i < sos.length; i++)
                        {
                            if (sos[i].isUse)
                            {
                                continue;
                            }
                            file2.renameTo(new File(sos[i].filepath));
                            sos[i].isUse = true;
                            sos[(i + 1) % 2].isUse = false;
                            System.out.println("success switch sos[" + i + "]");
                        }
    
                    }
    
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    
        class SwitchObj
        {
            boolean isUse; //热切换通常需要一个状态位来确定是否可用。
            String  filepath;
        }
    }
     
     
     
  • 相关阅读:
    C++的内存管理
    PostgreSQL学习手册(函数和操作符<一>)
    C++位操作
    C++的预处理
    PostGIS之路——几何对象编辑(二)
    C++运算符重载
    PostgreSQL学习手册(函数和操作符<二>)
    PostGIS之路——几何对象处理函数(一)
    postgresql命令
    不要迷失在技术的海洋中
  • 原文地址:https://www.cnblogs.com/highriver/p/2440897.html
Copyright © 2020-2023  润新知