-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from f-lab-edu/develop
Develop
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/mini/joymall/commons/config/EmbeddedRedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.mini.joymall.commons.config; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.util.StringUtils; | ||
import redis.embedded.RedisServer; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
@Profile("test") | ||
@Configuration | ||
public class EmbeddedRedisConfig { | ||
@Value("${spring.data.redis.port}") | ||
private int redisPort; | ||
|
||
private RedisServer redisServer; | ||
|
||
@PostConstruct | ||
public void redisServer() throws IOException { | ||
int port = isRedisRunning()? findAvailablePort() : redisPort; | ||
redisServer = new RedisServer(port); | ||
redisServer.start(); | ||
} | ||
|
||
@PreDestroy | ||
public void stopRedis() throws IOException { | ||
if (redisServer != null) { | ||
redisServer.stop(); | ||
} | ||
} | ||
|
||
private boolean isRedisRunning() throws IOException { | ||
return isRunning(executeGrepProcessCommand(redisPort)); | ||
} | ||
|
||
public int findAvailablePort() throws IOException { | ||
|
||
for (int port = 10000; port <= 65535; port++) { | ||
Process process = executeGrepProcessCommand(port); | ||
if (!isRunning(process)) { | ||
return port; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Not Found Available port: 10000 ~ 65535"); | ||
} | ||
|
||
private Process executeGrepProcessCommand(int port) throws IOException { | ||
String command = String.format("netstat -nat | grep LISTEN|grep %d", port); | ||
String[] shell = {"/bin/sh", "-c", command}; | ||
return Runtime.getRuntime().exec(shell); | ||
} | ||
|
||
private boolean isRunning(Process process) { | ||
String line; | ||
StringBuilder pidInfo = new StringBuilder(); | ||
|
||
try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) { | ||
|
||
while ((line = input.readLine()) != null) { | ||
pidInfo.append(line); | ||
} | ||
|
||
} catch (Exception e) { | ||
} | ||
|
||
return !StringUtils.hasText(pidInfo.toString()); | ||
} | ||
} |