本文整理汇总了Java中com.lambdaworks.redis.api.StatefulRedisConnection类的典型用法代码示例。如果您正苦于以下问题:Java StatefulRedisConnection类的具体用法?Java StatefulRedisConnection怎么用?Java StatefulRedisConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StatefulRedisConnection类属于com.lambdaworks.redis.api包,在下文中一共展示了StatefulRedisConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connect
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
/**
* Connects to the Redis server.
* @param configuration The configuration object.
* @throws Exception If an error occurs.
*/
public void connect(Configuration configuration) throws Exception {
if(sync != null) {
return;
}
RedisURI.Builder uri = RedisURI.Builder.redis(configuration.getRedisHost(), configuration.getRedisPort())
.withDatabase(configuration.getRedisIndex());
if(!configuration.getRedisAuth().isEmpty()) {
uri.withPassword(configuration.getRedisAuth());
}
RedisClient client = RedisClient.create(uri.build());
StatefulRedisConnection<String, String> connection = client.connect();
sync = connection.sync();
for(Category category : Category.values()) {
logger.info("Registered the category {} with the type {}.", category, category.getEntry().getType());
category.getEntry().setCategory(category);
}
}
示例2: insertionRemoval
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
@Test
public void insertionRemoval() throws NumberFormatException, InterruptedException, ExecutionException {
RedisClient redis = RedisClient.create("redis://127.0.0.1:6379");
StatefulRedisConnection<String, String> redisConn = redis.connect();
RedisAsyncCommands<String, String> redisCmd = redisConn.async();
long iterations = 1000;
for (long i = 0; i < iterations; i++) {
redisCmd.set(String.valueOf(i), String.valueOf(i + 1));
}
for (long i = 0; i < iterations; i++) {
long v = Long.valueOf(redisCmd.get(String.valueOf(i)).get());
assertEquals(i + 1, v);
}
for (long i = 0; i < iterations; i++) {
redisCmd.del(String.valueOf(i));
}
redisConn.close();
redis.shutdown();
}
示例3: ping
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
public void ping(final PingCallback callback) {
Consumer<StatefulRedisConnection> connectionConsumer = new Consumer<StatefulRedisConnection>() {
@Override
public void accept(StatefulRedisConnection statefulRedisConnection) {
final CompletableFuture<String> future = statefulRedisConnection.async().ping().toCompletableFuture();
future.whenCompleteAsync((pong, th) -> {
if(th != null){
callback.fail(th);
}else{
callback.pong(pong);
}
}, executors);
}
};
Consumer<Throwable> throwableConsumer = new Consumer<Throwable>() {
@Override
public void accept(Throwable e) {
callback.fail(e);
log.error("[ping]" + hostPort, e);
}
};
asyncExecute(connectionConsumer, throwableConsumer);
}
示例4: role
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
public void role(RollCallback callback) {
Consumer<StatefulRedisConnection> connectionConsumer = new Consumer<StatefulRedisConnection>() {
@Override
public void accept(StatefulRedisConnection statefulRedisConnection) {
final CompletableFuture<List<Object>> future = statefulRedisConnection.async().role().toCompletableFuture();
future.whenCompleteAsync((role, th) -> {
if (th != null) {
callback.fail(th);
} else {
callback.role((String) role.get(0));
}
}, executors);
}
};
asyncExecute(connectionConsumer, null);
}
示例5: serverInfo
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
public void serverInfo(Callbackable<String> callback) {
String serverInfoSection = "server";
Consumer<StatefulRedisConnection> connectionConsumer = (connection) -> {
CompletableFuture<String> future = connection.async().info(serverInfoSection).toCompletableFuture();
future.whenCompleteAsync((info, th) -> {
if(th != null){
log.error("[info]{}", hostPort, th);
callback.fail(th);
}else{
callback.success(info);
}
}, executors);
};
Consumer<Throwable> throwableConsumer = (throwable) -> {
callback.fail(throwable);
log.error("[info]{}", hostPort, throwable);
};
asyncExecute(connectionConsumer, throwableConsumer);
}
示例6: conf
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
public void conf(String confSection, Callbackable<List<String>> callback) {
Consumer<StatefulRedisConnection> connectionConsumer = (connection) -> {
CompletableFuture<List<String>> future = connection.async().configGet(confSection).toCompletableFuture();
future.whenCompleteAsync((conf, throwable) -> {
if(throwable != null) {
log.error("[conf]Executing conf command error", throwable);
callback.fail(throwable);
} else {
callback.success(conf);
}
});
};
Consumer<Throwable> throwableConsumer = (throwable) -> {
callback.fail(throwable);
log.error("[conf]{}", hostPort, throwable);
};
asyncExecute(connectionConsumer, throwableConsumer);
}
示例7: publish
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
@SuppressWarnings("unused")
private void publish() {
scheduled.scheduleAtFixedRate(new AbstractExceptionLogTask() {
StatefulRedisConnection<String, String> redisConnection;
private RedisClient redisClient;
{
redisClient = RedisClient.create(clientResources, redisURI);
}
@Override
public void doRun() {
if (redisConnection == null) {
redisConnection = redisClient.connect();
}
logger.info("[run][publish]{}", channel);
redisConnection.async().publish(channel, randomString(10));
}
}, 0, 5, TimeUnit.SECONDS);
}
示例8: getConnection
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
protected StatefulRedisConnection<String, String> getConnection()
{
if (connection == null) {
synchronized (syncObject)
{
if (connection == null)
{
Logging.write("$");
RedisClient client = getRedisClient();
connection = client.connect();
TrySetClientName();
}
}
}
return connection;
}
示例9: handleInvocation
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
try {
Method targetMethod = methodCache.get(method);
Object result = targetMethod.invoke(asyncApi, args);
if (result instanceof RedisFuture) {
RedisFuture<?> command = (RedisFuture<?>) result;
if (!method.getName().equals("exec") && !method.getName().equals("multi")) {
if (connection instanceof StatefulRedisConnection && ((StatefulRedisConnection) connection).isMulti()) {
return null;
}
}
LettuceFutures.awaitOrCancel(command, connection.getTimeout(), connection.getTimeoutUnit());
return command.get();
}
return result;
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
示例10: regularClientFailsOnFirstCommandWithDelay
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
@Test
public void regularClientFailsOnFirstCommandWithDelay() {
try (StatefulRedisConnection<String, String> connect = client.connect()) {
Wait.untilEquals(false, connect::isOpen).waitOrTimeout();
connect.sync().ping();
} catch (RedisException e) {
if (e.getCause() instanceof IOException) {
assertThat(e).hasCauseInstanceOf(IOException.class);
} else {
assertThat(e.getCause()).hasMessageContaining("DENIED");
}
}
}
示例11: connectStandaloneAsync
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
private <K, V> ConnectionFuture<StatefulRedisConnection<K, V>> connectStandaloneAsync(RedisCodec<K, V> codec,
RedisURI redisURI, Timeout timeout) {
assertNotNull(codec);
checkValidRedisURI(redisURI);
logger.debug("Trying to get a Redis connection for: " + redisURI);
CommandHandler<K, V> handler = new CommandHandler<>(clientOptions, clientResources);
StatefulRedisConnectionImpl<K, V> connection = newStatefulRedisConnection(handler, codec, timeout.timeout,
timeout.timeUnit);
ConnectionFuture<StatefulRedisConnection<K, V>> future = connectStatefulAsync(handler, connection, redisURI);
future.whenComplete((channelHandler, throwable) -> {
if (throwable != null) {
connection.close();
}
});
return future;
}
示例12: closeStaleConnections
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
/**
* Close stale connections.
*/
public void closeStaleConnections() {
logger.debug("closeStaleConnections() count before expiring: {}", getConnectionCount());
Set<ConnectionKey> stale = getStaleConnectionKeys();
for (ConnectionKey connectionKey : stale) {
StatefulRedisConnection<K, V> connection = connections.get(connectionKey);
if (connection != null) {
connections.remove(connectionKey);
connection.close();
}
}
logger.debug("closeStaleConnections() count after expiring: {}", getConnectionCount());
}
示例13: tryWithResourcesReturnsSoftRefConnectionToPool
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
@Test
public void tryWithResourcesReturnsSoftRefConnectionToPool() throws Exception {
SoftReferenceObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
.createSoftReferenceObjectPool(() -> client.connect());
StatefulRedisConnection<String, String> usedConnection = null;
try (StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
RedisCommands<String, String> sync = connection.sync();
sync.ping();
usedConnection = connection;
}
try {
usedConnection.isMulti();
fail("Missing RedisException");
} catch (RedisException e) {
assertThat(e).hasMessageContaining("deallocated");
}
pool.close();
}
示例14: call
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
@Override
public void call(Subscriber<? super T> subscriber) {
// Reuse the first command but then discard it.
RedisCommand<K, V, T> command = this.command;
if (command == null) {
command = commandSupplier.get();
}
if (command.getOutput() instanceof StreamingOutput<?>) {
StreamingOutput<T> streamingOutput = (StreamingOutput<T>) command.getOutput();
if (connection instanceof StatefulRedisConnection<?, ?> && ((StatefulRedisConnection) connection).isMulti()) {
streamingOutput.setSubscriber(new DelegatingWrapper<>(new ObservableSubscriberWrapper<>(subscriber),
streamingOutput.getSubscriber()));
} else {
streamingOutput.setSubscriber(new ObservableSubscriberWrapper<>(subscriber));
}
}
connection.dispatch(new ObservableCommand<>(command, subscriber, dissolve));
this.command = null;
}
示例15: shouldRetrieveTopology
import com.lambdaworks.redis.api.StatefulRedisConnection; //导入依赖的package包/类
@Test
public void shouldRetrieveTopology() {
MasterSlaveTopologyRefresh refresh = new MasterSlaveTopologyRefresh(connectionFactory, provider);
CompletableFuture<StatefulRedisConnection<String, String>> master = CompletableFuture.completedFuture(connection);
CompletableFuture<StatefulRedisConnection<String, String>> slave = CompletableFuture.completedFuture(connection);
when(connectionFactory.connectToNodeAsync(any(), any())).thenReturn((CompletableFuture) master,
(CompletableFuture) slave);
RedisURI redisURI = new RedisURI();
redisURI.setTimeout(1);
redisURI.setUnit(TimeUnit.MILLISECONDS);
List<RedisNodeDescription> nodes = refresh.getNodes(redisURI);
assertThat(nodes).hasSize(2);
}