Search in sources :

Example 1 with BatchException

use of io.lettuce.core.dynamic.batch.BatchException in project lettuce-core by lettuce-io.

the class BatchExecutableCommand method synchronize.

protected static Object synchronize(BatchTasks batchTasks, StatefulConnection<Object, Object> connection) {
    if (batchTasks == BatchTasks.EMPTY) {
        return null;
    }
    Duration timeout = connection.getTimeout();
    BatchException exception = null;
    List<RedisCommand<?, ?, ?>> failures = null;
    for (RedisCommand<?, ?, ?> batchTask : batchTasks) {
        try {
            Futures.await(timeout, (RedisFuture) batchTask);
        } catch (Exception e) {
            if (exception == null) {
                failures = new ArrayList<>();
                exception = new BatchException(failures);
            }
            failures.add(batchTask);
            exception.addSuppressed(e);
        }
    }
    if (exception != null) {
        throw exception;
    }
    return null;
}
Also used : RedisCommand(io.lettuce.core.protocol.RedisCommand) ArrayList(java.util.ArrayList) Duration(java.time.Duration) BatchException(io.lettuce.core.dynamic.batch.BatchException) ExecutionException(java.util.concurrent.ExecutionException) BatchException(io.lettuce.core.dynamic.batch.BatchException)

Example 2 with BatchException

use of io.lettuce.core.dynamic.batch.BatchException in project lettuce-core by lettuce-io.

the class RedisCommandsBatchingIntegrationTests method shouldHandleSynchronousBatchErrors.

@Test
void shouldHandleSynchronousBatchErrors() {
    RedisCommandFactory factory = new RedisCommandFactory(redis.getStatefulConnection());
    Batching api = factory.getCommands(Batching.class);
    api.set("k1", value);
    api.set("k2", value);
    api.llen("k2");
    api.set("k4", value);
    assertThat(redis.get("k1")).isNull();
    try {
        api.llen("k4");
        fail("Missing BatchException");
    } catch (BatchException e) {
        assertThat(redis.get("k1")).isEqualTo(value);
        assertThat(redis.get("k4")).isEqualTo(value);
        assertThat(e).isInstanceOf(BatchException.class);
        assertThat(e.getSuppressed()).hasSize(2);
        assertThat(e.getFailedCommands()).hasSize(2);
    }
}
Also used : CommandBatching(io.lettuce.core.dynamic.batch.CommandBatching) BatchException(io.lettuce.core.dynamic.batch.BatchException) Test(org.junit.jupiter.api.Test)

Aggregations

BatchException (io.lettuce.core.dynamic.batch.BatchException)2 CommandBatching (io.lettuce.core.dynamic.batch.CommandBatching)1 RedisCommand (io.lettuce.core.protocol.RedisCommand)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.jupiter.api.Test)1