2012-06-26 97 views
0

我想在我的JSF应用程序中添加额外的功能,用于将消息发送给我公司的聊天用户。为了开发这个项目,我使用了NetBeans v7.1,GlassFish v3.1.1和Smack API 3.2.2。在我的Web应用程序中,我创建了CDI ApplicationScoped bean来管理XMPP连接。集成JSF和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("[email protected]", 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秒的时间间隔。这个错误是什么以及如何解决它?

回答

0

只是不要忘记断开会话。

检查您的Jabber连接在重新部署战争包时总是断开连接。