Skip to content

Commit 95a7199

Browse files
v1.1.0 update, includes; more documentation, fixed bugs with channel registration
1 parent 4a6f21d commit 95a7199

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.swofty</groupId>
88
<artifactId>AtlasRedisAPI</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.1.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>1.8</maven.compiler.source>

src/main/java/net/swofty/redisapi/api/ChannelRegistry.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ public class ChannelRegistry {
2020
*/
2121
@NonNull
2222
public static RedisChannel getFromName(String channelName) {
23-
return registeredChannels.stream().filter(channel -> Objects.equals(channel.channelName, channelName)).findFirst().orElseThrow(() -> new ChannelNotRegisteredException("There is no channel registered with the name '" + channelName + "'"));
23+
return new RedisChannel(channelName, (e) -> {});
24+
// return registeredChannels.stream().filter(channel -> Objects.equals(channel.channelName, channelName)).findFirst().orElseThrow(() -> new ChannelNotRegisteredException("There is no channel registered with the name '" + channelName + "'"));
2425
}
2526

2627
public static void registerChannel(RedisChannel channel) {
2728
if (registeredChannels.stream().anyMatch(channel2 -> channel2.channelName.equals(channel.channelName)))
2829
throw new ChannelAlreadyRegisteredException("A channel already exists with this name '" + channel.channelName + "'");
2930

3031
registeredChannels.add(channel);
31-
Utility.runAsync(() -> RedisAPI.getInstance().getPool().getResource().subscribe(EventRegistry.pubSub, channel.channelName));
32+
// RedisAPI.getInstance().getPool().getResource().subscribe(EventRegistry.pubSub, channel.channelName);
3233
}
3334
}

src/main/java/net/swofty/redisapi/api/RedisAPI.java

+32-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.Getter;
99
import lombok.Setter;
1010
import net.swofty.redisapi.exceptions.ChannelAlreadyRegisteredException;
11+
import net.swofty.redisapi.exceptions.MessageFailureException;
1112
import redis.clients.jedis.Jedis;
1213
import redis.clients.jedis.JedisPool;
1314
import redis.clients.jedis.JedisPoolConfig;
@@ -53,22 +54,30 @@ public static RedisAPI generateInstance(String uri, String password) {
5354
throw new CouldNotConnectToRedisException("Either invalid Redis URI passed through; '" + uri + "' OR invalid Redis Password passed through; '" + password + "'");
5455
}
5556

56-
Jedis jedis = api.getPool().getResource();
57-
jedis.connect();
58-
59-
EventRegistry.pubSub = new JedisPubSub() {
60-
@Override
61-
public void onMessage(String channel, String message) {
62-
System.out.println("Received " + message);
63-
EventRegistry.handleAll(channel, message);
64-
super.onMessage(channel, message);
65-
}
66-
};
67-
6857
instance = api;
6958
return api;
7059
}
7160

61+
/**
62+
* Starts listeners for the Redis Pub/Sub channels
63+
*/
64+
public void startListeners() {
65+
new Thread(() -> {
66+
try (Jedis jedis = getPool().getResource()) {
67+
EventRegistry.pubSub = new JedisPubSub() {
68+
@Override
69+
public void onMessage(String channel, String message) {
70+
EventRegistry.handleAll(channel, message);
71+
}
72+
};
73+
jedis.subscribe(EventRegistry.pubSub, ChannelRegistry.registeredChannels.stream().map((e) -> e.channelName).toArray(String[]::new));
74+
getPool().returnResource(jedis);
75+
} catch (Exception e) {
76+
e.printStackTrace();
77+
}
78+
}).start();
79+
}
80+
7281
/**
7382
* Creates a new main Redis pool instance, there will only ever be one at a time so #getInstance should be used after generation
7483
* @param uri the URI used to connect to the Redis server running
@@ -94,9 +103,11 @@ public void setFilterID(String filterId) {
94103
* @param message the message being sent across that channel
95104
*/
96105
public void publishMessage(RedisChannel channel, String message) {
97-
Jedis jedis = pool.getResource();
98-
jedis.connect();
99-
jedis.publish(channel.channelName, "all" + ";" + message);
106+
try (Jedis jedis = pool.getResource()) {
107+
jedis.publish(channel.channelName, "none" + ";" + message);
108+
} catch (Exception ex) {
109+
throw new MessageFailureException("Failed to send message to redis", ex);
110+
}
100111
}
101112

102113
/**
@@ -107,9 +118,11 @@ public void publishMessage(RedisChannel channel, String message) {
107118
* @param message the message being sent across that channel
108119
*/
109120
public void publishMessage(String filterId, RedisChannel channel, String message) {
110-
Jedis jedis = pool.getResource();
111-
jedis.connect();
112-
jedis.publish(channel.channelName, filterId + ";" + message);
121+
try (Jedis jedis = pool.getResource()) {
122+
jedis.publish(channel.channelName, filterId + ";" + message);
123+
} catch (Exception ex) {
124+
throw new MessageFailureException("Failed to send message to redis", ex);
125+
}
113126
}
114127

115128
/**
@@ -139,4 +152,4 @@ public RedisChannel registerChannel(String channelName, @NonNull Consumer<RedisM
139152
ChannelRegistry.registerChannel(channel);
140153
return channel;
141154
}
142-
}
155+
}

src/main/java/net/swofty/redisapi/api/RedisChannel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ public RedisChannel(String channelName, Class<? extends RedisMessagingReceiveInt
5151
public java.lang.Long getTimestamp() {
5252
return timestamp;
5353
}
54-
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.swofty.redisapi.exceptions;
2+
3+
public class MessageFailureException extends RuntimeException
4+
{
5+
public MessageFailureException(String errorMessage, Throwable t) {
6+
super(errorMessage);
7+
this.setStackTrace(t.getStackTrace());
8+
}
9+
}

0 commit comments

Comments
 (0)