一步步调通Openfire3.10.2+Smack4.1.4官方Demo

本文介绍如何使用Smack库连接Openfire XMPP服务器的过程,并解决连接过程中出现的各种依赖缺失问题。
首先在电脑上安装Openfire服务器(本文安装的版本为Openfire 3.10.2)

Openfire下载地址http://www.igniterealtime.org/downloads/index.jsp 
本文Openfire服务器名称为192.169.20.246
Smack 4.1.4的官方文档下载地址包括jar包和javadoc(官方给的解释Easy to use Java XMPP client library.) 
http://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_4_1_4.zip

下载Smack之后从文档smack_4_1_4/releasedocs/documentation/index.html给的示例

 // Create a connection to the gwcheng-pc server on a specific port.
    XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
              .setUsernameAndPassword("<span style="color: rgb(0, 136, 0); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre;">wangzheng</span>", "<span style="color: rgb(0, 136, 0); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre;">wz20120404</span>")
              .setServiceName("<span style="color: rgb(136, 0, 0); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre;">192.169.20.246</span>")
              .setHost("<span style="color: rgb(136, 0, 0); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre;">192.169.20.246</span>")
              .build();
            AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
            conn2.connect();
建立一个java工程,拷贝smack_4_1_4\libs\下的jar包到自己的工程里。 

运行上面的代码(代码中”wangzheng”和"wz20120404"是我在openfire中注册的用户名和密码”192.169.20.246”为我的openfire服务器名称 ) 
运行上面的程序,会报如下的错误

 Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
    Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory

这个错误很明显是缺少jar包 xmlpull.jar,此处导入xmlpull-1.1.3.1.jar 
导入之后运行还是会报错

 Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.IllegalStateException: org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available
    Caused by: org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available

这种错误还是缺少jar包 kxml.jar,此处导入kxml2-2.3.0.jar 
导入之后运行还是会报错

  Exception in thread "main" java.lang.NoClassDefFoundError: de/measite/minidns/DNSCache
    Caused by: java.lang.ClassNotFoundException: de.measite.minidns.DNSCache

继续导入jar包 minidns.jar,此处导入minidns-0.1.7.jar 
导入之后运行还是会报错

  Exception in thread "main" java.lang.NoClassDefFoundError: org/jxmpp/util/cache/ExpirationCache
    Caused by: java.lang.ClassNotFoundException: org.jxmpp.util.cache.ExpirationCache

继续导入jar包 jxmpp-core-0.5.0-alpha6.jar和 jxmpp-util-cache-0.4.0-alpha1.jar 
导入之后运行还是会报错

 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/StrictHostnameVerifier
    Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.ssl.StrictHostnameVerifier

继续导入jar包 httpclient-4.4.1.jar 
导入之后运行还是会报错

  Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

继续导入jar包 commons-logging-1.2.jar 
导入之后运行还是会报错

org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

这个错误造成的原因是:Openfire 服务器(安全设置问题),这个安全证书问题,我会单独抽出来,进行详细的说明。

openfire服务器端解决办法:
“服务器设置”--“安全设置”---将“客户端安全联接”中由“非必须”,修改为“自定义”,另外把“旧的SSL方式”和“TLS方式”都设置为无效。


至此,我们完成了XMPP服务开发第一步:XMPP服务器连接

package com.openfire.util;

import java.io.IOException;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

public class ConnectionUtil {
	//简单连接
	public boolean connect(String username,String password,String serverName){	
		boolean target=false;
		XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
				  .setUsernameAndPassword(username, password)
				  .setServiceName(serverName)
				  .setHost(serverName)
				  .setPort(5222)
				  .build();
		AbstractXMPPConnection conn = new XMPPTCPConnection(config);
		try {
			conn.connect();
			target = conn.isConnected();
			if(target){
				System.out.println("XMPP 服务器连接成功");
			}else{
				System.out.println("XMPP 服务器连接不成功");
			}
		} catch (SmackException | IOException | XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return target;
		
	}

}

package com.openfire.main;

import com.openfire.util.ConnectionUtil;

public class ConnectionTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConnectionUtil connection=new ConnectionUtil();
		connection.connect("wangzheng", "wz20120404", "192.169.20.246");
	}

}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值