55import org .springframework .context .annotation .Configuration ;
66import org .springframework .context .annotation .Profile ;
77import redis .embedded .RedisServer ;
8- import redis .embedded .RedisServerBuilder ;
98
109import java .io .IOException ;
1110import java .net .ServerSocket ;
@@ -24,26 +23,28 @@ public class EmbeddedRedisConfig {
2423 private int port ;
2524
2625 @ PostConstruct
27- public void startRedis () throws IOException {
28- port = findAvailablePort ();
29- redisServer = new RedisServerBuilder ()
30- .port (port )
31- .setting ("maxheap 256M" )
32- .build ();
33- redisServer .start ();
34-
35- // 동적으로 찾은 포트를 Spring 환경 변수에 반영
36- System .setProperty ("spring.data.redis.port" , String .valueOf (port ));
26+ public void startRedis () {
27+ try {
28+ this .port = findAvailablePort ();
29+ this .redisServer = new RedisServer (port );
30+ this .redisServer .start ();
31+ System .setProperty ("spring.data.redis.port" , String .valueOf (port ));
32+ } catch (IOException e ) {
33+ throw new IllegalStateException ("Failed to start embedded Redis" , e );
34+ }
3735 }
3836
3937 @ PreDestroy
4038 public void stopRedis () {
41- if (redisServer != null ) {
42- redisServer .stop ();
39+ try {
40+ if (redisServer != null && redisServer .isActive ()) {
41+ redisServer .stop ();
42+ }
43+ } catch (IOException e ) {
44+ throw new IllegalStateException ("Failed to stop embedded Redis" , e );
4345 }
4446 }
4547
46- // 사용 가능한 포트를 찾는 유틸리티 메서드
4748 private int findAvailablePort () {
4849 try (ServerSocket socket = new ServerSocket (0 )) {
4950 socket .setReuseAddress (true );
0 commit comments