-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathServerClientApp.java
66 lines (53 loc) · 2.17 KB
/
ServerClientApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.mpilone.yeti;
import java.util.concurrent.TimeUnit;
import org.mpilone.yeti.client.StompClient;
import org.mpilone.yeti.server.InMemoryBrokerStomplet;
import org.mpilone.yeti.server.StompServer;
/**
* An example Yeti STOMP server with multiple clients. The
* {@link InMemoryBrokerStomplet} is used to support simple message passing
* between connected clients.
*
* @author mpilone
*/
public class ServerClientApp {
public static void main(String[] args) throws InterruptedException {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
System.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
System.setProperty("org.slf4j.simpleLogger.log.com.hazelcast", "info");
System.setProperty("org.slf4j.simpleLogger.log.io.netty", "info");
int port = 8090;
StompServer server = new StompServer(StompFrameDecoder.DEFAULT_MAX_FRAME_SIZE, port,
new StompServer.ClassStompletFactory(InMemoryBrokerStomplet.class));
server.start();
try {
StompClient.QueuingFrameListener msgListener =
new StompClient.QueuingFrameListener();
StompClient client1 = new StompClient(true, "localhost", port);
client1.connect();
client1.subscribe(FrameBuilder.subscribe("foo.bar", "client1-1").build(),
msgListener);
StompClient client2 = new StompClient(true, "localhost", port);
client2.connect();
client2.send(FrameBuilder.send("foo.bar", "Hello").build());
client2.send(FrameBuilder.send("foo.poo", "Goodbye").build());
client2.send(FrameBuilder.send("foo.bar", "World!").build());
client2.disconnect();
try {
// Wait for the messages to arrive. We should get two of them.
Frame msg = msgListener.poll(2, TimeUnit.SECONDS);
System.out.println("Got message 1: " + msg.getBodyAsString());
msg = msgListener.poll(2, TimeUnit.SECONDS);
System.out.println("Got message 2: " + msg.getBodyAsString());
}
catch (InterruptedException ex) {
// ignore
}
client1.unsubscribe(FrameBuilder.unsubscribe("client1-1").build());
client1.disconnect();
}
finally {
server.stop();
}
}
}