0

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...");
  1. In my code, if apps are down both i & iii is getting executed.Only iii must be executed.
  2. if servers are down both i & ii is getting executed. Only ii must be executed.
  3. If both are down only i must get executed, but all three are executed.
  4. 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.
V02169194
  • 67
  • 1
  • 2
  • 15
  • You are using `==` operator to compare String values, use `equals()` instead. Check the link to the duplicate discussion. – ttarczynski May 07 '15 at 05:40
  • Even if I use .equals, only the else part is getting executed – V02169194 May 07 '15 at 05:41
  • The logic of this program snippet looks very suspicious. What's the point of testing a list element's field using index i or j before the loop is entered? @Eran the comparison is just a red herring - the entire code is broken, I think. Reopening and editing the == to equals might be beneficial. – laune May 07 '15 at 06:00
  • @Vaishnavi Explain the logic of the loops and ifs. **Always use for statements with declaring the loop counter in the for statement, e.g. `for( int i = 0;...)` (unless you are very, very sure what you are doing).** – laune May 07 '15 at 06:03
  • @Vaishnavi Can you explain what is exact problem?? – Ranjeet May 07 '15 at 06:03
  • please find my edited question – V02169194 May 07 '15 at 10:07

0 Answers0