Java 如何检查每个URL的特定阈值?
我正在开发一个应用程序,在这个应用程序中,我有一个机器列表,对于每台机器,我需要点击URL,返回XML响应,我需要解析XML并从中为每台服务器提取一个字段(dataKey是字段名),然后检查该字段值是否与我的阈值匹配。如果它连续三次匹配我的阈值,则将该服务器添加到我的另一个列表中 因此,如果Java 如何检查每个URL的特定阈值?,java,algorithm,list,url,Java,Algorithm,List,Url,我正在开发一个应用程序,在这个应用程序中,我有一个机器列表,对于每台机器,我需要点击URL,返回XML响应,我需要解析XML并从中为每台服务器提取一个字段(dataKey是字段名),然后检查该字段值是否与我的阈值匹配。如果它连续三次匹配我的阈值,则将该服务器添加到我的另一个列表中 因此,如果machineAdataKey字段连续三次匹配我的阈值,则将machineA添加到我的列表中,如果我的machineBdataKey字段值也连续三次匹配我的相同thresold值,则将machineB添加到同
machineA
dataKey
字段连续三次匹配我的阈值,则将machineA
添加到我的列表中,如果我的machineB
dataKey
字段值也连续三次匹配我的相同thresold值,则将machineB
添加到同一列表中
将机器添加到列表后,重置计数器并再次启动
下面是我的MachineStats
类,它保存了每台机器的信息-
public class MachineStats {
private String serverName;
private String serverURL;
private int dataKey = 0;
public MachineStats(String serverName, String serverURL) {
this.serverName = serverName;
this.serverURL = serverURL;
}
// getters here
public boolean isEligibleForEmail(Integer dataKeyCount, int thresholdLimit) {
try {
if (!TestUtils.isEmpty(dataKeyCount)) {
if (dataKeyCount >= thresholdLimit) {
dataKey++;
} else {
dataKey = 0;
}
if (dataKey > 3) {
dataKey = 0;
return true;
}
return false;
} else {
return false;
}
} catch (NumberFormatException ex) {
// log an exception
return false;
}
}
}
下面是我的main方法中的代码,它是serverList
保存机器列表的起点
// this holds machineA and machineB information
private static List<MachineStats> serverList = new LinkedList<MachineStats>();
while(true) {
ServerMetricsTask task = new ServerMetricsTask(serverList, thresholdLimit);
task.run();
TimeUnit.MINUTES.sleep(2); // sleep for 2 minutes
}
问题陈述:-
现在我有上面的代码工作良好。到目前为止,我只为每台服务器检查一个属性,即dataKey
。现在我需要对每个服务器的两个字段执行相同的操作。一个字段是dataKey
,第二个字段是customerKey
,这两个字段将出现在每个服务器的XML响应中
所以我有一个fieldHolder
映射,它保存了我需要检查的字段名
此处fieldHolder
map保持-
key - field which we want to extract from the response coming from the URL.
value - is the threshold for that field which is a key.
现在我修改了我的代码如下-
// this holds machineA and machineB information
private static List<MachineStats> serverList = new LinkedList<MachineStats>();
private static Map<String, String> fieldHolder;
while(true) {
ServerMetricsTask task = new ServerMetricsTask(serverList, fieldHolder);
task.run();
TimeUnit.MINUTES.sleep(2); // sleep for 2 minutes
}
我面临的唯一问题是,我不知道应该如何添加逻辑,以便它可以在迭代fieldHolder
map之后通过执行URL来提取两个字段值,然后还可以检查这两个字段或其中一个字段是否连续三次满足阈值限制。如果连续三次达到阈值限制,则将该机器名称添加到列表持有者
我已经知道如何通过解析XML响应来提取给定URL的字段值。首先,将MachineStats中的dataKey更改为keyMap
public class MachineStats {
...
private HashMap<String,int> keyMap = new HashMap<String,int>;
...
public HashMap<String,int> getKeyMap() {
return this.keyMap;
}
}
现在,您需要更改TestUtils.extractKey()
,以便它接受并提取任意的密钥名。与我在评论中所说的相反,最好不要改变它的内在逻辑,这样我们就不会再做任何改变了
TestUtils.extractKey(String keyName, String serverName, string serverURL);
现在,ServerMetricsTask迭代机器列表,还迭代机器的键映射:
public class ServerMetricsTask {
...
for (MachineStats stats : listOfServers) {
//now what should I do here?
// I guess I need to iterate fieldHolder map
HashMap<String, int> keymap = stats.getKeyMap();
int key_count = keymap.size();
int valid_count = 0;
Set<String> keys = keymap.keySet();
for (String key : keys) {
int count = keymap.get(key);
int result = TestUtils.extractKey(key, stats.getServerName(), stats.getServerURL());
if (stats.isEligibleForEmail(result, thresholdLimit)) {
count++;
} else {
// here you can do count = 0 if you need strictly continuous appearance of the key
}
keymap.put(key, count);
if (count >= 3) valid_count++;
}
// now decide if you need all keys to be valid or just some
if (valid_count == key_count) holder.add(stats.getServerName());
...
公共类ServerMetricsTask{
...
对于(MachineStats统计:服务器列表){
//现在我该怎么办?
//我想我需要迭代fieldHolder映射
HashMap keymap=stats.getKeyMap();
int key_count=keymap.size();
int有效计数=0;
Set keys=keymap.keySet();
用于(字符串键:键){
int count=keymap.get(键);
int result=TestUtils.extractKey(key,stats.getServerName(),stats.getServerURL());
if(stats.isEligibleForEmail(结果,阈值限制)){
计数++;
}否则{
//如果需要密钥的严格连续外观,可以在这里执行count=0
}
keymap.put(键,计数);
如果(计数>=3)有效_计数++;
}
//现在决定是需要所有密钥都有效还是只需要一些密钥
if(valid_count==key_count)holder.add(stats.getServerName());
...
Yes您迭代fieldHolder
映射并更改TestUtils.extractKey(stats.getServerName(),stats.getServerURL())
到TestUtils.extractKey(keyName,stats.getServerName(),stats.getServerURL())
以便提取任何给定的键。或者只需将整个fieldHolder
传递到TestUtils.extractKey()
然后让它在那里迭代。它可以返回一个类似的映射,但只包含找到的密钥。@AndreiNikolaenko但是我将如何保持计数我的密钥是否连续三次满足每个服务器的阈值,如果两个密钥或其中一个密钥都满足阈值,则将它们添加到我的持有者列表中。哦,确实如此,对于each服务器初始化key=>counter的映射,并将此映射传递给extractKey,它将迭代映射并为找到的任何键递增计数器。然后,您可以检查有多少键具有有效计数器。下次调用时,您将传递同一映射。@AndreiNikolaenko hmmm有点困惑。您能否提供一个示例,说明我将如何实现此目标?我认为我可以n从TestUtils
类extractKey
方法中提取这些键值。感谢您的帮助。在您的建议中,您在for循环中有keyset
for(String key:keyset){
这是什么?或者它是keys
?
keyMap.put("Key1", 0);
keyMap.put("Key2", 0);
// etc. I don't know how you will do it
TestUtils.extractKey(String keyName, String serverName, string serverURL);
public class ServerMetricsTask {
...
for (MachineStats stats : listOfServers) {
//now what should I do here?
// I guess I need to iterate fieldHolder map
HashMap<String, int> keymap = stats.getKeyMap();
int key_count = keymap.size();
int valid_count = 0;
Set<String> keys = keymap.keySet();
for (String key : keys) {
int count = keymap.get(key);
int result = TestUtils.extractKey(key, stats.getServerName(), stats.getServerURL());
if (stats.isEligibleForEmail(result, thresholdLimit)) {
count++;
} else {
// here you can do count = 0 if you need strictly continuous appearance of the key
}
keymap.put(key, count);
if (count >= 3) valid_count++;
}
// now decide if you need all keys to be valid or just some
if (valid_count == key_count) holder.add(stats.getServerName());
...