Skip to content

Commit 3c5f66e

Browse files
v1.1.3 - Publishing messages is now asynchronous
1 parent 7f46960 commit 3c5f66e

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
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.1.2</version>
9+
<version>1.1.3</version>
1010

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

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

+24-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import redis.clients.jedis.JedisPoolConfig;
1717
import redis.clients.jedis.JedisPubSub;
1818

19+
import java.util.concurrent.CompletableFuture;
20+
import java.util.concurrent.ExecutorService;
21+
import java.util.concurrent.Executors;
1922
import java.util.function.Consumer;
2023
import java.util.regex.Matcher;
2124
import java.util.regex.Pattern;
@@ -26,6 +29,7 @@
2629
public class RedisAPI {
2730
private static final String REDIS_FULL_URI_PATTERN = "rediss?:\\/\\/(?:(?<user>\\w+)?:(?<password>[\\w-]+)@)?(?<host>[\\w.-]+):(?<port>\\d+)";
2831
private static final String REDIS_URI_PATTERN = "rediss?:\\/\\/[\\w.-]+:\\d+";
32+
private final ExecutorService executorService = Executors.newCachedThreadPool();
2933

3034
@Getter
3135
private static RedisAPI instance = null;
@@ -163,31 +167,37 @@ public void setFilterID(String filterId) {
163167
}
164168

165169
/**
166-
* Publishes a message to the generated instances redis pool
170+
* Asynchronously publishes a message to the generated instances redis pool
167171
* @param channel the channel object being published to, this is what should be registered on your other instances
168172
* @param message the message being sent across that channel
173+
* @return CompletableFuture<Void> representing the asynchronous operation
169174
*/
170-
public void publishMessage(RedisChannel channel, String message) {
171-
try (Jedis jedis = pool.getResource()) {
172-
jedis.publish(channel.channelName, "none" + ";" + message);
173-
} catch (Exception ex) {
174-
throw new MessageFailureException("Failed to send message to redis", ex);
175-
}
175+
public CompletableFuture<Void> publishMessage(RedisChannel channel, String message) {
176+
return CompletableFuture.runAsync(() -> {
177+
try (Jedis jedis = pool.getResource()) {
178+
jedis.publish(channel.channelName, "none" + ";" + message);
179+
} catch (Exception ex) {
180+
throw new MessageFailureException("Failed to send message to redis", ex);
181+
}
182+
}, executorService);
176183
}
177184

178185
/**
179-
* Publishes a message to the generated instances redis pool
186+
* Asynchronously publishes a message to the generated instances redis pool
180187
* @param filterId the filter id for the message being sent, this filter id is checked by all the receiving pools
181188
* to ensure that only a specific jedis pool handles the message
182189
* @param channel the channel object being published to, this is what should be registered on your other instances
183190
* @param message the message being sent across that channel
191+
* @return CompletableFuture<Void> representing the asynchronous operation
184192
*/
185-
public void publishMessage(String filterId, RedisChannel channel, String message) {
186-
try (Jedis jedis = pool.getResource()) {
187-
jedis.publish(channel.channelName, filterId + ";" + message);
188-
} catch (Exception ex) {
189-
throw new MessageFailureException("Failed to send message to redis", ex);
190-
}
193+
public CompletableFuture<Void> publishMessage(String filterId, RedisChannel channel, String message) {
194+
return CompletableFuture.runAsync(() -> {
195+
try (Jedis jedis = pool.getResource()) {
196+
jedis.publish(channel.channelName, filterId + ";" + message);
197+
} catch (Exception ex) {
198+
throw new MessageFailureException("Failed to send message to redis", ex);
199+
}
200+
}, executorService);
191201
}
192202

193203
/**

0 commit comments

Comments
 (0)