如何每隔x分钟执行一台机器的网址?

时间:2014-04-01 03:13:01

标签: java multithreading algorithm scheduledexecutorservice

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

DC1我有2台机器(machineA and machineB),在DC2我有两台机器(machineC and machineD),在DC3我又有两台机器{{1} }。

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

(machineE and machineF)

对于DC1 -

http://machineName:8080/textbeat

对于DC2 -

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

对于DC3 -

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

这是我在点击任何特定机器的网址后一般看到的响应字符串 -

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

问题陈述: -

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

现在如果data_syncs machineA在5分钟内始终为零,那么我想打印data_syncsDC1。同样适用于machineB和其他数据中心。

我在想的逻辑 -

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

下面是我的地图,如果他们符合上述条件我将把数据中心及其机器放在哪里 -

data_syncs

此处key是数据中心名称,value是满足条件的数据中心的计算机列表。

下面是我提出的用于解决上述问题的代码,但它不能像我想的那样工作。在这里,我的final Map<String, List<String>> holder = new LinkedHashMap<String, List<String>>(); 对于我认为不是我想要的所有机器是相同的。

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

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

对于每台计算机,如果public class MachineMetrics { private String machineName; private String dataSyncs; // getters and setters } data_syncs 连续期间为0,那么我需要记录该数据中心及其计算机。

1 个答案:

答案 0 :(得分: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();
    }

}