use of org.redisson.client.RedisClient in project redisson by redisson.
the class MasterSlaveConnectionManager method createClient.
@Override
public RedisClient createClient(NodeType type, String host, int port) {
RedisClient client = createClient(host, port, config.getConnectTimeout(), config.getRetryInterval() * config.getRetryAttempts());
clients.add(new RedisClientEntry(client, commandExecutor, type));
return client;
}
use of org.redisson.client.RedisClient in project redisson by redisson.
the class ReplicatedConnectionManager method connect.
private RFuture<RedisConnection> connect(BaseMasterSlaveServersConfig<?> cfg, final URL addr) {
RedisConnection connection = nodeConnections.get(addr);
if (connection != null) {
return newSucceededFuture(connection);
}
RedisClient client = createClient(addr.getHost(), addr.getPort(), cfg.getConnectTimeout(), cfg.getRetryInterval() * cfg.getRetryAttempts());
final RPromise<RedisConnection> result = newPromise();
RFuture<RedisConnection> future = client.connectAsync();
future.addListener(new FutureListener<RedisConnection>() {
@Override
public void operationComplete(Future<RedisConnection> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
RedisConnection connection = future.getNow();
RPromise<RedisConnection> promise = newPromise();
connectListener.onConnect(promise, connection, null, config);
promise.addListener(new FutureListener<RedisConnection>() {
@Override
public void operationComplete(Future<RedisConnection> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
RedisConnection connection = future.getNow();
if (connection.isActive()) {
nodeConnections.put(addr, connection);
result.trySuccess(connection);
} else {
connection.closeAsync();
result.tryFailure(new RedisException("Connection to " + connection.getRedisClient().getAddr() + " is not active!"));
}
}
});
}
});
return result;
}
use of org.redisson.client.RedisClient in project redisson by redisson.
the class RedisClientTest method test.
@Test
public void test() throws InterruptedException {
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerInstance().getRedisServerBindAddress(), RedisRunner.getDefaultRedisServerInstance().getRedisServerPort(), 1000000, 1000000);
final RedisConnection conn = c.connect();
conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
for (int i = 0; i < 100000; i++) {
pool.execute(() -> {
conn.async(StringCodec.INSTANCE, RedisCommands.INCR, "test");
});
}
pool.shutdown();
assertThat(pool.awaitTermination(1, TimeUnit.HOURS)).isTrue();
assertThat((Long) conn.sync(LongCodec.INSTANCE, RedisCommands.GET, "test")).isEqualTo(100000);
conn.sync(RedisCommands.FLUSHDB);
}
use of org.redisson.client.RedisClient 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);
}
use of org.redisson.client.RedisClient in project redisson by redisson.
the class RedisClientTest method testSubscribe.
@Test
public void testSubscribe() throws InterruptedException {
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RedisPubSubConnection pubSubConnection = c.connectPubSub();
final CountDownLatch latch = new CountDownLatch(2);
pubSubConnection.addListener(new RedisPubSubListener<Object>() {
@Override
public boolean onStatus(PubSubType type, String channel) {
assertThat(type).isEqualTo(PubSubType.SUBSCRIBE);
assertThat(Arrays.asList("test1", "test2").contains(channel)).isTrue();
latch.countDown();
return true;
}
@Override
public void onMessage(String channel, Object message) {
}
@Override
public void onPatternMessage(String pattern, String channel, Object message) {
}
});
pubSubConnection.subscribe(StringCodec.INSTANCE, "test1", "test2");
latch.await(10, TimeUnit.SECONDS);
}
Aggregations