简体   繁体   中英

Checking for internet connectivity in java

I have java code using which i am downloading mails from Outlook Exchange Server. The problem i am facing is: If internet connection is down, the program execution stops.

I want the code to check for internet connectivity until the connection is available. I don't want the execution to stop if internet is disconnected.

Few solutions for checking internet connectivity are there on stack overflow, but if i use those solution, after some time i get stackoverflow error.

private static void checkNetConnectivity() 
 {
     Socket sock = new Socket();
     InetSocketAddress addr = new InetSocketAddress("www.google.com",80);
     try{
        sock.connect(addr,3000);
        System.out.println("connected");
     }catch(Exception e){
         System.out.println("not connected");
     } 


 }

I am calling the method from the following function:

public static void downloadEmails(String protocol, String host, String port, String username, String password)
{
    Properties props = new Properties();
    Folder inbox = null;

    MimeBodyPart bp = null;
    String mail_subject = null, mail_body = null;
    int i;


    props.setProperty("mail.store.protocol", "imaps");
    //props.put("mail.pop3.host", host);
    //props.put("mail.pop3.port", port);
    //props.put("mail.pop3.starttls.enable", "true");
    try
    {
        do
        {
            checkNetConnectivity();
        Session session = Session.getInstance(props);
        //session.setDebug(true);
        Store store = session.getStore(protocol);
        store.connect(host, username, password);


        inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);// READ_WRITE mode is compulsory if you want to set the SEEN flag.


            cnt = 0;
            Flags seen = new Flags(Flags.Flag.SEEN);
            FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
            Message msg[] = inbox.search(unseenFlagTerm);
            System.out.println("No of unseen messages : " + msg.length);
            if (msg.length > 0) 
            {
                for (i = 0; i < msg.length; i++) 
                {
                    System.out.println("Serial No :" + i);
                    Address[] in = msg[i].getFrom();
                    for (Address address : in) 
                    {
                        System.out.println("FROM:" + address.toString());
                        //String mail_address = address.toString();
                    }
                    System.out.println("SENT DATE: " + msg[i].getSentDate());
                    System.out.println("SUBJECT: " + msg[i].getSubject());
                    mail_subject = msg[i].getSubject();
                    Date date = msg[i].getSentDate();
                    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                    String mail_sent_date = df.format(date);
                    System.out.println("Sent date = "+ mail_sent_date);


                    String str1[] = mail_subject.split("\\|");
                    int sub_delimiter_count = str1.length - 1; //count of delimiter for validating email subject


                    if (str1[0].equals("1001") && sub_delimiter_count == 3 ) 
                    {

                        System.out.println("Subject line valid.");

                        Object content = msg[i].getContent();

                        if (content instanceof String) 
                        {
                            String body = (String) content;
                            mail_body = body;

                            boolean isValid = new ReadMail().ValidateMail(mail_body);

                            if(isValid)
                            {
                                System.out.println("Email body in proper format.");
                            }
                            else
                            {
                                System.out.println("Email not in proper format.\nIgnoring...");
                                continue;
                            }
                        }
                        else if (content instanceof Multipart) 
                        {
                            System.out.println("This is MultiPart");
                            MimeMultipart mp = (MimeMultipart) msg[i].getContent();
                            bp = (MimeBodyPart) mp.getBodyPart(0);
                            InputStream partInput = bp.getInputStream();
                            mail_body = new Scanner(partInput, "UTF-8").useDelimiter("\\A").next();
                        }
                        System.out.println("hhhh");
                        ReadEmail(mail_subject, mail_body, mail_sent_date);


                    }
                    else
                    {
                        System.out.println("Subect not in required format.\nIgnoring...");
                    }
                    msg[i].setFlag(Flags.Flag.SEEN, true); // for this to work INBOX or any FOLDER has to opened in READ_WRITE mode.
                 }
            } else {
                Date date = new Date();
               System.out.println("No new messages. Last checked: "+date.toString());
           }

        try {
            Thread.sleep(2000);                 //1000 milliseconds is one second.
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        store.close();
      }while(true); 

    }catch(Exception e)
    {
        e.printStackTrace();
        //downloadEmails(protocol,host,port,username,password);
    }

}

This was the exception i got when internet got disconnected:

javax.mail.MessagingException: No route to host: connect; nested exception is: java.net.NoRouteToHostException: No route to host: connect at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)DEBUG: setDebug: JavaMail version 1.4.7 DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle] DEBUG IMAPS: mail.imap.fetchsize: 16384 DEBUG IMAPS: mail.imap.ignorebodystructuresize: false DEBUG IMAPS: mail.imap.statuscachetimeout: 1000 DEBUG IMAPS: mail.imap.appendbuffersize: -1 DEBUG IMAPS: mail.imap.minidletime: 10 DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 993, isSSL true

at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at readmail.ReadMail.downloadEmails(ReadMail.java:200) at readmail.ReadMail.main(ReadMail.java:323) Caused by: java.net.NoRouteToHostException: No route to host: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237) at com.sun.mail.iap.Protocol.(Protocol.java:116) at com.sun.mail.imap.protocol.IMAPP rotocol.(IMAPProtocol.java:115) at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:685) at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:636) ... 4 more

Any help will be greatly appreciated.

Per the OP's request. Supervising retries with Sarge framework.

I externalized your connect logic into a MailService class so that I could supervise it.

public class MailService {


public Store connect(String protocol, String host, String username, String password) throws MessagingException{

    Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imap");
    //props.put("mail.pop3.host", host);
    //props.put("mail.pop3.port", port);
    //props.put("mail.pop3.starttls.enable", "true");

    Session session = Session.getInstance(props);
    //session.setDebug(true);
    Store store = session.getStore(protocol);

    store.connect(host, username, password);

    return store;
}

}

Then I changed your downloadEmails implementation to use the MailService and create a Plan for Sarge .

    public static void downloadEmails(String protocol, String host, String port, String username, String password) {

    Plan failurePlan = new Plan() {
        public Directive apply(Throwable failure) {
            if (failure instanceof MessagingException) {
                // if we failed due to ConnectException, then retry five times over a minute.
                if (((MessagingException) failure).getCause() instanceof ConnectException) {
                    return Directive.Retry(5, Duration.mins(1));
                }
            }
            return Directive.Escalate;
        }

    };

    Sarge sarge = new Sarge();

    Folder inbox = null;
    MimeBodyPart bp = null;
    String mail_subject = null, mail_body = null;
    int i;


    try {

        MailService supervisedMailService = sarge.supervised(MailService.class, failurePlan);

        do {

            Store store = supervisedMailService.connect(protocol, host, username, password);

            inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);

// ... yada yada yada ... your code

This example will retry five times over a minute when ConnectExceptions are thrown. Add other failure / retry cases to the Plan as needed.

Edit

My pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.hall</groupId>
  <artifactId>sarge</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SargeExample</name>
  <description>SargeExample</description>
  <dependencies>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
        <groupId>net.jodah</groupId>
        <artifactId>sarge</artifactId>
        <version>0.3.1</version>
    </dependency>
  </dependencies>
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM