use of org.redisson.misc.RedissonPromise in project redisson by redisson.
the class ConnectionWatchdog method reconnect.
private void reconnect(final RedisConnection connection, final Channel channel) {
if (connection.getReconnectListener() != null) {
// new connection used only for channel init
RedisConnection rc = new RedisConnection(connection.getRedisClient(), channel);
RPromise<RedisConnection> connectionFuture = new RedissonPromise<RedisConnection>();
connection.getReconnectListener().onReconnect(rc, connectionFuture);
connectionFuture.addListener(new FutureListener<RedisConnection>() {
@Override
public void operationComplete(Future<RedisConnection> future) throws Exception {
if (future.isSuccess()) {
refresh(connection, channel);
}
}
});
} else {
refresh(connection, channel);
}
}
use of org.redisson.misc.RedissonPromise in project redisson by redisson.
the class RedisClient method connectPubSubAsync.
public RFuture<RedisPubSubConnection> connectPubSubAsync() {
final RPromise<RedisPubSubConnection> f = new RedissonPromise<RedisPubSubConnection>();
ChannelFuture channelFuture = bootstrap.connect();
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
if (future.isSuccess()) {
final RedisPubSubConnection c = new RedisPubSubConnection(RedisClient.this, future.channel());
bootstrap.group().execute(new Runnable() {
public void run() {
if (!f.trySuccess(c)) {
c.closeAsync();
}
}
});
} else {
bootstrap.group().execute(new Runnable() {
public void run() {
f.tryFailure(future.cause());
}
});
}
}
});
return f;
}
use of org.redisson.misc.RedissonPromise in project redisson by redisson.
the class RedisConnection method async.
public <T, R> RFuture<R> async(long timeout, Codec encoder, RedisCommand<T> command, Object... params) {
final RPromise<R> promise = new RedissonPromise<R>();
if (timeout == -1) {
timeout = redisClient.getCommandTimeout();
}
if (redisClient.getBootstrap().group().isShuttingDown()) {
RedissonShutdownException cause = new RedissonShutdownException("Redisson is shutdown");
return RedissonPromise.newFailedFuture(cause);
}
final ScheduledFuture<?> scheduledFuture = redisClient.getBootstrap().group().next().schedule(new Runnable() {
@Override
public void run() {
RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout for " + redisClient.getAddr());
promise.tryFailure(ex);
}
}, timeout, TimeUnit.MILLISECONDS);
promise.addListener(new FutureListener<R>() {
@Override
public void operationComplete(Future<R> future) throws Exception {
scheduledFuture.cancel(false);
}
});
send(new CommandData<T, R>(promise, encoder, command, params));
return promise;
}
use of org.redisson.misc.RedissonPromise in project redisson by redisson.
the class RedisClient method connectAsync.
public RFuture<RedisConnection> connectAsync() {
final RPromise<RedisConnection> f = new RedissonPromise<RedisConnection>();
ChannelFuture channelFuture = bootstrap.connect();
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
if (future.isSuccess()) {
final RedisConnection c = new RedisConnection(RedisClient.this, future.channel());
bootstrap.group().execute(new Runnable() {
public void run() {
if (!f.trySuccess(c)) {
c.closeAsync();
}
}
});
} else {
bootstrap.group().execute(new Runnable() {
public void run() {
f.tryFailure(future.cause());
}
});
}
}
});
return f;
}
use of org.redisson.misc.RedissonPromise in project redisson by redisson.
the class RedisClientTest method testPipeline.
@Test
public void testPipeline() throws InterruptedException, ExecutionException {
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RedisConnection conn = c.connect();
conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
List<CommandData<?, ?>> commands = new ArrayList<CommandData<?, ?>>();
CommandData<String, String> cmd1 = conn.create(null, RedisCommands.PING);
commands.add(cmd1);
CommandData<Long, Long> cmd2 = conn.create(null, RedisCommands.INCR, "test");
commands.add(cmd2);
CommandData<Long, Long> cmd3 = conn.create(null, RedisCommands.INCR, "test");
commands.add(cmd3);
CommandData<String, String> cmd4 = conn.create(null, RedisCommands.PING);
commands.add(cmd4);
RPromise<Void> p = new RedissonPromise<Void>();
conn.send(new CommandsData(p, commands));
assertThat(cmd1.getPromise().get()).isEqualTo("PONG");
assertThat(cmd2.getPromise().get()).isEqualTo(1);
assertThat(cmd3.getPromise().get()).isEqualTo(2);
assertThat(cmd4.getPromise().get()).isEqualTo("PONG");
conn.sync(RedisCommands.FLUSHDB);
}
Aggregations