io.quarkus.redis.runtime.datasource.BlockingPubSubCommandsImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-redis-client Show documentation
Show all versions of quarkus-redis-client Show documentation
Connect to Redis in either imperative or reactive style
package io.quarkus.redis.runtime.datasource;
import static io.smallrye.mutiny.helpers.ParameterValidation.nonNull;
import java.time.Duration;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import io.quarkus.redis.datasource.RedisDataSource;
import io.quarkus.redis.datasource.pubsub.PubSubCommands;
import io.quarkus.redis.datasource.pubsub.ReactivePubSubCommands;
public class BlockingPubSubCommandsImpl extends AbstractRedisCommandGroup implements PubSubCommands {
private final ReactivePubSubCommands reactive;
public BlockingPubSubCommandsImpl(RedisDataSource ds, ReactivePubSubCommands reactive, Duration timeout) {
super(ds, timeout);
this.reactive = reactive;
}
@Override
public void publish(String channel, V message) {
reactive.publish(channel, message)
.await().atMost(timeout);
}
@Override
public RedisSubscriber subscribe(String channel, Consumer onMessage) {
return subscribe(channel, onMessage, null, null);
}
@Override
public RedisSubscriber subscribeToPattern(String pattern, Consumer onMessage) {
return subscribeToPattern(pattern, onMessage, null, null);
}
@Override
public RedisSubscriber subscribeToPatterns(List patterns, Consumer onMessage) {
return subscribeToPatterns(patterns, onMessage, null, null);
}
@Override
public RedisSubscriber subscribe(List channels, Consumer onMessage) {
return subscribe(channels, onMessage, null, null);
}
@Override
public RedisSubscriber subscribe(String channel, Consumer onMessage, Runnable onEnd,
Consumer onException) {
nonNull(channel, "channel");
return subscribe(List.of(channel), onMessage, onEnd, onException);
}
@Override
public RedisSubscriber subscribeToPattern(String pattern, Consumer onMessage, Runnable onEnd,
Consumer onException) {
nonNull(pattern, "pattern");
return subscribeToPatterns(List.of(pattern), onMessage, onEnd, onException);
}
@Override
public RedisSubscriber subscribeToPatterns(List patterns, Consumer onMessage, Runnable onEnd,
Consumer onException) {
return reactive.subscribeToPatterns(patterns, onMessage, onEnd, onException)
.map(rs -> new BlockingRedisSubscriber(rs))
.await().atMost(timeout);
}
@Override
public RedisSubscriber subscribe(List channels, Consumer onMessage, Runnable onEnd,
Consumer onException) {
return reactive.subscribe(channels, onMessage, onEnd, onException)
.map(r -> new BlockingRedisSubscriber(r))
.await().atMost(timeout);
}
@Override
public RedisSubscriber subscribeToPattern(String pattern, BiConsumer onMessage) {
return reactive.subscribeToPattern(pattern, onMessage)
.map(r -> new BlockingRedisSubscriber(r))
.await().atMost(timeout);
}
@Override
public RedisSubscriber subscribeToPatterns(List patterns, BiConsumer onMessage) {
return reactive.subscribeToPatterns(patterns, onMessage)
.map(r -> new BlockingRedisSubscriber(r))
.await().atMost(timeout);
}
@Override
public RedisSubscriber subscribeToPattern(String pattern, BiConsumer onMessage, Runnable onEnd,
Consumer onException) {
return reactive.subscribeToPattern(pattern, onMessage, onEnd, onException)
.map(r -> new BlockingRedisSubscriber(r))
.await().atMost(timeout);
}
@Override
public RedisSubscriber subscribeToPatterns(List patterns, BiConsumer onMessage, Runnable onEnd,
Consumer onException) {
return reactive.subscribeToPatterns(patterns, onMessage, onEnd, onException)
.map(r -> new BlockingRedisSubscriber(r))
.await().atMost(timeout);
}
@Override
public RedisSubscriber subscribe(List channels, BiConsumer onMessage, Runnable onEnd,
Consumer onException) {
return reactive.subscribe(channels, onMessage, onEnd, onException)
.map(r -> new BlockingRedisSubscriber(r))
.await().atMost(timeout);
}
private class BlockingRedisSubscriber implements RedisSubscriber {
private final ReactivePubSubCommands.ReactiveRedisSubscriber reactiveRedisSubscriber;
public BlockingRedisSubscriber(ReactivePubSubCommands.ReactiveRedisSubscriber reactiveRedisSubscriber) {
this.reactiveRedisSubscriber = reactiveRedisSubscriber;
}
@Override
public void unsubscribe(String... channels) {
reactiveRedisSubscriber.unsubscribe(channels)
.await().atMost(timeout);
}
@Override
public void unsubscribe() {
reactiveRedisSubscriber.unsubscribe()
.await().atMost(timeout);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy