- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在从事一个项目,其中我有三个数据中心 - DC1
、DC2
和 DC3
。
在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 和其他数据中心也是如此。
我想的逻辑-
data_syncs
值(如果它为零),将计数器加一,下面是我的 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_syncs
在 5 分钟
期间连续 为 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/
我被难住了。如果我对文件路径进行硬编码,则此脚本在我的 Windows 机器上的 Eclipse 中运行良好。如果我尝试接受参数并在我的边缘节点(一个 linux 机器)上运行它,它不会抛出任何特定的
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 8 年前。 这个问题似乎不是关于 a specific programming problem,
我们最近将我们的基础架构从 Solaris(Oracle/Sun Java) 迁移到 AIX(IBM Java)。 我们的客户将使用我们共享的算法(AES)和 key 上传加密文件,一旦加密文件放置在
我想编写一个程序(java),它接受一个文件作为输入,对其进行加密(使用aes128)并通过ftp发送该加密文件,接收者接收它并使用 key 进行解密。我是初学者,有什么帮助可以做到这一点吗?非常感谢
我正在尝试将一些为 1c2 机器 (thumb) 编译的 DLL 导入 WinMobile 6.1 C# 智能设备项目。 然而,当我尝试将它们导入我的 C# 项目时,我得到“无法添加对...的引用”,
我正在寻找 FPGA + 机器。 它应该是入门级定价(例如不超过 200 美元)。 编辑:我想制作一个 ASM 图表并将 FPGA 编程为我在图表中指定的行为 最佳答案 你看过Arduino ? 关于
这是我想完成的: Write a program that stimulates a bean machine Your program should prompt the user to enter
我尝试使用以下命令在 Windows 10 上使用 hyperv 创建一台机器: docker-machine create --driver hyperv default 但它给了我: This m
我有个问题 我的问题是我有一个将 mapred.map.tasks 配置为10的作业(抓取工具),这意味着我的工作将一次创建10个映射器。但是我的集群将 mapred.tasktracker.map.
我正在尝试使用命令重新启动 Docker sudo docker restart a7f8ce75f51f 但我收到以下错误 Error response from daemon: Cannot re
在新机器上引导 Eclipse 是一个非常耗时的过程,您最终会问自己是否真的需要每个插件。但这些都很方便,并且有助于养成一致的习惯。 Eclipse 引导问题包括: 解释/记录需要发生的事情 粘贴正确
我们希望建立一个 Docker 开发节点,我们团队中的任何人都可以将东西部署到其中。 我使用 SSH 创建了一个新的 Docker 机器,如下所示: docker-machine create \
如果可能的话,我想使用 java.util.logging 来做到这一点,有什么想法吗?谢谢。 最佳答案 您可以尝试一下SLF4J . Simple Logging Facade for Java (
当 vagrant up 时,我们的 vagrant box 需要大约 1 小时才能提供第一次运行,在配置过程的最后,我想将盒子打包到本地文件夹中的图像,以便下次需要重建时将其用作基础盒子。我正在使用
我正在为我的图像处理项目构建一个 SVM 线性机,在其中提取正样本和负样本的特征并将其保存到目录中。然后,我使用这些功能训练 SVM,但收到一个无法调试的错误。下面是我用于训练分类器的 train-c
问题描述: 我要将MySQL server 5.7.11 (win32) 安装到Windows server 2012 中。服务器中安装了多个网络接口(interface)卡,我将安装多个绑定(bin
我想安排一台 (AWS) Linux 计算机启动、运行程序,然后自行关闭(以将成本保持在最低水平)。我可以放 mycommand; shutdown 在/etc/rc.local 文件中。但如果我需要
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
如何将此文件的输出发送到另一台 Linux 计算机的主目录。 显然,我想发送此文件的输出: sed '/^\s*#/d;/^$/d' /etc/httpd/conf/httpd.conf 到 nati
我有一个 Linux 机器,我可以使用 SSH 进行 root 访问。 我想使用GDB来调试系统。 这是一个精简的 Debian 软件包;因此,我里面没有任何编译工具。 uname -a 给出: 2.
我是一名优秀的程序员,十分优秀!