/** * Subscribe to one or more {@link ChannelTopic}s and receive a stream of {@link ChannelMessage}. Messages and channel * names are treated as {@link String}. The message stream subscribes lazily to the Redis channels and unsubscribes if * the {@link org.reactivestreams.Subscription} is {@link org.reactivestreams.Subscription#cancel() cancelled}. * * @param channelTopics the channels to subscribe. * @return the message stream. * @throws InvalidDataAccessApiUsageException if {@code patternTopics} is empty. * @see #receive(Iterable, SerializationPair, SerializationPair) */ public Flux<Message<String, String>> receive(ChannelTopic... channelTopics) { Assert.notNull(channelTopics, "ChannelTopics must not be null!"); Assert.noNullElements(channelTopics, "ChannelTopics must not contain null elements!"); return receive(Arrays.asList(channelTopics), stringSerializationPair, stringSerializationPair); }
/** * Subscribe to one or more {@link PatternTopic}s and receive a stream of {@link PatternMessage}. Messages, pattern, * and channel names are treated as {@link String}. The message stream subscribes lazily to the Redis channels and * unsubscribes if the {@link org.reactivestreams.Subscription} is {@link org.reactivestreams.Subscription#cancel() * cancelled}. * * @param patternTopics the channels to subscribe. * @return the message stream. * @throws InvalidDataAccessApiUsageException if {@code patternTopics} is empty. * @see #receive(Iterable, SerializationPair, SerializationPair) */ @SuppressWarnings("unchecked") public Flux<PatternMessage<String, String, String>> receive(PatternTopic... patternTopics) { Assert.notNull(patternTopics, "PatternTopic must not be null!"); Assert.noNullElements(patternTopics, "PatternTopic must not contain null elements!"); return receive(Arrays.asList(patternTopics), stringSerializationPair, stringSerializationPair) .map(m -> (PatternMessage<String, String, String>) m); }
@Override public Flux<? extends Message<String, V>> listenTo(Topic... topics) { ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(getConnectionFactory()); return container .receive(Arrays.asList(topics), getSerializationContext().getStringSerializationPair(), getSerializationContext().getValueSerializationPair()) // .doFinally((signalType) -> container.destroyLater().subscribeOn(Schedulers.elastic())); }
/** * Subscribe to one or more {@link ChannelTopic}s and receive a stream of {@link ChannelMessage}. Messages and channel * names are treated as {@link String}. The message stream subscribes lazily to the Redis channels and unsubscribes if * the {@link org.reactivestreams.Subscription} is {@link org.reactivestreams.Subscription#cancel() cancelled}. * * @param channelTopics the channels to subscribe. * @return the message stream. * @throws InvalidDataAccessApiUsageException if {@code patternTopics} is empty. * @see #receive(Iterable, SerializationPair, SerializationPair) */ public Flux<Message<String, String>> receive(ChannelTopic... channelTopics) { Assert.notNull(channelTopics, "ChannelTopics must not be null!"); Assert.noNullElements(channelTopics, "ChannelTopics must not contain null elements!"); return receive(Arrays.asList(channelTopics), stringSerializationPair, stringSerializationPair); }
/** * Subscribe to one or more {@link ChannelTopic}s and receive a stream of {@link ChannelMessage}. Messages and channel * names are treated as {@link String}. The message stream subscribes lazily to the Redis channels and unsubscribes if * the {@link org.reactivestreams.Subscription} is {@link org.reactivestreams.Subscription#cancel() cancelled}. * * @param channelTopics the channels to subscribe. * @return the message stream. * @throws InvalidDataAccessApiUsageException if {@code patternTopics} is empty. * @see #receive(Iterable, SerializationPair, SerializationPair) */ public Flux<Message<String, String>> receive(ChannelTopic... channelTopics) { Assert.notNull(channelTopics, "ChannelTopics must not be null!"); Assert.noNullElements(channelTopics, "ChannelTopics must not contain null elements!"); return receive(Arrays.asList(channelTopics), stringSerializationPair, stringSerializationPair); }
/** * Subscribe to one or more {@link PatternTopic}s and receive a stream of {@link PatternMessage}. Messages, pattern, * and channel names are treated as {@link String}. The message stream subscribes lazily to the Redis channels and * unsubscribes if the {@link org.reactivestreams.Subscription} is {@link org.reactivestreams.Subscription#cancel() * cancelled}. * * @param patternTopics the channels to subscribe. * @return the message stream. * @throws InvalidDataAccessApiUsageException if {@code patternTopics} is empty. * @see #receive(Iterable, SerializationPair, SerializationPair) */ @SuppressWarnings("unchecked") public Flux<PatternMessage<String, String, String>> receive(PatternTopic... patternTopics) { Assert.notNull(patternTopics, "PatternTopic must not be null!"); Assert.noNullElements(patternTopics, "PatternTopic must not contain null elements!"); return receive(Arrays.asList(patternTopics), stringSerializationPair, stringSerializationPair) .map(m -> (PatternMessage<String, String, String>) m); }
/** * Subscribe to one or more {@link PatternTopic}s and receive a stream of {@link PatternMessage}. Messages, pattern, * and channel names are treated as {@link String}. The message stream subscribes lazily to the Redis channels and * unsubscribes if the {@link org.reactivestreams.Subscription} is {@link org.reactivestreams.Subscription#cancel() * cancelled}. * * @param patternTopics the channels to subscribe. * @return the message stream. * @throws InvalidDataAccessApiUsageException if {@code patternTopics} is empty. * @see #receive(Iterable, SerializationPair, SerializationPair) */ @SuppressWarnings("unchecked") public Flux<PatternMessage<String, String, String>> receive(PatternTopic... patternTopics) { Assert.notNull(patternTopics, "PatternTopic must not be null!"); Assert.noNullElements(patternTopics, "PatternTopic must not contain null elements!"); return receive(Arrays.asList(patternTopics), stringSerializationPair, stringSerializationPair) .map(m -> (PatternMessage<String, String, String>) m); }
@Bean public ReactiveRedisMessageListenerContainer redisMessageListenerContainer(PostRepository posts, ReactiveRedisConnectionFactory connectionFactory) { ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(connectionFactory); ObjectMapper objectMapper = new ObjectMapper(); container.receive(ChannelTopic.of("posts")) .map(p->p.getMessage()) .map(m -> { try { Post post= objectMapper.readValue(m, Post.class); post.setId(UUID.randomUUID().toString()); return post; } catch (IOException e) { return null; } }) .switchIfEmpty(Mono.error(new IllegalArgumentException())) .flatMap(p-> posts.save(p)) .subscribe(c-> log.info(" count:" + c), null , () -> log.info("saving post.")); return container; }
@Override public Flux<? extends Message<String, V>> listenTo(Topic... topics) { ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(getConnectionFactory()); return container .receive(Arrays.asList(topics), getSerializationContext().getStringSerializationPair(), getSerializationContext().getValueSerializationPair()) // .doFinally((signalType) -> container.destroyLater().subscribeOn(Schedulers.elastic())); }
@Override public Flux<? extends Message<String, V>> listenTo(Topic... topics) { ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(getConnectionFactory()); return container .receive(Arrays.asList(topics), getSerializationContext().getStringSerializationPair(), getSerializationContext().getValueSerializationPair()) // .doFinally((signalType) -> container.destroyLater().subscribeOn(Schedulers.elastic())); }