0

我们有机器主机名 -

dbx111.dc1.host.com
dbx112.dc2.host.com
dcx113.dc3.host.com

dcx115.dev.host.com

这里dc1、和是我们的数据中心,到目前为止dc2,我们将只有四个数据中心。而且将来机器主机名之间可能会有更多的点,由另一个域分隔。dc3dev

现在我需要找出datacenter我当前的机器所在的位置,因为我将在实际机器上运行下面的代码。

而且我现在有两个流程,一个是 USERFLOW,另一个是 DEVICEFLOW。

public enum FlowTypeEnum {
    USERFLOW, DEVICEFLOW
}

问题陈述:-

  • 如果我的机器在DC1并且流类型是 USERFLOW 那么我需要返回/test/datacenter/dc1但是如果流类型是 DEVICEFLOW 那么我需要返回/testdevice/datacenter/dc1
  • 但是如果我的机器在DC2并且流类型是 USERFLOW 那么我需要返回/test/datacenter/dc2但是如果流类型是 DEVICEFLOW 那么我需要返回/testdevice/datacenter/dc2
  • 如果我的机器在DC3并且流类型是 USERFLOW 那么我需要返回/test/datacenter/dc3但是如果流类型是 DEVICEFLOW 那么我需要返回/testdevice/datacenter/dc3
  • 但是如果我的机器数据中心在DEV,并且流类型是 USERFLOW 那么我需要返回“/test/datacenter/dc1”但是如果流类型是 DEVICEFLOW 那么我需要返回/testdevice/datacenter/dc1

我的下面的代码工作正常,但它根本不使用FlowTypeEnum。如何将typeEnum值从 Java main 传递到DatacenterEnum类并返回字符串,如上述算法所示 -

下面是我的 TestsingEnum 类 -

public class TestingEnum {

    public static void main(String[] args) {
        // FlowTypeEnum typeEnum = FlowTypeEnum.USERFLOW;
        // how to pass this typeEnum value to DatacenterEnum class with the current setup I have
        // and return basis on above algorithm

        System.out.println(DatacenterEnum.LOCAL_POOK);
    }
}

下面是我的 DatacenterEnum 类 -

public enum DatacenterEnum {
    DEV, DC1, DC2, DC3;

    private static final Random random = new Random();

    public static String forCode(int code) {
        return (code >= 0 && code < values().length) ? values()[code].name() : null;
    }

    private static final DatacenterEnum ourlocation = compareLocation();

    private static DatacenterEnum compareLocation() {
        String currenthost = getHostNameOfServer();

        if (currenthost != null) {
            if (isDevMachine(currenthost)) {
                return DC1;
            }

            for (DatacenterEnum dc : values()) {
                String namepart = "." + dc.name().toLowerCase() + ".";
                if (currenthost.indexOf(namepart) >= 0) {
                    return dc;
                }
            }
        }
    }

    private String toLocalPook() {
        if (this == DEV) {
            return "/test/datacenter/dc1";
        }

        return "/test/datacenter/" + name().toLowerCase();
    }

    public static final String LOCAL_POOK = ourlocation.toLocalPook();

    private static final String getHostNameOfServer() {
        try {
            return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
        } catch (UnknownHostException e) {
            // log an exception
        }

        return null;
    }

    private static boolean isDevMachine(String hostName) {
        return hostName.indexOf("." + DEV.name().toLowerCase() + ".") >= 0;
    }
}

USERFLOW 和 DEVICEFLOW 之间的唯一区别是 - 对于 USERFLOW,我需要使用/test,对于 DEVICEFLOW,我需要使用/testdevice,其他的东西都是一样的。

4

1 回答 1

1

为什么不将流类型传递给 toLocalPook 方法:

enum Flow {
    USERFLOW , DEVICEFLOW 
}
private String toLocalPook(Flow f) {
    String prefix = "";
    if (f.equals(Flow.DEVICEFLOW)) {
        prefix = "/testdevice";
    } else if (f.equals(Flow.USERFLOW)) {
        prefix = "/test";
    }
    if (this == DEV) {
        return prefix + "/datacenter/dc1";
    }

    return prefix + "/datacenter/" + name().toLowerCase();
}

如果这样做,则必须更改调用 toLocalPook 的方式... LOCAL_POOK 不能是最终静态的:

class OtherClass {
    public static void main(String[] args) {
        String LOCAL_POOK = DatacenterEnum.getOurlocation().toLocalPook(
                Flow.USERFLOW);
        System.out.println(LOCAL_POOK);
    }

}

这是完整的枚举

    public enum DatacenterEnum {
    DEV, DC1, DC2, DC3;


    public static DatacenterEnum getOurlocation() {
        return ourlocation;
    }

    private static final Random random = new Random();

    public static String forCode(int code) {
        return (code >= 0 && code < values().length) ? values()[code].name() : null;
    }

    private static final DatacenterEnum ourlocation = compareLocation();

    private static DatacenterEnum compareLocation() {
        String currenthost = getHostNameOfServer();

        if (currenthost != null) {
            if (isDevMachine(currenthost)) {
                return DC1;
            }

            for (DatacenterEnum dc : values()) {
                String namepart = "." + dc.name().toLowerCase() + ".";
                if (currenthost.indexOf(namepart) >= 0) {
                    return dc;
                }
            }
        }
        return null;
    }

    enum Flow {
        USERFLOW , DEVICEFLOW 
    }
    public String toLocalPook(Flow f) {
        String prefix = "";
        if (f.equals(Flow.DEVICEFLOW)) {
            prefix = "/testdevice";
        } else if (f.equals(Flow.USERFLOW)) {
            prefix = "/test";
        }
        if (this == DEV) {
            return prefix + "/datacenter/dc1";
        }

        return prefix + "/datacenter/" + name().toLowerCase();
    }


    private static final String getHostNameOfServer() {
        try {
            return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
        } catch (UnknownHostException e) {
            // log an exception
        }

        return null;
    }

    private static boolean isDevMachine(String hostName) {
        return hostName.indexOf("." + DEV.name().toLowerCase() + ".") >= 0;
    }

}
于 2014-06-29T18:00:10.363 回答