Files
spring-data-examples/redis/streams

Spring Data Redis - Streams Examples

The Redis Stream is a new data type introduced with Redis 5.0 modelling log data structure. Spring Data Redis supports Redis Streams via both the imperative and the reactive API.

Imperative API

Basic Usage

@Autowired 
RedisTemplate template;

StringRecord record = StreamRecords.string()
    .withStreamKey("my-stream");
RecordId id = template.streamOps().add(record);

List<...> records = template.streamOps().read(count(2), from(id));

ContinuousRead Read

@Autowired 
RedisConnectionFactory factory;

StreamListener<String, MapRecord<> listener = 
  (msg) -> {
    // ...
  };

StreamMessageListenerContainer container = StreamMessageListenerContainer.create(factory));

container.receive(StreamOffset.fromStart("my-stream"), listener);

Reactive API

Basic Usage

@Autowired 
ReactiveRedisTemplate template;

StringRecord record = StreamRecords.string()
    .withStreamKey("my-stream");
Mono<RecordId> id = template.streamOps().add(record);

Flux<...> records = template.streamOps().read(count(2), from(id));

ContinuousRead Read

@Autowired 
ReactiveRedisConnectionFactory factory;

StreamReceiver receiver = StreamReceiver.create(factory));

container.receive(StreamOffset.fromStart("my-stream"))
    .doOnNext((msg) -> {
    	// ...
    })
    .subscribe();