Spring Integration은 메시지 기반 애플리케이션 개발을 적용하기 위해 사용하였습니다.
Spring Integration은 유기적으로 연결해서 효율적으로 통합하는 방법을
여러 패턴을 통해 사용할 수 있게 도와주는 프레임워크입니다.
Spring Integration에 대해서는 블로그에 설명한 글이 많기 때문에 자세한 설명은 생략하겠습니다.
Spring Integration Point🔥
아래 3가지만 기억하시면 주요 기능을 사용하기에 충분합니다.
- Message
- 전송할 데이터가 담긴 Wrapper Class
- Channer (Pipes Line)
- Message Wrapper Class가 발/수신 되는곳
- Endpoint (Filters) GateWay
- Message Wrapper Class의 출/도착지
- GateWay : 메세지 프레임워크에 종속되지 않은 코드가 됨
- Service Activator : 메시지를 받을 채널과 연결
- 채널 종속적인 문제를 해결
- 어댑터(gateway)만 갈아끼면 외부 API와 통신할 수 있다.

코드 적용
@MessagingGateway
@MessagingGateway(defaultRequestChannel = COMMAND_GATEWAY_CHANNEL)
public interface CommendGateway {
void request(Command command, @Header(value = MESSAGE_USER_ID) UserId userId);
void request(Command command, @Header(value = MESSAGE_USER_ID) UserId userId, @Header(value = MESSAGE_POST_ID) PostId postId);
}
@MessagingGateway는 들어오는 매개변수를 Spring Integration Message로 감싸 Integration Flow의 흐름대로 전송합니다.
Channel & IntegrationFlow
@Slf4j
@Configuration
@EnableIntegration
public class CommandConfig {
private final ThreadPool threadPool;
public CommandConfig(final ThreadPool threadPool) {
this.threadPool = threadPool;
}
@Bean(name = COMMAND_GATEWAY_CHANNEL)
public ExecutorChannel createChannel() {
return new ExecutorChannel(threadPool.getDomainEventTaskExecutor());
}
@Bean
public IntegrationFlow createCommandFlow() {
return IntegrationFlow
.from(COMMAND_GATEWAY_CHANNEL)
// endpoint가 2개 이상부터 router가 작동하는것을 확인!
.route((Command c) -> c.getClass().getSimpleName())
.get();
}
}
Channel은 비동기 처리를 하기 위해 ExceutorChannel을 사용하였습니다.
그리고 IntegrationFlow를 사용하여 Router 처리를 하였습니다.
Router처리는 자세히 살펴보겠습니다.
Flow & Router
@Bean
public IntegrationFlow createCommandFlow() {
return IntegrationFlow
.from(COMMAND_GATEWAY_CHANNEL)
// endpoint가 2개 이상부터 router가 작동하는것을 확인!
.route((Command c) -> c.getClass().getSimpleName())
.get();
}
특정 Gateway로부터 Command가 들어오면
router를 사용해 Command의 클래스 명인 EndPoint로 Command를 보냅니다.
@ServiceActivator : EndPoint
@Transactional
@CommandAop
@ServiceActivator(inputChannel = COMMAND_GATEWAY_POST_CREATE_CHANNEL)
public void postCreate(Message<PostCreate> message) {
~ 메세지 처리!
}
@ServiceActivator를 사용하여 EndPoint를 설정할 수 있습니다!
느낀점
Spring Integration을 적용하는 과정은 너무나 힘들었습니다.
기존에 인강이나 블로그에도 Spring Integration 강의는 없거나 기본 설명 정도만 블로그에 작성했기 때문입니다.
이번에 Spring Integration을 적용하면서 공식 문서를 들여다 보고
여러가지 시도를 통해 Gateway, Router, EndPoint 등 이벤트 주도 기반 개발의 개념을
이해하는데 많은 도움이 되었습니다!
참고
Spring Integration Reference Guide
'Project > 개인프로젝트' 카테고리의 다른 글
[Error] 멀티모듈 BeanCreationException (0) | 2023.10.20 |
---|---|
[Error] Multi Module 적용 에러 (0) | 2023.10.19 |
ThreadLocal적용하여 Thread 안에서의 값을 공유하기 (0) | 2023.10.07 |
[Spring Integration] Direct Channel과 Executor Channel 차이 (0) | 2023.10.07 |
TransactionSynchronizationManager를 사용해 Transaction 이후에 Event 등록하기 (0) | 2023.10.06 |
Spring Integration은 메시지 기반 애플리케이션 개발을 적용하기 위해 사용하였습니다.
Spring Integration은 유기적으로 연결해서 효율적으로 통합하는 방법을
여러 패턴을 통해 사용할 수 있게 도와주는 프레임워크입니다.
Spring Integration에 대해서는 블로그에 설명한 글이 많기 때문에 자세한 설명은 생략하겠습니다.
Spring Integration Point🔥
아래 3가지만 기억하시면 주요 기능을 사용하기에 충분합니다.
- Message
- 전송할 데이터가 담긴 Wrapper Class
- Channer (Pipes Line)
- Message Wrapper Class가 발/수신 되는곳
- Endpoint (Filters) GateWay
- Message Wrapper Class의 출/도착지
- GateWay : 메세지 프레임워크에 종속되지 않은 코드가 됨
- Service Activator : 메시지를 받을 채널과 연결
- 채널 종속적인 문제를 해결
- 어댑터(gateway)만 갈아끼면 외부 API와 통신할 수 있다.

코드 적용
@MessagingGateway
@MessagingGateway(defaultRequestChannel = COMMAND_GATEWAY_CHANNEL)
public interface CommendGateway {
void request(Command command, @Header(value = MESSAGE_USER_ID) UserId userId);
void request(Command command, @Header(value = MESSAGE_USER_ID) UserId userId, @Header(value = MESSAGE_POST_ID) PostId postId);
}
@MessagingGateway는 들어오는 매개변수를 Spring Integration Message로 감싸 Integration Flow의 흐름대로 전송합니다.
Channel & IntegrationFlow
@Slf4j
@Configuration
@EnableIntegration
public class CommandConfig {
private final ThreadPool threadPool;
public CommandConfig(final ThreadPool threadPool) {
this.threadPool = threadPool;
}
@Bean(name = COMMAND_GATEWAY_CHANNEL)
public ExecutorChannel createChannel() {
return new ExecutorChannel(threadPool.getDomainEventTaskExecutor());
}
@Bean
public IntegrationFlow createCommandFlow() {
return IntegrationFlow
.from(COMMAND_GATEWAY_CHANNEL)
// endpoint가 2개 이상부터 router가 작동하는것을 확인!
.route((Command c) -> c.getClass().getSimpleName())
.get();
}
}
Channel은 비동기 처리를 하기 위해 ExceutorChannel을 사용하였습니다.
그리고 IntegrationFlow를 사용하여 Router 처리를 하였습니다.
Router처리는 자세히 살펴보겠습니다.
Flow & Router
@Bean
public IntegrationFlow createCommandFlow() {
return IntegrationFlow
.from(COMMAND_GATEWAY_CHANNEL)
// endpoint가 2개 이상부터 router가 작동하는것을 확인!
.route((Command c) -> c.getClass().getSimpleName())
.get();
}
특정 Gateway로부터 Command가 들어오면
router를 사용해 Command의 클래스 명인 EndPoint로 Command를 보냅니다.
@ServiceActivator : EndPoint
@Transactional
@CommandAop
@ServiceActivator(inputChannel = COMMAND_GATEWAY_POST_CREATE_CHANNEL)
public void postCreate(Message<PostCreate> message) {
~ 메세지 처리!
}
@ServiceActivator를 사용하여 EndPoint를 설정할 수 있습니다!
느낀점
Spring Integration을 적용하는 과정은 너무나 힘들었습니다.
기존에 인강이나 블로그에도 Spring Integration 강의는 없거나 기본 설명 정도만 블로그에 작성했기 때문입니다.
이번에 Spring Integration을 적용하면서 공식 문서를 들여다 보고
여러가지 시도를 통해 Gateway, Router, EndPoint 등 이벤트 주도 기반 개발의 개념을
이해하는데 많은 도움이 되었습니다!
참고
Spring Integration Reference Guide
'Project > 개인프로젝트' 카테고리의 다른 글
[Error] 멀티모듈 BeanCreationException (0) | 2023.10.20 |
---|---|
[Error] Multi Module 적용 에러 (0) | 2023.10.19 |
ThreadLocal적용하여 Thread 안에서의 값을 공유하기 (0) | 2023.10.07 |
[Spring Integration] Direct Channel과 Executor Channel 차이 (0) | 2023.10.07 |
TransactionSynchronizationManager를 사용해 Transaction 이후에 Event 등록하기 (0) | 2023.10.06 |