首先需要安装jdk


我用的是

openfire_4_0_4.tar.gz

tar -zvxf openfire_4_0_4.tar.gz

cd openfire/bin

./openfire start 启动

登录到你安装服务器的ip:9090默认端口号为9090

如:192.168.1.252:9090


下面是您的数据库和 JDBC 驱动程序的属性列表。


数据库连接信息

数据库和版本:

MySQL 5.5.49-log

JDBC 驱动程序:

MySQL Connector Java

JDBC 驱动程序版本:

mysql-connector-java-5.1.30 ( Revision: alexander.soklakov@oracle.com-20140310090514-8xt1yoht5ksg2e7c )

DB 连接 URL:

jdbc:mysql://localhost:3306/of?rewriteBatchedStatements=true

0DB 用户:

root@localhost

事务支持:

Yes

事务隔离级别:

TRANSACTION_REPEATABLE_READ

支持多连接

一次打开:

Yes

在只读方式中:

No




连接池信息

House Keeping Interval:

30 秒

最大连接生命周期:

86400 秒

最小连接数:

5

最大连接数:

25

连接测试sql:

select 1

在使用前测试连接:

No

在使用后测试连接:

No

连接数:

0 (活跃), 5 (可用的), 25 (最大)

连接成功:

101

连接被拒绝:

48

连接详细信息:


 





开始写java代码进行聊天

注意:先配置完openfire,开启服务

登录ip:9090进后台可以先创建两个用户,user1和user2密码都为123456


smack.jar
smackx.jar

需要这两个jar包

下载地点

http://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_3_2_2.zip

开一个项目把jar包放入lib里面

Java opennlp 中文训练 java openfire_服务器

下面是做演示作用的,完全可以用一个公共方法进行封装,主要是为了做区分,和模拟两个文件中的人进行聊天

创建文件1,:openfire2Action

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import java.util.Scanner;

public class openFire3Action {
    private Connection connection;
    private ConnectionConfiguration config;
    /** openfire服务器address */
    private final static String server = "wen";
    
    public void init() {
        try {
            //config = new ConnectionConfiguration(server,5222);
            ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.252", 5222, server);
            /*config.setCompressionEnabled(true);
            *//** 是否启用安全验证 *//*
            config.setSASLAuthenticationEnabled(true);
            *//** 是否启用调试 *//*
            config.setDebuggerEnabled(false);*/
            config.setReconnectionAllowed(false);
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setSendPresence(true);
            /** 创建connection链接 */
            connection = new XMPPConnection(config);
            /** 建立连接 */
            connection.connect();
        } catch (XMPPException e) {
            e.printStackTrace();
        }
    }

    public void destory() {
        if (connection != null) {
            connection.disconnect();
            connection = null;
        }
    }
    public static void main(String[] args) {
        openFire3Action smack = new openFire3Action();
        FromUser fromUser = new FromUser();
        fromUser.setUsername("user2");
        fromUser.setPassword("123456");
        smack.sendMsg(fromUser, "user1","");
        smack.destory();
    }
    public void sendMsg(FromUser fromUser, String toUserName, String msg) {
        init();
        try {
            connection.login(fromUser.getUsername(), fromUser.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        ChatManager chatmanager = connection.getChatManager();
        Chat newChat = chatmanager.createChat(toUserName + "@" + server, new MessageListener() {
            public void processMessage(Chat chat, Message message) {
                if (message.getBody() != null) {
                    System.out.println("name 【"
                            + message.getFrom() + "】 地瓜: "
                            + message.getBody());
                }
            }
        });
        Scanner input = new Scanner(System.in);
        while (true) {
            String message = input.nextLine();
            try {
                newChat.sendMessage(message);
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
        /*try {
            destory();
        }catch (Exception e){
            e.printStackTrace();
        }*/
    }
}

创建文件二:openFire3Action

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import java.util.Scanner;

public class openFire3Action {
    private Connection connection;
    private ConnectionConfiguration config;
    /** openfire服务器address */
    private final static String server = "wen";

    public void init() {
        try {
            //config = new ConnectionConfiguration(server,5222);
            ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.252", 5222, server);
            /*config.setCompressionEnabled(true);
            *//** 是否启用安全验证 *//*
            config.setSASLAuthenticationEnabled(true);
            *//** 是否启用调试 *//*
            config.setDebuggerEnabled(false);*/
            config.setReconnectionAllowed(false);
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setSendPresence(true);
            /** 创建connection链接 */
            connection = new XMPPConnection(config);
            /** 建立连接 */
            connection.connect();
        } catch (XMPPException e) {
            e.printStackTrace();
        }
    }

    public void destory() {
        if (connection != null) {
            connection.disconnect();
            connection = null;
        }
    }
    public static void main(String[] args) {
        openFire3Action smack = new openFire3Action();
        FromUser fromUser = new FromUser();
        fromUser.setUsername("user2");
        fromUser.setPassword("123456");
        smack.sendMsg(fromUser, "user1","");
        smack.destory();
    }
    public void sendMsg(FromUser fromUser, String toUserName, String msg) {
        init();
        try {
            connection.login(fromUser.getUsername(), fromUser.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        ChatManager chatmanager = connection.getChatManager();
        Chat newChat = chatmanager.createChat(toUserName + "@" + server, new MessageListener() {
            public void processMessage(Chat chat, Message message) {
                if (message.getBody() != null) {
                    System.out.println("name 【"
                            + message.getFrom() + "】 地瓜: "
                            + message.getBody());
                }
            }
        });
        Scanner input = new Scanner(System.in);
        while (true) {
            String message = input.nextLine();
            try {
                newChat.sendMessage(message);
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
        /*try {
            destory();
        }catch (Exception e){
            e.printStackTrace();
        }*/
    }
}

执行效果

Java opennlp 中文训练 java openfire_System_02


Java opennlp 中文训练 java openfire_System_03



注意:下面是遇到的问题:

private final static String server = "wen";

其中,这个是openfire服务器的名称,

还有:这个是本地openfire连接的方式,用这种方式测试就可以了

ConnectionConfiguration 
 config = new ConnectionConfiguration(server,5222);
 config.setCompressionEnabled(true); 
/** 是否启用安全验证 */ 
config.setSASLAuthenticationEnabled(true); 
/** 是否启用调试 */ 
config.setDebuggerEnabled(false);




下面这种是openfire服务器安装在别的机器上:如果想要在公网上进行聊天,需要把openfire安装在有公网固定ip的机器上。


192.168.1.252是公网的ip,或者是本地局域网,中安装openfire服务的那台机器的ip,5222是端口,默认端口,server是openfire服务器名称

ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.252", 5222, server);
            config.setReconnectionAllowed(false);
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setSendPresence(true);




报错提示:


javax.net.ssl.SSLException: Received fatal alert: internal_error


这个错误我猜测是因为,你还没连接上openfire服务器,就登录导致的。


stream:error (conflict)


这个错误是多次登录用户导致的。


还有一个是server什么504的错误,是因为,连接不上

未完待续



继续

那么怎么添加openfire用户账号和密码呢?

代码如下,继续引用上面的文件

import net.sf.json.JSONObject;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class openFire3Action {
    private Connection connection;
    private ConnectionConfiguration config;
    /** openfire服务器address */
    private final static String server = "wen";

    /*public void init() {
        try {
            //config = new ConnectionConfiguration(server,5222);
            ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.252", 5222, server);
            *//*config.setCompressionEnabled(true);
            *//**//** 是否启用安全验证 *//**//*
            config.setSASLAuthenticationEnabled(true);
            *//**//** 是否启用调试 *//**//*
            config.setDebuggerEnabled(false);*//*
            config.setReconnectionAllowed(false);
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setSendPresence(true);
            *//** 创建connection链接 *//*
            connection = new XMPPConnection(config);
            *//** 建立连接 *//*
            connection.connect();
        } catch (XMPPException e) {
            e.printStackTrace();
        }
    }*/

    public XMPPConnection getXMPPConnection() {
        try {
            ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.252", 5222, server);
            config.setReconnectionAllowed(false);
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setSendPresence(true);
            XMPPConnection connection = new XMPPConnection(config);
            connection.connect();
            return connection;
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void destory() {
        if (connection != null) {
            connection.disconnect();
            connection = null;
        }
    }
    public static void main(String[] args) {
        openFire3Action smack = new openFire3Action();
        /*FromUser fromUser = new FromUser();
        fromUser.setUsername("user2");
        fromUser.setPassword("123456");
        smack.sendMsg(fromUser, "user1","");*/
        //创建用户的信息
        Map<String,String> map = new HashMap<String, String>();
        map.put("email","");
        map.put("name", "name");
        smack.register("user3",map,smack.getXMPPConnection());
        smack.destory();
    }
    /**
     * 注册账号
     * username 是账号名
     * map是邮箱,name,还有其他信息
     * XMPPConnection 是连接通道
     */
    public void register(String username, Map<String, String> attrMap,XMPPConnection XMPPConnection) {
        try {
            if (username != null && !username.isEmpty()) {
                //统一默认密码
                if(XMPPConnection!=null) {
                    //"1"为账号密码,默认都为
                    XMPPConnection.getAccountManager().createAccount(username, "123456", attrMap);
                }
            }
        } catch (XMPPException e) {
            e.printStackTrace();
        }
    }

    /*public void sendMsg(FromUser fromUser, String toUserName, String msg) {
        init();
        try {
            connection.login(fromUser.getUsername(), fromUser.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        ChatManager chatmanager = connection.getChatManager();
        Chat newChat = chatmanager.createChat(toUserName + "@" + server, new MessageListener() {
            public void processMessage(Chat chat, Message message) {
                if (message.getBody() != null) {
                    System.out.println("name 【"
                            + message.getFrom() + "】 地瓜: "
                            + message.getBody());
                }
            }
        });
        Scanner input = new Scanner(System.in);
        while (true) {
            String message = input.nextLine();
            try {
                newChat.sendMessage(message);
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
    }*/
}

效果如下创建了用户3


Java opennlp 中文训练 java openfire_Java opennlp 中文训练_04



如多人聊天,该怎么办呢?则需创建会议室,

下面则演示,会议室的创建。

首先,可以利用上面的代码创建用户,也可以直接在openfire的后台直接添加用户,怎么喜欢怎么来。

我现有如下用户

Java opennlp 中文训练 java openfire_System_05

创建一个公共的文件,openFire4Action代码如下

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.muc.DiscussionHistory;
import org.jivesoftware.smackx.muc.MultiUserChat;

import java.util.*;

/**
 * wen
 */
public class openFire4Action {
    private final static String server = "wen";
    Map<String,MultiUserChat> map = new HashMap<String, MultiUserChat>();

    /**
     * 创建会议室
     * @param roomName
     * @param connection
     */
    public void createChatRoom(String roomName, Connection connection){
        if (connection == null) {
            return;
        }
        try {
            // 创建一个MultiUserChat
            MultiUserChat muc = new MultiUserChat(connection, roomName
                    + "@conference." + connection.getServiceName());
            // 创建聊天室
            muc.create(roomName); // roomName房间的名字
            // 获得聊天室的配置表单
            Form form = muc.getConfigurationForm();
            // 根据原始表单创建一个要提交的新表单。
            Form submitForm = form.createAnswerForm();
            // 向要提交的表单添加默认答复
            for (Iterator<FormField> fields = form.getFields(); fields
                    .hasNext();) {
                FormField field = (FormField) fields.next();
                if (!FormField.TYPE_HIDDEN.equals(field.getType())
                        && field.getVariable() != null) {
                    // 设置默认值作为答复
                    submitForm.setDefaultAnswer(field.getVariable());
                }
            }
            // 设置聊天室的新拥有者
            List<String> owners = new ArrayList<String>();
            owners.add(connection.getUser());// 用户JID
            submitForm.setAnswer("muc#roomconfig_roomowners", owners);
            // 设置聊天室是持久聊天室,即将要被保存下来
            submitForm.setAnswer("muc#roomconfig_persistentroom", false);
            // 房间仅对成员开放
            submitForm.setAnswer("muc#roomconfig_membersonly", false);
            // 允许占有者邀请其他人
            submitForm.setAnswer("muc#roomconfig_allowinvites", true);
            // 进入是否需要密码
            //submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
            // 设置进入密码
            //submitForm.setAnswer("muc#roomconfig_roomsecret", "password");
            // 能够发现占有者真实 JID 的角色
            // submitForm.setAnswer("muc#roomconfig_whois", "anyone");
            // 登录房间对话
            submitForm.setAnswer("muc#roomconfig_enablelogging", true);
            // 仅允许注册的昵称登录
            submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
            // 允许使用者修改昵称
            submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
            // 允许用户注册房间
            submitForm.setAnswer("x-muc#roomconfig_registration", false);
            // 发送已完成的表单(有默认值)到服务器来配置聊天室
            submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
            // 发送已完成的表单(有默认值)到服务器来配置聊天室
            muc.sendConfigurationForm(submitForm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 加入会议室
     *
     * @param user 昵称
     * @param password 会议室密码
     * @param roomsName 会议室名
     * @param connection
     */
    public static MultiUserChat joinMultiUserChat(String user, String password, String roomsName,
                                                  Connection connection) {
        try {
            // 使用XMPPConnection创建一个MultiUserChat窗口
            MultiUserChat muc = new MultiUserChat(connection, roomsName
                    + "@conference." + connection.getServiceName());
            // 聊天室服务将会决定要接受的历史记录数量
            DiscussionHistory history = new DiscussionHistory();
            history.setMaxStanzas(0);
            // 用户加入聊天室
            muc.join(user, password, history, SmackConfiguration.getPacketReplyTimeout());
            System.out.println("会议室加入成功+++++++++++++++++++++++++++++++++");
            return muc;
        } catch (XMPPException e) {
            e.printStackTrace();
            System.out.println("会议室加入失败------------------------------");
            return null;
        }
    }

    /**
     * 连接openfire服务器
     * @return
     */
    public static XMPPConnection getXMPPConnection() {
        try {
            ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.252", 5222, server);
            config.setReconnectionAllowed(false);
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setSendPresence(true);
            XMPPConnection connection = new XMPPConnection(config);
            connection.connect();
            return connection;
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询会议室成员名字
     * @param muc
     */
    public static List<String> findMulitUser(MultiUserChat muc){
        List<String> listUser = new ArrayList<String>();
        Iterator<String> it = muc.getOccupants();
        //遍历出聊天室人员名称
        while (it.hasNext()) {
            // 聊天室成员名字
            String name = StringUtils.parseResource(it.next());
            listUser.add(name);
        }
        return listUser;
    }


    /**
     * 用户登录和发送
     * @param username
     * @param password
     * @param roomusername
     * @param roompasswd
     * @param roomname
     * @param flag
     */
    //flag 是分辨,是否是创建会议室的用户登录
    public static void getConferenceRoom(String username,String password,String roomusername,String roompasswd,String roomname,boolean flag) {
        //连接openfire服务器
        Connection c = openFire4Action.getXMPPConnection();
        //自己必须先登录,不然会报错,
        try {
            c.login(username,password);
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        if(flag){
            openFire4Action smack4 = new openFire4Action();
            //创建一个会议室,“yiqihai”C是连接服务器的通道
            smack4.createChatRoom("yiqihai", c);
        }
        MultiUserChat mu = openFire4Action.joinMultiUserChat(roomusername, roompasswd, roomname, c);
        mu.addMessageListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    System.out.println("【"
                            + message.getFrom() + "】: "
                            + message.getBody());
                }
            }
        });
        Scanner input = new Scanner(System.in);
        while (true) {

            String message = input.nextLine();
            try {
                mu.sendMessage(message);
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
    }
}



下面我再建立4个文件进行模拟用户,

分别为

PersonClass1

PersonClass2

PersonClass3

PersonClass4

代码如下:

PersonClass1:

/**
 * 用户1
 */
public class PersonClass1 {
    public static void main(String[] args) {
        openFire4Action.getConferenceRoom("user3","1","土豆1","1","yiqihai",true);
    }
}


PersonClass2:

/**
 * 用户2
 */
public class PersonClass2 {

    public static void main(String[] arge){
        openFire4Action.getConferenceRoom("user2","123456","土豆2","1","yiqihai",false);
    }
}


PersonClass3:

/**
 * 用户三
 */
public class PersonClass3 {

    public static void main(String[] args) {

        openFire4Action.getConferenceRoom("user5","123456","土豆3","1","yiqihai",false);
    }
}


PersonClass4:

/**
 * 用户四
 */
public class PersonClass4 {
    public static void main(String[] args) {
        openFire4Action.getConferenceRoom("user6","123456","土豆4","1","yiqihai",false);
    }
}



效果如下:


Java opennlp 中文训练 java openfire_服务器_06


Java opennlp 中文训练 java openfire_System_07

Java opennlp 中文训练 java openfire_Java opennlp 中文训练_08

Java opennlp 中文训练 java openfire_Java opennlp 中文训练_09