Skip to content

Commit

Permalink
Use AbstractRedisConfiguration name field for name qualifier (#177)
Browse files Browse the repository at this point in the history
Close: #172
Close: #169

Co-authored-by: Mariano Kfuri <[email protected]>
  • Loading branch information
sdelamo and mkfuri authored Mar 23, 2021
1 parent 8f1fba5 commit 6ebbd32
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
package io.micronaut.configuration.lettuce;

import io.lettuce.core.RedisURI;
import io.lettuce.core.resource.ClientResources;
import io.micronaut.context.env.Environment;

import io.micronaut.core.naming.Named;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -29,18 +28,19 @@
/**
* Abstract configuration for Lettuce.
*/
public abstract class AbstractRedisConfiguration extends RedisURI {
public abstract class AbstractRedisConfiguration extends RedisURI implements Named {

private RedisURI uri;
private List<RedisURI> uris = Collections.emptyList();
private Integer ioThreadPoolSize;
private Integer computationThreadPoolSize;

private String name;

/**
* Constructor.
*/
protected AbstractRedisConfiguration() {
setClientName(Environment.DEFAULT_NAME);
setName(Environment.DEFAULT_NAME);
setPort(RedisURI.DEFAULT_REDIS_PORT);
setHost("localhost"); // localhost by default
}
Expand Down Expand Up @@ -126,4 +126,21 @@ public Integer getComputationThreadPoolSize() {
public void setComputationThreadPoolSize(Integer computationThreadPoolSize) {
this.computationThreadPoolSize = computationThreadPoolSize;
}

/**
* @return Get the name of the bean.
*/
@Override
public String getName() {
return name;
}

/**
* Sets the name of the bean.
*
* @param name The name of the bean
*/
public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public StatefulRedisPubSubConnection<String, String> redisPubSubConnection(Named
* @return named The ClientResources
*/
private @Nullable ClientResources getClientResources(NamedRedisServersConfiguration config) {
return beanLocator.findBean(ClientResources.class, Qualifiers.byName(config.getClientName())).orElse(this.defaultClientResources);
return beanLocator.findBean(ClientResources.class, Qualifiers.byName(config.getName())).orElse(this.defaultClientResources);
}

/**
Expand All @@ -100,7 +100,7 @@ public StatefulRedisPubSubConnection<String, String> redisPubSubConnection(Named
* @return named The RedisClient
*/
private RedisClient getRedisClient(NamedRedisServersConfiguration config) {
return beanLocator.getBean(RedisClient.class, Qualifiers.byName(config.getClientName()));
return beanLocator.getBean(RedisClient.class, Qualifiers.byName(config.getName()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public class NamedRedisServersConfiguration extends AbstractRedisConfiguration {
* @param name name from configuration
*/
public NamedRedisServersConfiguration(@Parameter String name) {
setClientName(name);
setName(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.configuration.lettuce

import io.lettuce.core.RedisClient
import io.lettuce.core.RedisURI
import io.lettuce.core.api.StatefulRedisConnection
import io.lettuce.core.api.sync.RedisCommands
import io.micronaut.context.ApplicationContext
Expand Down Expand Up @@ -57,14 +58,14 @@ class RedisClientFactorySpec extends Specification{

void "test redis server config by URI"() {
given:
def port = SocketUtils.findAvailableTcpPort()
int port = SocketUtils.findAvailableTcpPort()
RedisServer redisServer = RedisServer.builder().port(port).setting(MAX_HEAP_SETTING).build()
redisServer.start()

when:
ApplicationContext applicationContext = ApplicationContext.run('redis.uri':"redis://localhost:$port")
StatefulRedisConnection client = applicationContext.getBean(StatefulRedisConnection)
def command = client.sync()
RedisCommands<?,?> command = client.sync()
then:
command.set("foo", "bar")
command.get("foo") == "bar"
Expand All @@ -76,26 +77,32 @@ class RedisClientFactorySpec extends Specification{

void "test multi redis server config by URI"() {
given:
def port = SocketUtils.findAvailableTcpPort()
int port = SocketUtils.findAvailableTcpPort()
RedisServer redisServer = RedisServer.builder().port(port).setting(MAX_HEAP_SETTING).build()
redisServer.start()

ApplicationContext applicationContext = ApplicationContext.run(['redis.servers.foo.uri':"redis://localhost:$port",'redis.servers.bar.uri':"redis://localhost:$port"])
ApplicationContext applicationContext = ApplicationContext.run(['redis.servers.foo.uri':"redis://localhost:$port",
'redis.servers.foo.client-name':"foo-client-name",
'redis.servers.bar.uri':"redis://localhost:$port"])
when:
RedisClient clientFoo = applicationContext.getBean(RedisClient, Qualifiers.byName("foo"))
def innerRedisURI = clientFoo.@redisURI
def commandFoo = clientFoo.connect().sync()
RedisURI innerRedisURI = clientFoo.@redisURI
RedisCommands<?,?> commandFoo = clientFoo.connect().sync()

then:
innerRedisURI.port == port
innerRedisURI.clientName == "foo-client-name"
commandFoo.info().contains("tcp_port:$port")
commandFoo.set("foo", "bar")
commandFoo.get("foo") == "bar"

when:
StatefulRedisConnection clientBar = applicationContext.getBean(StatefulRedisConnection, Qualifiers.byName("bar"))
def commandBar = clientBar.sync()
RedisClient clientBar = applicationContext.getBean(RedisClient, Qualifiers.byName("bar"))
RedisURI innerBarRedisURI = clientBar.@redisURI
RedisCommands<?,?> commandBar = clientBar.connect().sync()
then:
commandBar.info().contains("tcp_port:$port")
!innerBarRedisURI.clientName

cleanup:
redisServer.stop()
Expand Down Expand Up @@ -124,4 +131,26 @@ class RedisClientFactorySpec extends Specification{
redisServer.stop()
applicationContext.stop()
}

void "test redis client name settings"() {
given:
int port = SocketUtils.findAvailableTcpPort()
RedisServer redisServer = RedisServer.builder().port(port).setting(MAX_HEAP_SETTING).build()
redisServer.start()

ApplicationContext applicationContext = ApplicationContext.run([
'redis.uri':"redis://localhost:$port",
'redis.client-name':"test-name"
])
when:
RedisClient client = applicationContext.getBean(RedisClient)
RedisURI innerRedisURI = client.@redisURI

then:
innerRedisURI.clientName == "test-name"

cleanup:
redisServer.stop()
applicationContext.stop()
}
}

0 comments on commit 6ebbd32

Please sign in to comment.