一尘不染

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

jsp

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

阅读 233

收藏
2020-06-10

共1个答案

一尘不染

该消息表示您关闭了套接字,然后尝试对其进行读取或写入。关闭连接后,您正在尝试阅读消息内容。打开连接时,您不需要保存消息本身,而是保存其内容。

2020-06-10