I have a requirement to send mail to an User if the applications and servers are down/up. So I have coded in such a way that if application or server is down a mail should be sent,else when all applications are up a different mail should be sent. I am iterating because I have n number of apps and servers.
i The following code must be executed only if both apps and servers are down
for(i=0;i<resp.size();i++){
for(j=0;j<obj.size();j++){
if((obj.get(j).get("status").equals("DOWN"))&&(resp.get(i).get("status").equals("DOWN"))){
sender.sendMail(
"Demo Apps & Server Status","Hi,"
+ "<br><br>"+
"<u>The Following Servers are DOWN : </u>"
+ '\n'
+ "<table cellpadding='10' cellspacing='10'><th>Name</th><th>Status</th><tr><td>"
+ obj.get(j).get("name") + "</td>"
+ "<td>" + obj.get(j).get("status")
+ "</td></tr></table>" + '\n'+"<u>The Following Applications are DOWN : </u>"
+ '\n'
+ "<table cellpadding='15',cellpadding='15'><th>Name</th>"
+ "<th>Status</th>"
+ '\n'
+ "<tr align='left'><td align='left'>"
+ resp.get(i).get("name")
+ "</td>"
+ "<td align='right'>"
+ resp.get(i).get("status")
+ "</td>"
+ "</tr></table>"+ "<br><br>" + "Thanks & Regards," + "<br>"
+ "<b>Smart Miles Support Team</b>",Constants.setFrom, Constants.emailTO);
System.out.println("The servers & app down Email was Sent Succesfully...");
}
}
}
ii The following code must be executed only if servers are down
for(i=0;i<resp.size();i++){
if ((resp.get(i).get("status").equals("DOWN"))) {
sender.sendMail(
"Demo Apps & Server Status","Hi,"
+ "<br><br>"+
"<u>The Following Applications are</u>"
+ '\n'
+ "<table cellpadding='15',cellpadding='15'><th>Name</th>"
+ "<th>Status</th>"
+ '\n'
+ "<tr align='left'><td align='left'>"
+ resp.get(i).get("name")
+ "</td>"
+ "<td align='right'>"
+ resp.get(i).get("status")
+ "</td>"
+ "</tr></table>"
+ '\n'
+ "<u>The Following Servers are up and running</u>"
+ '\n'
+ "<ul><li>Demo Apps</li><li>L&T Infotech Appstore</li><li>L&T Appstore</li><li>DCP Onetouch</li><li>Viacom</li></ul>"+ "<br><br>" + "Thanks & Regards," + "<br>"
+ "<b>Smart Miles Support Team</b>",
Constants.setFrom, Constants.emailTO);
System.out.println("The Apps down Email was Sent Succesfully...");
}
}
}
iii The following code must be executed only if apps are down
for(j=0;j<obj.size();j++){
if ((obj.get(j).get("status").equals("DOWN"))) {
sender.sendMail(
"Demo Apps & Server Status","Hi,"
+ "<br><br>"+
"<u>The Following Servers are DOWN : </u>"
+ '\n'
+ "<table cellpadding='10' cellspacing='10'><th>Name</th><th>Status</th><tr><td>"
+ obj.get(j).get("name") + "</td>"
+ "<td>" + obj.get(j).get("status")
+ "</td></tr></table>" + '\n'
+ "<b>All the Applications are UP and RUNNING.</b>"+ "<br><br>" + "Thanks & Regards," + "<br>"
+ "<b>Smart Miles Support Team</b>",
Constants.setFrom, Constants.emailTO);
System.out.println("The servers down Email was Sent Succesfully...");
- In my code, if apps are down both i & iii is getting executed.Only iii must be executed.
- if servers are down both i & ii is getting executed. Only ii must be executed.
- If both are down only i must get executed, but all three are executed.
- I am getting n number of mails if n number of apps/server is down. I must get only one mail with the n number of apps and servers.