Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/org/github/siahsang/redutils/RedUtilsLockImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.github.siahsang.redutils;

import java.net.URI;
import org.github.siahsang.redutils.common.OperationCallBack;
import org.github.siahsang.redutils.common.RedUtilsConfig;
import org.github.siahsang.redutils.common.ThreadManager;
Expand Down Expand Up @@ -47,6 +48,17 @@ public RedUtilsLockImpl(final String hostAddress, final int port) {
this(hostAddress, port, 0);
}

public RedUtilsLockImpl(String uri) {
this(URI.create(uri));
}

public RedUtilsLockImpl(URI uri) {
this(new RedUtilsConfig
.RedUtilsConfigBuilder()
.uri(uri)
.build());
}

/**
* Use with master-replica configuration
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.github.siahsang.redutils.common;

import java.net.URI;
import java.net.URISyntaxException;

/**
* @author Javad Alimohammadi
*/
Expand All @@ -9,6 +12,8 @@ public class RedUtilsConfig {

public static final int DEFAULT_PORT = 6379;

private static final String REDIS_URI_PREFIX = "redis://";

private final int waitingTimeForReplicasMillis;

private final int retryCountForSyncingWithReplicas;
Expand All @@ -23,9 +28,7 @@ public class RedUtilsConfig {

private final int replicaCount;

private final String hostAddress;

public final int port;
private final URI uri;

private RedUtilsConfig(RedUtilsConfigBuilder redUtilsConfigBuilder) {
this.waitingTimeForReplicasMillis = redUtilsConfigBuilder.waitingTimeForReplicasMillis;
Expand All @@ -35,9 +38,7 @@ private RedUtilsConfig(RedUtilsConfigBuilder redUtilsConfigBuilder) {
this.lockMaxPoolSize = redUtilsConfigBuilder.maxPoolSize;
this.unlockedMessagePattern = redUtilsConfigBuilder.redUtilsUnLockedMessage;
this.replicaCount = redUtilsConfigBuilder.replicaCount;
this.hostAddress = redUtilsConfigBuilder.hostAddress;
this.port = redUtilsConfigBuilder.port;

this.uri = redUtilsConfigBuilder.parseUri();
}

public int getWaitingTimeForReplicasMillis() {
Expand Down Expand Up @@ -68,15 +69,10 @@ public int getReplicaCount() {
return replicaCount;
}

public String getHostAddress() {
return hostAddress;
public URI getUri() {
return uri;
}

public int getPort() {
return port;
}


public static final class RedUtilsConfigBuilder {
private int waitingTimeForReplicasMillis = 1000;

Expand All @@ -96,6 +92,8 @@ public static final class RedUtilsConfigBuilder {

private int port = DEFAULT_PORT;

private URI uri = null;

public RedUtilsConfig build() {
return new RedUtilsConfig(this);
}
Expand Down Expand Up @@ -144,5 +142,19 @@ public RedUtilsConfigBuilder port(int port) {
this.port = port;
return this;
}

public RedUtilsConfigBuilder uri(URI uri) {
this.uri = uri;
return this;
}

public RedUtilsConfigBuilder uri(String uri) {
this.uri = URI.create(uri);
return this;
}

private URI parseUri() {
return uri != null ? uri : URI.create(REDIS_URI_PREFIX + hostAddress + ":" + port);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ public class JedisConnectionManager implements ConnectionManager<Jedis> {

private final Map<String, List<Jedis>> reservedConnections = new ConcurrentHashMap<>();


private final JedisPool channelConnectionPool;

public JedisConnectionManager(RedUtilsConfig redUtilsConfig) {
this.capacity.set(redUtilsConfig.getLockMaxPoolSize());

GenericObjectPoolConfig<Jedis> lockPoolConfig = ConnectionPoolFactory.makePool(capacity.get());
this.channelConnectionPool = new JedisPool(lockPoolConfig,
redUtilsConfig.getHostAddress(),
redUtilsConfig.getPort(),
redUtilsConfig.getUri(),
redUtilsConfig.getReadTimeOutMillis()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,32 @@ void test_acquire_WHEN_happy_path() throws Exception {

}

@Test
void test_acquire_with_URI_WHEN_happy_path() throws Exception {
//************************
// Given
//************************
RedUtilsLockImpl redUtilsLock = new RedUtilsLockImpl("redis://" + GENERAL_REDIS_ADDRESS.masterHostAddress + ":" + GENERAL_REDIS_ADDRESS.masterPort);
AtomicBoolean firstTryToGetLock = new AtomicBoolean(false);


//************************
// WHEN
//************************
redUtilsLock.acquire("lock1", () -> {
sleepSeconds(2);
firstTryToGetLock.set(true);
});


//************************
// THEN
//************************
Assertions.assertTrue(firstTryToGetLock.get());
Assertions.assertNull(getKey("lock1"));

}

@Test
void test_acquire_get_lock_multiple_time() throws Exception {
//************************
Expand Down