[BAEL-2270] Guide to XMPP Smack Client (#5959)
Smack Library - Simple chat client
This commit is contained in:
parent
14771a3b2c
commit
18e1a3067d
|
@ -1 +1,5 @@
|
|||
log4j.rootLogger=INFO, stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
|
@ -675,6 +675,30 @@
|
|||
<version>${mockftpserver.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.igniterealtime.smack</groupId>
|
||||
<artifactId>smack-tcp</artifactId>
|
||||
<version>${smack.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.igniterealtime.smack</groupId>
|
||||
<artifactId>smack-im</artifactId>
|
||||
<version>${smack.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.igniterealtime.smack</groupId>
|
||||
<artifactId>smack-extensions</artifactId>
|
||||
<version>${smack.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.igniterealtime.smack</groupId>
|
||||
<artifactId>smack-java7</artifactId>
|
||||
<version>${smack.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
@ -896,6 +920,7 @@
|
|||
<derive4j.version>1.1.0</derive4j.version>
|
||||
<mockftpserver.version>2.7.1</mockftpserver.version>
|
||||
<commons-net.version>3.6</commons-net.version>
|
||||
<smack.version>4.3.1</smack.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.smack;
|
||||
|
||||
import org.jivesoftware.smack.AbstractXMPPConnection;
|
||||
import org.jivesoftware.smack.chat2.Chat;
|
||||
import org.jivesoftware.smack.chat2.ChatManager;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class StanzaThread implements Runnable {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(StanzaThread.class);
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
XMPPTCPConnectionConfiguration config = null;
|
||||
try {
|
||||
config = XMPPTCPConnectionConfiguration.builder()
|
||||
.setUsernameAndPassword("baeldung2","baeldung2")
|
||||
.setXmppDomain("jabb3r.org")
|
||||
.setHost("jabb3r.org")
|
||||
.build();
|
||||
|
||||
AbstractXMPPConnection connection = new XMPPTCPConnection(config);
|
||||
connection.connect();
|
||||
connection.login();
|
||||
|
||||
ChatManager chatManager = ChatManager.getInstanceFor(connection);
|
||||
|
||||
Chat chat = chatManager.chatWith(JidCreate.from("baeldung@jabb3r.org").asEntityBareJidOrThrow());
|
||||
|
||||
chat.send("Hello!");
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
package com.baeldung.smack;
|
||||
|
||||
import org.jivesoftware.smack.AbstractXMPPConnection;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.chat2.ChatManager;
|
||||
import org.jivesoftware.smack.filter.StanzaTypeFilter;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class SmackIntegrationTest {
|
||||
|
||||
private static AbstractXMPPConnection connection;
|
||||
private Logger logger = LoggerFactory.getLogger(SmackIntegrationTest.class);
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws IOException, InterruptedException, XMPPException, SmackException {
|
||||
|
||||
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
|
||||
.setUsernameAndPassword("baeldung","baeldung")
|
||||
.setXmppDomain("jabb3r.org")
|
||||
.setHost("jabb3r.org")
|
||||
.build();
|
||||
|
||||
XMPPTCPConnectionConfiguration config2 = XMPPTCPConnectionConfiguration.builder()
|
||||
.setUsernameAndPassword("baeldung2","baeldung2")
|
||||
.setXmppDomain("jabb3r.org")
|
||||
.setHost("jabb3r.org")
|
||||
.build();
|
||||
|
||||
connection = new XMPPTCPConnection(config);
|
||||
connection.connect();
|
||||
connection.login();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendMessageWithChat_thenReceiveMessage() throws XmppStringprepException, InterruptedException {
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
ChatManager chatManager = ChatManager.getInstanceFor(connection);
|
||||
final String[] expected = {null};
|
||||
|
||||
new StanzaThread().run();
|
||||
|
||||
chatManager.addIncomingListener((entityBareJid, message, chat) -> {
|
||||
logger.info("Message arrived: " + message.getBody());
|
||||
expected[0] = message.getBody();
|
||||
latch.countDown();
|
||||
});
|
||||
|
||||
latch.await();
|
||||
Assert.assertEquals("Hello!", expected[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendMessage_thenReceiveMessageWithFilter() throws XmppStringprepException, InterruptedException {
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
final String[] expected = {null};
|
||||
|
||||
new StanzaThread().run();
|
||||
|
||||
connection.addAsyncStanzaListener(stanza -> {
|
||||
if (stanza instanceof Message) {
|
||||
Message message = (Message) stanza;
|
||||
expected[0] = message.getBody();
|
||||
latch.countDown();
|
||||
}
|
||||
}, StanzaTypeFilter.MESSAGE);
|
||||
|
||||
latch.await();
|
||||
Assert.assertEquals("Hello!", expected[0]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue