我们有机器主机名 -
dbx111.dc1.host.com
dbx112.dc2.host.com
dcx113.dc3.host.com
dcx115.dev.host.com
这里dc1
、和是我们的数据中心,到目前为止dc2
,我们将只有四个数据中心。而且将来机器主机名之间可能会有更多的点,由另一个域分隔。dc3
dev
现在我需要找出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
,其他的东西都是一样的。