44import java .net .UnknownHostException ;
55import java .time .Duration ;
66import java .util .Map ;
7+ import java .util .UUID ;
78import java .util .concurrent .Executor ;
89
910import org .ezcode .codetest .infrastructure .event .listener .RedisJudgeQueueConsumer ;
@@ -52,7 +53,7 @@ public void initConsumerGroup() {
5253 connection .xGroupDelConsumer (
5354 "judge-queue" .getBytes (),
5455 "judge-group" ,
55- "consumer-1"
56+ getConsumerName (). replace ( "consumer-" , "" )
5657 );
5758 return null ;
5859 });
@@ -78,7 +79,7 @@ public void initConsumerGroup() {
7879 public StreamMessageListenerContainer <String , MapRecord <String , String , String >> streamMessageListenerContainer (
7980 RedisConnectionFactory factory ,
8081 RedisJudgeQueueConsumer consumer
81- ) throws UnknownHostException {
82+ ) {
8283 StreamMessageListenerContainer
8384 .StreamMessageListenerContainerOptions <String , MapRecord <String , String , String >> options =
8485 StreamMessageListenerContainer
@@ -92,12 +93,21 @@ public StreamMessageListenerContainer<String, MapRecord<String, String, String>>
9293 StreamMessageListenerContainer .create (factory , options );
9394
9495 container .receive (
95- Consumer .from ("judge-group" , "consumer-" + InetAddress . getLocalHost (). getHostName ()),
96+ Consumer .from ("judge-group" , getConsumerName ()),
9697 StreamOffset .create ("judge-queue" , ReadOffset .lastConsumed ()),
9798 consumer
9899 );
99100
100101 container .start ();
101102 return container ;
102103 }
104+
105+ private String getConsumerName () {
106+ try {
107+ return "consumer-" + InetAddress .getLocalHost ().getHostName ();
108+ } catch (UnknownHostException e ) {
109+ log .warn ("호스트명 확인 실패, UUID 사용: {}" , e .getMessage ());
110+ return "consumer-" + UUID .randomUUID ().toString ().substring (0 , 8 );
111+ }
112+ }
103113}
0 commit comments