2012-06-26 102 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連接在重新部署戰爭包時總是斷開連接。