Skip to content

Commit 18e1a30

Browse files
freddyaottdiegomoreira001
authored andcommitted
[BAEL-2270] Guide to XMPP Smack Client (eugenp#5959)
Smack Library - Simple chat client
1 parent 14771a3 commit 18e1a30

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎libraries/log4j.properties‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
log4j.rootLogger=INFO, stdout
2+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3+
log4j.appender.stdout.Target=System.out
4+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5+
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

‎libraries/pom.xml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,30 @@
675675
<version>${mockftpserver.version}</version>
676676
<scope>test</scope>
677677
</dependency>
678+
679+
<dependency>
680+
<groupId>org.igniterealtime.smack</groupId>
681+
<artifactId>smack-tcp</artifactId>
682+
<version>${smack.version}</version>
683+
</dependency>
684+
685+
<dependency>
686+
<groupId>org.igniterealtime.smack</groupId>
687+
<artifactId>smack-im</artifactId>
688+
<version>${smack.version}</version>
689+
</dependency>
690+
691+
<dependency>
692+
<groupId>org.igniterealtime.smack</groupId>
693+
<artifactId>smack-extensions</artifactId>
694+
<version>${smack.version}</version>
695+
</dependency>
696+
697+
<dependency>
698+
<groupId>org.igniterealtime.smack</groupId>
699+
<artifactId>smack-java7</artifactId>
700+
<version>${smack.version}</version>
701+
</dependency>
678702
</dependencies>
679703

680704
<repositories>
@@ -896,6 +920,7 @@
896920
<derive4j.version>1.1.0</derive4j.version>
897921
<mockftpserver.version>2.7.1</mockftpserver.version>
898922
<commons-net.version>3.6</commons-net.version>
923+
<smack.version>4.3.1</smack.version>
899924
</properties>
900925

901926
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.smack;
2+
3+
import org.jivesoftware.smack.AbstractXMPPConnection;
4+
import org.jivesoftware.smack.chat2.Chat;
5+
import org.jivesoftware.smack.chat2.ChatManager;
6+
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
7+
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
8+
import org.jxmpp.jid.impl.JidCreate;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
public class StanzaThread implements Runnable {
13+
14+
private Logger logger = LoggerFactory.getLogger(StanzaThread.class);
15+
16+
@Override
17+
public void run() {
18+
XMPPTCPConnectionConfiguration config = null;
19+
try {
20+
config = XMPPTCPConnectionConfiguration.builder()
21+
.setUsernameAndPassword("baeldung2","baeldung2")
22+
.setXmppDomain("jabb3r.org")
23+
.setHost("jabb3r.org")
24+
.build();
25+
26+
AbstractXMPPConnection connection = new XMPPTCPConnection(config);
27+
connection.connect();
28+
connection.login();
29+
30+
ChatManager chatManager = ChatManager.getInstanceFor(connection);
31+
32+
Chat chat = chatManager.chatWith(JidCreate.from("baeldung@jabb3r.org").asEntityBareJidOrThrow());
33+
34+
chat.send("Hello!");
35+
36+
} catch (Exception e) {
37+
logger.error(e.getMessage(), e);
38+
}
39+
}
40+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.smack;
2+
3+
import org.jivesoftware.smack.AbstractXMPPConnection;
4+
import org.jivesoftware.smack.SmackException;
5+
import org.jivesoftware.smack.XMPPException;
6+
import org.jivesoftware.smack.chat2.ChatManager;
7+
import org.jivesoftware.smack.filter.StanzaTypeFilter;
8+
import org.jivesoftware.smack.packet.Message;
9+
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
10+
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
11+
import org.junit.Assert;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
import org.jxmpp.stringprep.XmppStringprepException;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
18+
import java.io.IOException;
19+
import java.util.concurrent.CountDownLatch;
20+
21+
public class SmackIntegrationTest {
22+
23+
private static AbstractXMPPConnection connection;
24+
private Logger logger = LoggerFactory.getLogger(SmackIntegrationTest.class);
25+
26+
@BeforeClass
27+
public static void setup() throws IOException, InterruptedException, XMPPException, SmackException {
28+
29+
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
30+
.setUsernameAndPassword("baeldung","baeldung")
31+
.setXmppDomain("jabb3r.org")
32+
.setHost("jabb3r.org")
33+
.build();
34+
35+
XMPPTCPConnectionConfiguration config2 = XMPPTCPConnectionConfiguration.builder()
36+
.setUsernameAndPassword("baeldung2","baeldung2")
37+
.setXmppDomain("jabb3r.org")
38+
.setHost("jabb3r.org")
39+
.build();
40+
41+
connection = new XMPPTCPConnection(config);
42+
connection.connect();
43+
connection.login();
44+
45+
}
46+
47+
@Test
48+
public void whenSendMessageWithChat_thenReceiveMessage() throws XmppStringprepException, InterruptedException {
49+
50+
CountDownLatch latch = new CountDownLatch(1);
51+
ChatManager chatManager = ChatManager.getInstanceFor(connection);
52+
final String[] expected = {null};
53+
54+
new StanzaThread().run();
55+
56+
chatManager.addIncomingListener((entityBareJid, message, chat) -> {
57+
logger.info("Message arrived: " + message.getBody());
58+
expected[0] = message.getBody();
59+
latch.countDown();
60+
});
61+
62+
latch.await();
63+
Assert.assertEquals("Hello!", expected[0]);
64+
}
65+
66+
@Test
67+
public void whenSendMessage_thenReceiveMessageWithFilter() throws XmppStringprepException, InterruptedException {
68+
69+
CountDownLatch latch = new CountDownLatch(1);
70+
final String[] expected = {null};
71+
72+
new StanzaThread().run();
73+
74+
connection.addAsyncStanzaListener(stanza -> {
75+
if (stanza instanceof Message) {
76+
Message message = (Message) stanza;
77+
expected[0] = message.getBody();
78+
latch.countDown();
79+
}
80+
}, StanzaTypeFilter.MESSAGE);
81+
82+
latch.await();
83+
Assert.assertEquals("Hello!", expected[0]);
84+
}
85+
}

0 commit comments

Comments
 (0)