有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中检查internet连接

我使用java代码从Outlook Exchange Server下载邮件。 我面临的问题是:如果互联网连接中断,程序就会停止执行

我想让代码检查互联网连接,直到连接可用。如果互联网断开,我不想停止执行

关于堆栈溢出,很少有检查internet连接的解决方案,但如果我使用这些解决方案,一段时间后,我会出现堆栈溢出错误

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");
     } 


 }

我通过以下函数调用该方法:

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);
    }

}

这是互联网断开时我遇到的一个例外:

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.IMAPProtocol.(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

任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    按照行动的要求。用Sarge框架监督重试

    我将你的连接逻辑外部化到一个邮件服务类中,这样我就可以监督它了

    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;
    }
    

    }

    然后我将downloadEmails实现更改为使用MailService并为Sarge创建一个Plan

        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
    

    当抛出ConnectException时,此示例将在一分钟内重试五次。根据需要将其他失败/重试案例添加到计划中

    编辑

    我的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>