-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(callback): Support callback functions
- Loading branch information
Showing
5 changed files
with
139 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
api/src/main/java/cc/carm/plugin/mineredis/api/callback/RedisCallbackBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cc.carm.plugin.mineredis.api.callback; | ||
|
||
import cc.carm.plugin.mineredis.api.RedisManager; | ||
import cc.carm.plugin.mineredis.api.message.RedisMessage; | ||
import cc.carm.plugin.mineredis.api.message.RedisMessageListener; | ||
import com.google.common.io.ByteArrayDataOutput; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
public class RedisCallbackBuilder { | ||
|
||
protected final @NotNull RedisManager redis; | ||
protected final @NotNull String requestChannel; | ||
protected final @NotNull ByteArrayDataOutput requestData; | ||
|
||
protected Predicate<RedisMessage> filter; | ||
protected Duration timeoutDuration = Duration.ofSeconds(5); | ||
|
||
public RedisCallbackBuilder(@NotNull RedisManager redis, | ||
@NotNull String requestChannel, @NotNull ByteArrayDataOutput requestData) { | ||
this.redis = redis; | ||
this.requestChannel = requestChannel; | ||
this.requestData = requestData; | ||
} | ||
|
||
public <R> CompletableFuture<R> response(@NotNull String channel, | ||
@NotNull Function<RedisMessage, R> handler) { | ||
CompletableFuture<R> future = new CompletableFuture<>(); | ||
RedisMessageListener listener = message -> { | ||
if (filter != null && !filter.test(message)) return; | ||
future.complete(handler.apply(message)); | ||
}; | ||
redis.registerChannelListener(listener, channel); | ||
redis.publish(requestChannel, requestData); | ||
return future.whenComplete((r, e) -> redis.unregisterListener(listener)); | ||
} | ||
|
||
public RedisCallbackBuilder filter(@NotNull Predicate<RedisMessage> filter) { | ||
this.filter = filter; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import cc.carm.plugin.mineredis.MineRedis; | ||
|
||
import java.util.UUID; | ||
|
||
public class RedisCallback { | ||
|
||
public void demo() { | ||
UUID requestID = UUID.randomUUID(); | ||
MineRedis.request("test.request", out -> { | ||
out.writeUTF(requestID.toString()); | ||
out.writeUTF("test"); | ||
}) | ||
.filter(message -> message.getData().readUTF().equals(requestID.toString())) // 限制条件 | ||
.response("test.response", message -> { // | ||
System.out.println("response: " + message.getData().readUTF()); | ||
return message.getData().readUTF(); | ||
}) // 如有收到了符合条件的反馈,则读取结果 | ||
.thenAccept(System.out::println) // 使用结果 | ||
.exceptionally(throwable -> { | ||
throwable.printStackTrace(); | ||
return null; | ||
}); | ||
} | ||
} |