-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisMessagingExample.java
More file actions
85 lines (71 loc) · 2.48 KB
/
RedisMessagingExample.java
File metadata and controls
85 lines (71 loc) · 2.48 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import nl.hauntedmc.dataprovider.api.DataProviderAPI;
import nl.hauntedmc.dataprovider.database.DatabaseType;
import nl.hauntedmc.dataprovider.database.messaging.MessagingDataAccess;
import nl.hauntedmc.dataprovider.database.messaging.api.AbstractEventMessage;
import nl.hauntedmc.dataprovider.database.messaging.api.Subscription;
import java.util.Optional;
/**
* Example: Redis messaging publish/subscribe workflow.
*/
public final class RedisMessagingExample {
private MessagingDataAccess bus;
private Subscription subscription;
public void onEnable(DataProviderAPI api) {
Optional<MessagingDataAccess> optBus = api.registerDataAccess(
DatabaseType.REDIS_MESSAGING,
"default",
MessagingDataAccess.class
);
if (optBus.isEmpty()) {
return;
}
bus = optBus.get();
subscription = bus.subscribe("proxy.staffchat.message", StaffChatMessage.class, msg -> {
System.out.println("[" + msg.getServer() + "] " + msg.getSender() + ": " + msg.getMessage());
});
}
public void publishMessage(String sender, String server, String message) {
if (bus == null) {
return;
}
bus.publish("proxy.staffchat.message", new StaffChatMessage(sender, server, message));
}
public void onDisable(DataProviderAPI api) {
if (subscription != null) {
subscription.unsubscribe();
subscription = null;
}
if (bus != null) {
bus.shutdown();
bus = null;
}
api.unregisterDatabase(DatabaseType.REDIS_MESSAGING, "default");
}
public static final class StaffChatMessage extends AbstractEventMessage {
private final String sender;
private final String server;
private final String message;
@SuppressWarnings("unused")
private StaffChatMessage() {
super("staffchat");
this.sender = null;
this.server = null;
this.message = null;
}
public StaffChatMessage(String sender, String server, String message) {
super("staffchat");
this.sender = sender;
this.server = server;
this.message = message;
}
public String getSender() {
return sender;
}
public String getServer() {
return server;
}
public String getMessage() {
return message;
}
}
}