gpt4 book ai didi

java - 如何每 x 分钟执行每台机器的 url?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:44 25 4
gpt4 key购买 nike

我正在从事一个项目,其中我有三个数据中心 - DC1DC2DC3

DC1中我有2台机器(machineA和machineB),在DC2中我有两台机器(machineC和machineD) DC3 中,我又有两台机器 (machineE 和 machineF)

每个数据中心的每个机器 URL 都是这样,它返回字符串作为响应 -

http://machineName:8080/textbeat

对于 DC1-

http://machineA:8080/textbeat
http://machineB:8080/textbeat

对于 DC2-

http://machineC:8080/textbeat
http://machineD:8080/textbeat

对于 DC3-

http://machineE:8080/textbeat
http://machineF:8080/textbeat

这是我在点击任何特定机器的 url 后通常看到的响应字符串 -

state: READY server_uptime: 12462125 data_syncs: 29

问题陈述:-

现在我需要迭代每个数据中心的所有机器并执行 URL,然后从中提取 data_syncs。这必须每 1 分钟完成一次。

现在如果 machineA data_syncs 在 5 分钟内始终连续为零,那么我想打印 DC1机器A。 machineB 和其他数据中心也是如此。

我想的逻辑-

  • 从每个数据中心 Ping 每台机器,提取 data_syncs 值(如果它为零),将计数器加一,
  • 然后在一分钟后重试,如果该值仍然为零,则将同一个计数器再次递增 1。
  • 如果计数器达到 5(因为它是 5 分钟)并且它仍然连续为零,那么我会在我的 map 中添加这台机器和数据中心名称。
  • 但假设在连续三次尝试中它为零,而在第四次尝试中它变为非零,那么我的计数器将为数据中心中的那台机器重置为零,并为该机器再次启动该过程。

下面是我的 map ,如果满足上述条件,我将在其中放置数据中心及其机器 -

final Map<String, List<String>> holder = new LinkedHashMap<String, List<String>>();

这里的 key 是数据中心的名称,value 是满足条件的数据中心的机器列表。

下面是我想出的解决上述问题的代码,但我猜它并没有按照我应该做的方式工作。这里我的 counter 对于我猜的所有机器都是相同的,这不是我想要的。

public class MachineTest {

private static int counter = 0;
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);

public static void main(String[] args) {

final ScheduledFuture<?> taskUtility = scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
generalUtility();
} catch (Exception ex) {
// log an exception
}
}
}, 0, 1L, TimeUnit.MINUTES);

}

protected static void generalUtility() {
try {
final Map<String, List<String>> holder = new LinkedHashMap<String, List<String>>();

List<String> datacenters = Arrays.asList("DC1", "DC2", "DC3");
for (String datacenter : datacenters) {
LinkedList<String> machines = new LinkedList<String>();

List<String> childrenInEachDatacenter = getMachinesInEachDatacenter(datacenter);

for (String hosts : childrenInEachDatacenter) {
String host_name = hosts;
String url = "http://" + host_name + ":8080/textbeat";

MachineMetrics metrics = GeneralUtilities.getMetricsOfMachine(host_name, url); // execute the url and populate the MachineMetrics object
if (metrics.getDataSyncs().equalsIgnoreCase("0")) {
counter++;
if (counter == 5) {
machines.add(hosts);
}
}
}
if(!machines.isEmpty()) {
holder.put(datacenter, machines);
}
}

if (!holder.isEmpty()) {
// log the datacenter and its machine as our criteria is met
System.out.println(holder);
}
} catch (Exception e) {
e.printStackTrace();
}

}

// Below method will return list of machines given the name of datacenter
private static List<String> getMachinesInEachDatacenter(String datacenter) {
// this will return list of machines for a given datacenter
}

}

这是我的 MachineMetrics 类 -

public class MachineMetrics {

private String machineName;
private String dataSyncs;

// getters and setters
}

是否可以使用 ScheduledExecutorService 来完成,因为这不是一次性过程?必须反复做

基本上对于每台机器,如果 data_syncs5 分钟 期间连续 为 0,那么我需要记录该数据中心及其机器。

最佳答案

public class Machine{
private String dataCenter;
private String machineName;
private String hostname;
private int zeroCount = 0;

//getters setters, except for zeroCount
// constructor with datacenter,machine as args

private boolean isEligibleForLogging(String dataSyncs){
if(dataSyncs.equals("0")){
zeroCount++;
}else{
zeroCount = 0;
}

if(zeroCount > 5){
zeroCount = 0;
return true;
}

return false;

}
}


static List<Machine> machines = new ArrayList<Machine>();

static{
Machine machine1 = new Machine("DC1", "name1","hostname1");
machines.add(machine1);
//repeat the above two lines per each machine.
}


protected static void generalUtility() {
try {

for (Machine machine : machines) {

String host_name = machine.getHostName();
String url = "http://" + host_name + ":8080/textbeat";

String dataSyncs = //execute url and get datasyncs
if(machine.isEligibleForLogging()){
System.out.println(machine.getName() + ... +machine.getDataCenter() + ... + dataSyncs......);
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

关于java - 如何每 x 分钟执行每台机器的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22776125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com