Skip to content

Commit ccf4a4b

Browse files
committed
Update Redis.java
1 parent 208d3b7 commit ccf4a4b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

  • src/main/java/io/github/intisy/utils/custom/external

src/main/java/io/github/intisy/utils/custom/external/Redis.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ default void onDataSet(String key, String value) {}
2424

2525
private final List<RedisDataListener> dataListeners = new CopyOnWriteArrayList<>();
2626
private static final boolean DEFAULT_USE_EMBEDDED = false;
27+
private static final boolean DEFAULT_ALLOW_PORT_SEARCH = false;
28+
private static final boolean DEFAULT_ALLOW_MOCK_FALLBACK = false;
2729
private static final int DEFAULT_REDIS_PORT = 6379;
2830
private static final String DEFAULT_REDIS_HOST = "localhost";
2931

3032
private final String host;
3133
private final int port;
3234
private final boolean useEmbedded;
35+
private final boolean allowPortSearch;
36+
private final boolean allowMockFallback;
3337
private JedisPool jedisPool;
3438
private SimpleLogger logger;
3539
private boolean connected = false;
@@ -58,9 +62,19 @@ public Redis(String host, int port) {
5862
}
5963

6064
public Redis(String host, int port, boolean useEmbedded) {
65+
this(host, port, useEmbedded, DEFAULT_ALLOW_PORT_SEARCH);
66+
}
67+
68+
public Redis(String host, int port, boolean useEmbedded, boolean allowPortSearch) {
69+
this(host, port, useEmbedded, allowPortSearch, DEFAULT_ALLOW_MOCK_FALLBACK);
70+
}
71+
72+
public Redis(String host, int port, boolean useEmbedded, boolean allowPortSearch, boolean allowMockFallback) {
6173
this.host = host;
6274
this.port = port;
6375
this.useEmbedded = useEmbedded;
76+
this.allowPortSearch = allowPortSearch;
77+
this.allowMockFallback = allowMockFallback;
6478
this.logger = new EmptyLogger();
6579
}
6680

@@ -94,6 +108,11 @@ public void connect() throws IOException {
94108
try {
95109
startEmbeddedServer();
96110
} catch (IOException e) {
111+
if (!allowMockFallback) {
112+
logger.error("Failed to start embedded Redis server and mock fallback is disabled.", e);
113+
throw e;
114+
}
115+
97116
logger.warn("Failed to start embedded Redis server. Falling back to mock implementation.", e);
98117
mockRedis = new MockRedis();
99118
mockRedis.startServer();
@@ -168,6 +187,11 @@ private void startEmbeddedServer() throws IOException {
168187
int serverPort = port;
169188

170189
if (!isPortAvailable(serverPort)) {
190+
if (!allowPortSearch) {
191+
logger.error("Port " + serverPort + " is not available for embedded Redis server and port search is disabled");
192+
throw new IOException("Specified port " + serverPort + " is not available and port search is disabled");
193+
}
194+
171195
logger.warn("Port " + serverPort + " is not available for embedded Redis server. Trying to find a free port...");
172196
serverPort = findFreePort();
173197
if (serverPort == -1) {

0 commit comments

Comments
 (0)