我已经使用smack api创建了一个xmpp客户端。并且为了创建名单条目,我使用了以下代码。
public void likeOnXmpp(UserFriendsBaseModel userFriendsBaseModel) {
AbstractXMPPConnection con = null;
try {
con = new SecuredXmppConnectionImpl(SecuredXmppConnectionImpl.getConfig(xmppResource.getXmppHost(), Integer.parseInt(xmppResource.getXmppSSLPort()))).connect();
if (con != null) {
xmppLogin(con, String.valueOf(userFriendsBaseModel.getUserId()), generatePassword(userFriendsBaseModel.getUserId()));
addFriendInXMPP(String.valueOf(userFriendsBaseModel.getUserId()),
generatePassword(userFriendsBaseModel.getUserId()),
String.valueOf(userFriendsBaseModel.getFriendId()),
String.valueOf(generatePassword(userFriendsBaseModel
.getFriendId())),
getProfileName(userFriendsBaseModel.getFriendId()), con);
removeRosterFailureRequest(userFriendsBaseModel);
} else {
addRosterFailureRequest(userFriendsBaseModel);
logger.info("LIKE saved in Roster Failure : "+userFriendsBaseModel.getUserId()+" -> "+userFriendsBaseModel.getFriendId());
}
} catch (XMPPException | SmackException | IOException
| InternalServerError e1) {
logger.error("unable to login at xmpp ", e1);
addRosterFailureRequest(userFriendsBaseModel);
logger.info("LIKE saved in Roster Failure : "+userFriendsBaseModel.getUserId()+" -> "+userFriendsBaseModel.getFriendId());
}
将Roster方法代码添加为:
private void addFriendInXMPP(String username, String password, String contactUserName, String contactPassword, String name,
AbstractXMPPConnection userConnection) throws InternalServerError {
try {
userConnection.setPacketReplyTimeout(20000);
xmppRosterManager.createEntry(userConnection, getBareJID(contactUserName), name, new String[] { "Friends" });
} catch (XMPPException e) {
logger.error("unable to create roster entry : ", e);
throw new InternalServerError();
} catch (SmackException e) {
logger.error("unable to create roster entry : ", e);
throw new InternalServerError();
}
}
和createEntry:
@Override
public void createEntry(AbstractXMPPConnection connection, String jId, String name, String[] groups)
throws XMPPException, NotLoggedInException, NoResponseException, NotConnectedException {
//getting instance of a roster
Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
Roster.getInstanceFor(connection).createEntry(jId, name, groups); // adding a entry in roster
}
这是SecuredXmppConnectionImpl类的代码
public class SecuredXmppConnectionImpl extends XmppConnectionImpl {
public SecuredXmppConnectionImpl(XMPPTCPConnectionConfiguration config) {
super(config);
}
/**
* Returns default configuration for Old SSL port method
*
* @param serviceName
* @param port
* @return
* @throws GeneralSecurityException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static XMPPTCPConnectionConfiguration getConfig(String serviceName,
int port) throws KeyManagementException, NoSuchAlgorithmException {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(
null, // KeyManager not required
new TrustManager[] { new DummyTrustManager() },
new java.security.SecureRandom());
SSLSocketFactory ssf = sslcontext.getSocketFactory();
return XMPPTCPConnectionConfiguration.builder()
.setServiceName(serviceName).setHost(serviceName).setPort(port)
.setSocketFactory(ssf).build();
}
static class DummyTrustManager implements X509TrustManager {
public boolean isClientTrusted(X509Certificate[] cert) {
return true;
}
public boolean isServerTrusted(X509Certificate[] cert) {
try {
cert[0].checkValidity();
return true;
} catch (CertificateExpiredException e) {
return false;
} catch (CertificateNotYetValidException e) {
return false;
}
}
public void checkClientTrusted(X509Certificate[] x509Certificates,
String s) throws CertificateException {
// Do nothing for now.
}
public void checkServerTrusted(X509Certificate[] x509Certificates,
String s) throws CertificateException {
// Do nothing for now.
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
}
但有时我得错误为:
key =javax.crypto.spec.SecretKeySpec@15f8b
2016-03-09 05:15:50,834 [http-bio-8080-exec-698] INFO (TokenHelper.java:81) - token valid
2016-03-09 05:15:50,872 [_bgExecutor-2] ERROR (UserManagerImpl.java:899) - unable to create roster entry :
org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 20000ms (~20s). Used filter: IQReplyFilter: iqAndIdFilter (AndFilter: (OrFilter: (IQTypeFilter: type=error, IQTypeFilter: type=result), StanzaIdFilter: id=bb8Lb-1268146)), : fromFilter (OrFilter: (FromMatchesFilter (full): null, FromMatchesFilter (bare): 9223370581384268972@prdmastchat01.matchstixapp.com, FromMatchesFilter (full): prdmastchat01.matchstixapp.com)).
at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:106)
at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:90)
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:229)
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:213)
at org.jivesoftware.smack.roster.Roster.createEntry(Roster.java:484)
at com.craterzone.xmpprestconnector.user.impl.XMPPRosterManagerImpl.createEntry(XMPPRosterManagerImpl.java:31)
at com.craterzone.matchstix.manager.impl.UserManagerImpl.addFriendInXMPP(UserManagerImpl.java:894)
at com.craterzone.matchstix.manager.impl.UserManagerImpl.likeOnXmpp(UserManagerImpl.java:462)
at com.craterzone.matchstix.manager.impl.UserManagerImpl$3.run(UserManagerImpl.java:411)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
2016-03-09 05:15:50,872 [_bgExecutor-2] ERROR (UserManagerImpl.java:477) - unable to login at xmpp
com.craterzone.matchstix.exception.InternalServerError: Server Error
at com.craterzone.matchstix.manager.impl.UserManagerImpl.addFriendInXMPP(UserManagerImpl.java:900)
at com.craterzone.matchstix.manager.impl.UserManagerImpl.likeOnXmpp(UserManagerImpl.java:462)
at com.craterzone.matchstix.manager.impl.UserManagerImpl$3.run(UserManagerImpl.java:411)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Mar 09, 2016 5:15:50 AM org.jivesoftware.smack.AbstractXMPPConnection callConnectionClosedOnErrorListener
WARNING: Connection closed with error
org.jivesoftware.smack.XMPPException$StreamErrorException: conflict You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions
<stream:error><conflict xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error>
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:994)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:935)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:950)
at java.lang.Thread.run(Thread.java:745)
我无法识别问题,我该如何解决?或者我需要做什么才能在给定时间内收听回复节?
提前致谢