org.springframework.data.redis.connection.ReactiveRedisConnection Java Examples

The following examples show how to use org.springframework.data.redis.connection.ReactiveRedisConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.NumericResponse<AcknowledgeCommand, Long>> xAck(Publisher<AcknowledgeCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getGroup(), "Group must not be null!");
        Assert.notNull(command.getRecordIds(), "recordIds must not be null!");

        List<Object> params = new ArrayList<>();
        byte[] k = toByteArray(command.getKey());
        params.add(k);
        params.add(command.getGroup());
        params.addAll(toStringList(command.getRecordIds()));

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XACK, params.toArray());
        return m.map(v -> new ReactiveRedisConnection.NumericResponse<>(command, v));
    });
}
 
Example #2
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();

    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    ss.unsubscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    
    
}
 
Example #3
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();

    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    ss.unsubscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
}
 
Example #4
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplate() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicLong counter = new AtomicLong();

    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    template.listenTo(ChannelTopic.of("test2")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    ReactiveRedisConnection connection = factory.getReactiveConnection();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> counter.get() == 1);
}
 
Example #5
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplate() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();

    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        msg.set(message.getMessage().getBytes());
        return template.delete("myobj");
    }).subscribe();

    ReactiveRedisConnection connection = factory.getReactiveConnection();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
}
 
Example #6
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();

    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    ss.unsubscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
}
 
Example #7
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();

    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    ss.unsubscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    
    
}
 
Example #8
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.NumericResponse<AcknowledgeCommand, Long>> xAck(Publisher<AcknowledgeCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getGroup(), "Group must not be null!");
        Assert.notNull(command.getRecordIds(), "recordIds must not be null!");

        List<Object> params = new ArrayList<>();
        byte[] k = toByteArray(command.getKey());
        params.add(k);
        params.add(command.getGroup());
        params.addAll(toStringList(command.getRecordIds()));

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XACK, params.toArray());
        return m.map(v -> new ReactiveRedisConnection.NumericResponse<>(command, v));
    });
}
 
Example #9
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<AddStreamRecord, RecordId>> xAdd(Publisher<AddStreamRecord> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getBody(), "Body must not be null!");

        byte[] k = toByteArray(command.getKey());
        List<Object> params = new LinkedList<>();
        params.add(k);

        if (!command.getRecord().getId().shouldBeAutoGenerated()) {
            params.add(command.getRecord().getId().getValue());
        } else {
            params.add("*");
        }

        for (Map.Entry<ByteBuffer, ByteBuffer> entry : command.getBody().entrySet()) {
            params.add(toByteArray(entry.getKey()));
            params.add(toByteArray(entry.getValue()));
        }

        Mono<StreamMessageId> m = write(k, StringCodec.INSTANCE, RedisCommands.XADD, params.toArray());
        return m.map(v -> new ReactiveRedisConnection.CommandResponse<>(command, RecordId.of(v.toString())));
    });
}
 
Example #10
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<DeleteCommand, Long>> xDel(Publisher<DeleteCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getRecordIds(), "recordIds must not be null!");

        byte[] k = toByteArray(command.getKey());
        List<Object> params = new ArrayList<>();
        params.add(k);
        params.addAll(toStringList(command.getRecordIds()));

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XDEL, params.toArray());
        return m.map(v -> new ReactiveRedisConnection.CommandResponse<>(command, v));
    });
}
 
Example #11
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<DeleteCommand, Long>> xDel(Publisher<DeleteCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getRecordIds(), "recordIds must not be null!");

        byte[] k = toByteArray(command.getKey());
        List<Object> params = new ArrayList<>();
        params.add(k);
        params.addAll(toStringList(command.getRecordIds()));

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XDEL, params.toArray());
        return m.map(v -> new ReactiveRedisConnection.CommandResponse<>(command, v));
    });
}
 
Example #12
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<XInfoCommand, Flux<StreamInfo.XInfoConsumer>>> xInfoConsumers(Publisher<XInfoCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getGroupName(), "Group name must not be null!");

        byte[] k = toByteArray(command.getKey());

        Mono<List<StreamConsumer>> m = write(k, StringCodec.INSTANCE, RedisCommands.XINFO_CONSUMERS, k, command.getGroupName());
        return m.map(v -> new ReactiveRedisConnection.CommandResponse<>(command, Flux.fromStream(v.stream()).map(r -> {
            Map<String, Object> res = new HashMap<>();
            res.put("name", r.getName());
            res.put("idle", r.getIdleTime());
            res.put("pending", (long) r.getPending());

            List<Object> list = res.entrySet().stream()
                                    .flatMap(e -> Stream.of(e.getKey(), e.getValue()))
                                    .collect(Collectors.toList());
            return new StreamInfo.XInfoConsumer(command.getGroupName(), list);
        })));
    });
}
 
Example #13
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<XInfoCommand, Flux<StreamInfo.XInfoGroup>>> xInfoGroups(Publisher<XInfoCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] k = toByteArray(command.getKey());

        Mono<List<StreamGroup>> m = write(k, StringCodec.INSTANCE, RedisCommands.XINFO_GROUPS, k);
        return m.map(v -> new ReactiveRedisConnection.CommandResponse<>(command, Flux.fromStream(v.stream()).map(r -> {
            Map<String, Object> res = new HashMap<>();
            res.put("name", r.getName());
            res.put("consumers", (long) r.getConsumers());
            res.put("pending", (long) r.getPending());
            res.put("last-delivered-id", r.getLastDeliveredId().toString());

            List<Object> list = res.entrySet().stream()
                                    .flatMap(e -> Stream.of(e.getKey(), e.getValue()))
                                    .collect(Collectors.toList());
            return StreamInfo.XInfoGroup.fromList(list);
        })));
    });
}
 
Example #14
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<PendingRecordsCommand, PendingMessagesSummary>> xPendingSummary(Publisher<PendingRecordsCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getGroupName(), "Group name must not be null!");

        byte[] k = toByteArray(command.getKey());

        Mono<PendingResult> m = write(k, StringCodec.INSTANCE, RedisCommands.XPENDING, k, command.getGroupName());
        return m.map(v -> {
            Range<String> range = Range.open(v.getLowestId().toString(), v.getHighestId().toString());
            PendingMessagesSummary s = new PendingMessagesSummary(command.getGroupName(), v.getTotal(), range, v.getConsumerNames());
            return new ReactiveRedisConnection.CommandResponse<>(command, s);
        });
    });
}
 
Example #15
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<XClaimCommand, Flux<RecordId>>> xClaimJustId(Publisher<XClaimCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getGroupName(), "Group name must not be null!");
        Assert.notNull(command.getNewOwner(), "NewOwner must not be null!");
        Assert.notEmpty(command.getOptions().getIds(), "Ids collection must not be empty!");

        List<Object> params = new ArrayList<>();
        byte[] k = toByteArray(command.getKey());
        params.add(k);
        params.add(command.getGroupName());
        params.add(command.getNewOwner());
        params.add(Objects.requireNonNull(command.getOptions().getIdleTime()).toMillis());
        params.addAll(Arrays.asList(command.getOptions().getIdsAsStringArray()));
        params.add("JUSTID");

        Mono<Map<StreamMessageId, Map<byte[], byte[]>>> m = write(k, ByteArrayCodec.INSTANCE, RedisCommands.XCLAIM, params.toArray());
        return m.map(v -> new ReactiveRedisConnection.CommandResponse<>(command, Flux.fromStream(v.entrySet().stream()).map(e -> {
            return RecordId.of(e.getKey().toString());
        })));
    });
}
 
Example #16
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplate() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicLong counter = new AtomicLong();

    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    template.listenTo(ChannelTopic.of("test2")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    ReactiveRedisConnection connection = factory.getReactiveConnection();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> counter.get() == 1);
}
 
Example #17
Source File: TracingRedisConnectionFactory.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Override
public ReactiveRedisConnection getReactiveConnection() {
  if (this.delegate instanceof ReactiveRedisConnectionFactory) {
    ReactiveRedisConnectionFactory connectionFactory = (ReactiveRedisConnectionFactory) this.delegate;
    ReactiveRedisConnection connection = connectionFactory.getReactiveConnection();
    // support cluster connection
    if (connection instanceof ReactiveRedisClusterConnection) {
      return new TracingReactiveRedisClusterConnection(
          (ReactiveRedisClusterConnection) connection, tracingConfiguration);
    }
    return new TracingReactiveRedisConnection(connectionFactory.getReactiveConnection(),
        tracingConfiguration);
  }
  // TODO: shouldn't we throw an exception?
  return null;
}
 
Example #18
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();

    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    ss.unsubscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
}
 
Example #19
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();

    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    ss.unsubscribe();
    
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    
    
}
 
Example #20
Source File: RoutesConfig.java    From spring-5-examples with MIT License 5 votes vote down vote up
@Bean
public RouterFunction<ServerResponse> routes(final TaskRepository taskRepository,
                                             final ActivityRepository activityRepository,
                                             final ReactiveRedisConnection connection) {

  val keyCommands = connection.keyCommands();

  return

      route(

          DELETE("/"),
          request -> ok().body(
              Mono.fromCallable(() -> {
                activityRepository.deleteAll();
                taskRepository.deleteAll();
                return "done.";
              }), String.class))

      .andRoute(

          GET("/tasks"),
          request -> ok().body(Flux.fromIterable(taskRepository.findAll()), Task.class))

      .andRoute(

          GET("/activities"),
          request -> ok().body(Flux.fromIterable(activityRepository.findAll()), Activity.class))

      .andRoute(

          GET("/**"),
          request -> ok().body(Mono.just(format("command type %s",
                                                keyCommands.randomKey()
                                                           .flatMap(keyCommands::type)
                                                           .map(DataType::code)
                                                           .subscribe())), String.class))
      ;
}
 
Example #21
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.NumericResponse<ReactiveRedisConnection.KeyCommand, Long>> xLen(Publisher<ReactiveRedisConnection.KeyCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] k = toByteArray(command.getKey());

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XLEN, k);
        return m.map(v -> new ReactiveRedisConnection.NumericResponse<>(command, v));
    });
}
 
Example #22
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.NumericResponse<ReactiveRedisConnection.KeyCommand, Long>> xTrim(Publisher<TrimCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getCount(), "Count must not be null!");

        byte[] k = toByteArray(command.getKey());

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XTRIM, k, "MAXLEN", command.getCount());
        return m.map(v -> new ReactiveRedisConnection.NumericResponse<>(command, v));
    });
}
 
Example #23
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.NumericResponse<ReactiveRedisConnection.KeyCommand, Long>> xTrim(Publisher<TrimCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getCount(), "Count must not be null!");

        byte[] k = toByteArray(command.getKey());

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XTRIM, k, "MAXLEN", command.getCount());
        return m.map(v -> new ReactiveRedisConnection.NumericResponse<>(command, v));
    });
}
 
Example #24
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.NumericResponse<ReactiveRedisConnection.KeyCommand, Long>> xLen(Publisher<ReactiveRedisConnection.KeyCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] k = toByteArray(command.getKey());

        Mono<Long> m = write(k, StringCodec.INSTANCE, RedisCommands.XLEN, k);
        return m.map(v -> new ReactiveRedisConnection.NumericResponse<>(command, v));
    });
}
 
Example #25
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<AddStreamRecord, RecordId>> xAdd(Publisher<AddStreamRecord> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getBody(), "Body must not be null!");

        byte[] k = toByteArray(command.getKey());
        List<Object> params = new LinkedList<>();
        params.add(k);

        if (command.getMaxlen() != null) {
            params.add("MAXLEN");
            params.add(command.getMaxlen());
        }

        if (!command.getRecord().getId().shouldBeAutoGenerated()) {
            params.add(command.getRecord().getId().getValue());
        } else {
            params.add("*");
        }

        for (Map.Entry<ByteBuffer, ByteBuffer> entry : command.getBody().entrySet()) {
            params.add(toByteArray(entry.getKey()));
            params.add(toByteArray(entry.getValue()));
        }

        Mono<StreamMessageId> m = write(k, StringCodec.INSTANCE, RedisCommands.XADD, params.toArray());
        return m.map(v -> new ReactiveRedisConnection.CommandResponse<>(command, RecordId.of(v.toString())));
    });
}
 
Example #26
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<XInfoCommand, StreamInfo.XInfoStream>> xInfo(Publisher<XInfoCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] k = toByteArray(command.getKey());

        RedisCommand<org.redisson.api.StreamInfo<Object, Object>> xinfoStreamCommand = new RedisCommand<>("XINFO", "STREAM",
            new ListMultiDecoder2(
                    new StreamInfoDecoder(),
                    new CodecDecoder(),
                    new ObjectMapDecoder(ByteArrayCodec.INSTANCE, false)));

        Mono<org.redisson.api.StreamInfo<byte[], byte[]>> m = write(k, StringCodec.INSTANCE, xinfoStreamCommand, k);
        return m.map(i -> {

            Map<String, Object> res = new HashMap<>();
            res.put("length", (long) i.getLength());
            res.put("first-entry", i.getFirstEntry().getData());
            res.put("last-entry", i.getLastEntry().getData());
            res.put("radix-tree-keys", i.getRadixTreeKeys());
            res.put("radix-tree-nodes", i.getRadixTreeNodes());
            res.put("groups", (long) i.getGroups());
            res.put("last-generated-id", i.getLastGeneratedId().toString());

            List<Object> list = res.entrySet().stream()
                                    .flatMap(e -> Stream.of(e.getKey(), e.getValue()))
                                    .collect(Collectors.toList());
            return new ReactiveRedisConnection.CommandResponse<>(command, StreamInfo.XInfoStream.fromList(list));
        });
    });
}
 
Example #27
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<PendingRecordsCommand, PendingMessages>> xPending(Publisher<PendingRecordsCommand> publisher) {
    return execute(publisher, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getGroupName(), "Group name must not be null!");

        byte[] k = toByteArray(command.getKey());

        List<Object> params = new ArrayList<>();
        params.add(k);

        params.add(((Range.Bound<String>)command.getRange().getLowerBound()).getValue().orElse("-"));
        params.add(((Range.Bound<String>)command.getRange().getUpperBound()).getValue().orElse("+"));

        if (command.getCount() != null) {
            params.add(command.getCount());
        }
        if (command.getConsumerName() != null) {
            params.add(command.getConsumerName());
        }

        Mono<List<PendingEntry>> m = write(k, StringCodec.INSTANCE, RedisCommands.XPENDING_ENTRIES, params.toArray());
        return m.map(list -> {
            List<PendingMessage> msgs = list.stream().map(v -> new PendingMessage(RecordId.of(v.getId().toString()),
                    Consumer.from(command.getGroupName(), v.getConsumerName()),
                    Duration.of(v.getIdleTime(), ChronoUnit.MILLIS),
                    v.getLastTimeDelivered())).collect(Collectors.toList());
            PendingMessages s = new PendingMessages(command.getGroupName(), command.getRange(), msgs);
            return new ReactiveRedisConnection.CommandResponse<>(command, s);
        });
    });
}
 
Example #28
Source File: RedissonConnectionFactory.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public ReactiveRedisConnection getReactiveConnection() {
    return new RedissonReactiveRedisConnection(new CommandReactiveService(((Redisson)redisson).getConnectionManager()));
}
 
Example #29
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<RangeCommand, Flux<ByteBufferRecord>>> xRevRange(Publisher<RangeCommand> publisher) {
    return range(RedisCommands.XREVRANGE, publisher);
}
 
Example #30
Source File: RedissonReactiveStreamCommands.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public Flux<ReactiveRedisConnection.CommandResponse<RangeCommand, Flux<ByteBufferRecord>>> xRange(Publisher<RangeCommand> publisher) {
    return range(RedisCommands.XRANGE, publisher);
}