-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] SQS Listener 종료를 위한 GracefulShutdown 도입 #95
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
umbba-notification/src/main/java/sopt/org/umbba/notification/config/GracefulShutdown.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,68 @@ | ||
package sopt.org.umbba.notification.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; | ||
import org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer; | ||
import org.springframework.context.SmartLifecycle; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Map; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class GracefulShutdown implements SmartLifecycle, BeanFactoryAware { | ||
private final Map<String, SimpleMessageListenerContainer> messageListenerContainers; | ||
|
||
private boolean isRunning; | ||
private DefaultSingletonBeanRegistry beanFactory; | ||
|
||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
this.beanFactory = (DefaultSingletonBeanRegistry) beanFactory; | ||
} | ||
|
||
@Override | ||
public boolean isAutoStartup() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void stop(Runnable callback) { | ||
stop(); | ||
callback.run(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
this.isRunning = true; | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
stopSqsListeners(); | ||
this.isRunning = false; | ||
log.info("[GracefulShutdown 완료]"); | ||
} | ||
|
||
private void stopSqsListeners() { | ||
this.messageListenerContainers.keySet().forEach(beanName -> beanFactory.destroySingleton(beanName)); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isRunning() { | ||
return this.isRunning; | ||
} | ||
|
||
@Override | ||
public int getPhase() { | ||
return Integer.MAX_VALUE; // 명시적으로 PHASE 설정 | ||
} | ||
|
||
} |