集成JSF和XMPP

时间:2012-06-26 06:16:11

标签: java jsf xmpp

我想在我的JSF应用程序中添加附加功能,以便向我公司的聊天用户发送消息。对于开发这个项目,我使用NetBeans v7.1,GlassFish v3.1.1和Smack API 3.2.2。在我的Web应用程序中,我创建了CDI ApplicationScoped bean来管理XMPP连接。

@Named("xmpp")
@ApplicationScoped    
public class XMPPBean implements Serializable {

    private org.apache.log4j.Logger loggerErr = org.apache.log4j.Logger.getLogger("err");

    private XMPPConnection connection = null;

    private String chatHostName = "localhost";
    private int chatport = 5222;
    private String userName = "testuser";
    private String userPass = "12345678";


    public void connect(){       

        ConnectionConfiguration config = new ConnectionConfiguration(chatHostName, chatport);
        connection = new XMPPConnection(config);
        try {            
            connection.connect();            
            connection.login(userName, userPass);            
        } catch (XMPPException e) {            
            loggerErr.error(e.getMessage(), e);            
        }
    }


    public void sendMessage(){

        ChatManager chatmanager = connection.getChatManager();
        Chat newChat = chatmanager.createChat("anotheruser@127.0.0.1", new MessageListener() {
            @Override
            public void processMessage(Chat chat, Message message) {
                System.out.println("Received message: " + " "+message.toXML());
            }
        });

        try {
            newChat.sendMessage("Howdy!");
        } catch (XMPPException e) {
            System.out.println("Error Delivering block");
        }
    }    

    public void disconnect() {
        connection.disconnect();
    }

    public boolean chackConnection(){                
        return connection==null?false:connection.isConnected();
    }


    public String getLoggedinUser() {        
        if (connection == null )
            return "";                
        return connection.getUser();                   
    }       

}

一切正常。我可以连接到XMPP服务器,发送和接收消息。但在我的服务器日志中,我会定期看到异常:

SEVERE: java.lang.NullPointerException
    at org.xmlpull.mxp1.MXParser.isNameStartChar(MXParser.java:3157)
    at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1253)
    at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
    at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:325)
    at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
    at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:70)

SEVERE:     at org.xmlpull.mxp1.MXParser.isNameStartChar(MXParser.java:3157)
SEVERE:     at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1253)
SEVERE:     at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
SEVERE:     at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:325)
SEVERE:     at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
SEVERE:     at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:70)
SEVERE: java.lang.NullPointerException
    at org.jivesoftware.smackx.commands.AdHocCommandManager$4.connectionClosedOnError(AdHocCommandManager.java:268)
    at org.jivesoftware.smack.PacketReader.notifyConnectionError(PacketReader.java:172)
    at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:332)
    at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
    at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:70)

SEVERE:     at org.jivesoftware.smackx.commands.AdHocCommandManager$4.connectionClosedOnError(AdHocCommandManager.java:268)
SEVERE:     at org.jivesoftware.smack.PacketReader.notifyConnectionError(PacketReader.java:172)
SEVERE:     at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:332)
SEVERE:     at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
SEVERE:     at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:70)

此异常在调用XMPP函数后出现,或者以10-15秒的间隔定期出现。这个错误是什么以及如何修复它?

1 个答案:

答案 0 :(得分:0)

不要忘记断开会话。

检查您的jabber连接是否始终在war-package重新部署时断开连接。