gnu.mail.providers.nntp.NNTPMessage的容器类

时间:2012-04-03 00:02:07

标签: java sockets jsp jsf facelets

为什么Glassfish会显示此facelets错误:

javax.el.ELException: /foo/client.xhtml @11,74 value="#{messageBean.messages}": javax.mail.MessagingException: Socket closed;
  nested exception is:
    java.net.SocketException: Socket closed

我将其归因于在localhost上频繁且过快地点击leafnode。下面的Glassfish日志显示了从leafnode成功的消息检索,但多次调用SingletonNNTP.loadMessages()。

如何正确处理套接字?有时Leafnode会提供所需的输出,有时不会。

INFO: Initializing Mojarra 2.1.6 (SNAPSHOT 20111206) for context '/NNTPjsf'
INFO: WEB0671: Loading application [NNTPjsf] at [/NNTPjsf]
INFO: NNTPjsf was successfully deployed in 2,574 milliseconds.
INFO: MessageBean..
INFO: MessageBean..
INFO: MessageBean.getMessages..
INFO: MessageBean.getNNTP..
INFO: MAKING SINGLETON..
INFO: NNTP.loadMessages...
INFO: NNTP.loadMessages...
nntp: <200 Leafnode NNTP Daemon, version 1.11.8 running at localhost (my fqdn: dur.bounceme.net)
nntp: >GROUP comp.lang.java.help
nntp: <211 82 3 84 comp.lang.java.help group selected
nntp: >GROUP comp.lang.java.help
nntp: <211 82 3 84 comp.lang.java.help group selected
nntp: >XHDR Message-ID 3-84
nntp: <221 Message-ID header (from overview) for postings 3-84:
nntp: <3 <afp6m75oi6nli4b6q87s317lkc13g689c2@4ax.com>
nntp: <4 <ed7f31e9-8a19-46c7-9a7c-ad8aabfb9599@x10g2000pbi.googlegroups.com>
nntp: <5 <uA4ar.13560$fj7.13111@newsfe20.iad>

支持bean如下:

package net.bounceme.dur.nntp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.inject.Named;
import javax.mail.Message;

@Named
@SessionScoped
public class MessageBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(MessageBean.class.getName());
    private static Level level = Level.INFO;
    private DataModel dm = null;

    public MessageBean() {
        logger.log(level, "MessageBean..");
    }

    public List<Message> getMessages() throws Exception {
        logger.log(level, "MessageBean.getMessages..");
        List<Message> messages = getNNTP();
        return messages;
    }

    public DataModel getModel() throws Exception {
        logger.log(level, "MessageBean.getModel..");
        List<Message> messages = getNNTP();
        dm = new ListDataModel(messages);
        return dm;
    }

    private synchronized List<Message> getNNTP() throws Exception {
        logger.log(level, "MessageBean.getNNTP..");
        List<Message> messages = new ArrayList<Message>();
        SingletonNNTP nntp = SingletonNNTP.INSTANCE;
        messages = nntp.getMessages(false);
        logger.log(level, "MessageBean.getNNTP nntp.size:  {0}", messages.size());
        return messages;
    }
}

单身人士:

package net.bounceme.dur.nntp;

import static java.lang.System.out;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;

public enum SingletonNNTP {

    INSTANCE;
    private final Logger logger = Logger.getLogger(SingletonNNTP.class.getName());
    private final Level level = Level.INFO;
    private Properties props = new Properties();
    private List<Message> messages = new ArrayList<Message>();

    private SingletonNNTP() {
        out.println("MAKING SINGLETON..");
        props = PropertiesReader.getProps();
        boolean loaded = false;
        try {
            loaded = setMessages(false);
        } catch (Exception ex) {
            Logger.getLogger(SingletonNNTP.class.getName()).log(Level.SEVERE, "FAILED TO LOAD MESSAGES", ex);
        }
    }

    public List<Message> getMessages(boolean debug) throws Exception {
        logger.log(level, "NNTP.getMessages...");
        logMessages();
        return Collections.unmodifiableList(messages);
    }

    private boolean setMessages(boolean debug) throws Exception {
        logger.log(level, "NNTP.loadMessages...");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(debug);
        Store store = session.getStore(new URLName(props.getProperty("nntp.host")));
        store.connect();
        Folder root = store.getDefaultFolder();
        Folder folder = root.getFolder(props.getProperty("nntp.group"));
        folder.open(Folder.READ_ONLY);
        Message[] msgs = folder.getMessages();
        messages = Arrays.asList(msgs);
        folder.close(false);
        store.close();
        if (debug) {

        }
        return true;
    }

    private void logMessages() throws Exception {
        logger.log(level, "NNTP.logMessages..");
        for (Message m : messages) {
            logger.log(level, String.valueOf(m.getMessageNumber()));
            logger.log(level, m.getSubject());
            logger.log(level, m.getContent().toString());
        }
    }

    private void persistMessages() throws Exception {
        //entities.Messages
    }
}

1 个答案:

答案 0 :(得分:1)

该消息表示您关闭了套接字,然后尝试读取或写入它。您正在尝试在关闭连接后阅读邮件内容。

,当连接打开时,您需要保存消息本身而不是内容。