我想在我的 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 秒定期出现。这是什么错误以及如何解决?