0

使用GNU NNTP,我怎样才能getMessage(String newsgroup, int i)以真正的javax.mail.Message或 null 这样的方式返回Message

init:
Deleting: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
Deleted 1 out of date files in 0 seconds
Compiling 2 source files to /home/thufir/NetBeansProjects/USENET/build/classes
warning: Supported source version 'RELEASE_6' from annotation processor 'org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor' less than -source '1.7'
warning: The following options were not recognized by any processor: '[eclipselink.canonicalmodel.use_static_factory]'
1 warning
Copying 1 file to /home/thufir/NetBeansProjects/USENET/build/classes
compile:
run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 6 groups in 31ms
[EL Info]: 2012-07-31 05:21:51.235--ServerSession(1478593)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-07-31 05:21:52.523--ServerSession(1478593)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
[EL Warning]: 2012-07-31 05:21:52.672--ServerSession(1478593)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'usenet.HEADERFIELD' doesn't exist
Error Code: 1146
Call: ALTER TABLE HEADERFIELD DROP FOREIGN KEY FK_HEADERFIELD_ARTICLE_ID
Query: DataModifyQuery(sql="ALTER TABLE HEADERFIELD DROP FOREIGN KEY FK_HEADERFIELD_ARTICLE_ID")
[EL Warning]: 2012-07-31 05:21:52.687--ServerSession(1478593)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'ARTICLE'
Error Code: 1051
Call: DROP TABLE ARTICLE
Query: DataModifyQuery(sql="DROP TABLE ARTICLE")
[EL Warning]: 2012-07-31 05:21:52.797--ServerSession(1478593)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'HEADERFIELD'
Error Code: 1051
Call: DROP TABLE HEADERFIELD
Query: DataModifyQuery(sql="DROP TABLE HEADERFIELD")
Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: [gwene.ca.craigslist.vancouver.labour.general, gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode, gwene.com.blogspot.googlereader, gwene.com.economist]
Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 5
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 6
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 7
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 8
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 9
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 10
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 9
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 10
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

BUILD SUCCESSFUL (total time: 10 seconds)

枚举:

package net.bounceme.dur.usenet.model;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;

public enum Usenet {

    INSTANCE;
    private final Logger LOG = Logger.getLogger(Usenet.class.getName());
    private Properties props = new Properties();
    private Folder root = null;
    private Store store = null;
    private List<Folder> folders = new ArrayList<>();
    private Folder folder = null;

    Usenet() {
        LOG.fine("controller..");
        props = PropertiesReader.getProps();
        try {
            connect();
        } catch (Exception ex) {
            Logger.getLogger(Usenet.class.getName()).log(Level.SEVERE, "FAILED TO LOAD MESSAGES", ex);
        }
    }

    private void connect() throws Exception {
        LOG.fine("Usenet.connect..");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);
        store = session.getStore(new URLName(props.getProperty("nntp.host")));
        store.connect();
        root = store.getDefaultFolder();
        setFolders(Arrays.asList(root.listSubscribed()));
    }

    public void foo(String ng) throws Exception {
        folder = root.getFolder(ng);
        LOG.fine("opened the folder!!!!!");
        folder.open(Folder.READ_ONLY);
    }

    public List<Message> getMessages(String newsgroup) throws Exception {
        LOG.fine("fetching.." + newsgroup);
        folder = root.getFolder(newsgroup);
        LOG.fine("opened the folder!!!!!");
        folder.open(Folder.READ_ONLY);
        LOG.fine("opened: " + folder.getFullName());
        LOG.fine("opened: " + folder.getFullName());
        LOG.fine("opened: " + folder.getFullName());
        List<Message> messages = Arrays.asList(folder.getMessages());
        LOG.fine("..fetched " + folder);
        return messages;
    }

    public List<Folder> getFolders() {
        LOG.fine("folders " + folders);
        return Collections.unmodifiableList(folders);
    }

    private void setFolders(List<Folder> folders) {
        this.folders = folders;
    }

    public Message getMessage(String newsgroup, int i) {
        Message message = null;
        try {
            LOG.fine("fetching.." + newsgroup);
            folder = root.getFolder(newsgroup);
            folder.open(Folder.READ_ONLY);
            return folder.getMessage(i);
        } catch (MessagingException ex) {
            Logger.getLogger(Usenet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return message;//crummy
        }
    }
}

如何处理此异常以使程序不会崩溃?

4

1 回答 1

1

在尝试处理它之前进行简单的布尔检查:

if(!message.isExpunged)  
{  
     //process your message here  
}    
else{
    //log a message about it being expunged  
}

API

公共布尔 isExpunged()

检查此消息是否已被删除。除 getMessageNumber() 之外的所有其他方法在已删除的 Message 对象上均无效。

由于对包含文件夹的显式 expunge() 请求而被删除的消息会立即从该文件夹中删除。被另一个来源从外部删除的消息被标记为“已删除”并为 isExpunged() 方法返回 true,但在对文件夹执行显式 expunge() 之前,它们不会从文件夹中删除。

有关删除处理的更多详细信息,请参阅 expunge() 的描述。

于 2012-07-31T12:43:59.950 回答