java中如何通过Smack实现流管理?

Posted

技术标签:

【中文标题】java中如何通过Smack实现流管理?【英文标题】:how to implement Stream Management through Smack in java? 【发布时间】:2017-03-21 08:58:30 【问题描述】:

如何使用 smack 4.1 中的 XEP 198(流管理)使用 java ,请分享一些参考。

我正在使用 java 代码将消息从 smack 发送到 spark

 private static String username = "test";
        private static String password = "test123";

        public static class MessageParrot implements PacketListener 
            private XMPPConnection xmppConnection;

            public MessageParrot(XMPPConnection conn) 
                xmppConnection = conn;
            

            public void processPacket(Packet packet) 
                Message message = (Message)packet;
                if(message.getBody() != null) 
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                    System.out.println("Message from " + fromName + "\n" + message.getBody() + "\n");
                    Message reply = new Message();
                    reply.setTo(fromName);
                    reply.setBody("I am a Java bot. You said: " + message.getBody());
                    xmppConnection.sendPacket(reply);
                
            
        


        public static void main(String[] args ) 




            System.out.println("Starting IM client");

            // gtalk requires this or your messages bounce back as errors
            ConnectionConfiguration connConfig = new ConnectionConfiguration("domain.com", 5222);
            XMPPConnection connection = new XMPPConnection(connConfig);

            try 
                connection.connect();
                System.out.println("Connected to " + connection.getHost());
             catch (XMPPException ex) 
                //ex.printStackTrace();
                System.out.println("Failed to connect to " + connection.getHost());
                System.exit(1);
            
            try 
                connection.login(username, password);
                System.out.println("Logged in as " + connection.getUser());

                Presence presence = new Presence(Presence.Type.available);

                connection.sendPacket(presence);

             catch (XMPPException ex) 
                //ex.printStackTrace();
                // XMPPConnection only remember the username if login is succesful
                // so we can''t use connection.getUser() unless we log in correctly
                System.out.println("Failed to log in as " + username);
                System.exit(1);
            

            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new MessageParrot(connection), filter);

          /*  if(args.length > 0) */
                // google bounces back the default message types, you must use chat
                Message msg = new Message("test2@domain.com", Message.Type.chat);
                msg.setBody("hi");
              //  msg.addExtension(new DeliveryReceipt(msg.getPacketID()));

               /* XhtmlExtension xhtmlExtension = new XHTMLExtension();
                xhtmlExtension.addBody(
                "<body>My lord, dispatch; read o'er these articles.</body><request xmlns='urn:xmpp:receipts'/>");
                msg.addExtension(xhtmlExtension);*/


               /* MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
                DeliveryReceiptManager.addDeliveryReceiptRequest(msg);*/
                connection.sendPacket(msg);


            System.out.println("Press enter to disconnect\n");

            try 
                System.in.read();
             catch (IOException ex) 
                //ex.printStackTrace();
            

            connection.disconnect();
        

通过这个我可以和两个客户端聊天,一个来自 smack,另一个来自 spark,如何在这里实现流管理?

【问题讨论】:

【参考方案1】:

创建连接对象后使用下面的代码

 connection.setUseStreamManagement(true);
 connection.setUseStreamManagementResumptionDefault(true);

之后查看&lt;enable xmlns='urn:xmpp:sm:3' resume='true'/&gt; 的日志,这意味着客户端想要使用流管理。服务器将响应此(如果支持)&lt;enabled xmlns='urn:xmpp:sm:3' id='.......' resume='true' max='1'/&gt;,这意味着流现已启用

完成后,如果您不断检查日志,您会发现 &lt;r xmlns='urn:xmpp:sm:3'/&gt;&lt;a xmlns='urn:xmpp:sm:3' h='1'/&gt; 在客户端和服务器之间进行交换。

【讨论】:

以上是关于java中如何通过Smack实现流管理?的主要内容,如果未能解决你的问题,请参考以下文章

数据处理平台架构中的SMACK组合:SparkMesosAkkaCassandra以及Kafka

无法使用教程“使用 Smack 实现基于 XMPP 的应用服务器的 Java 示例”

openfire+smack怎么实现类似于qq群的功能?

如何在 ejabberd 服务器中启用流管理?

在smack中阻止用户[关闭]

在 Android Studio 上使用 Smack 构建 XML 流消息