Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring AMQP 정리 #4

Open
suna-ji opened this issue Jan 6, 2025 · 0 comments
Open

Spring AMQP 정리 #4

suna-ji opened this issue Jan 6, 2025 · 0 comments
Labels
문서화 문서화
Milestone

Comments

@suna-ji
Copy link
Member

suna-ji commented Jan 6, 2025

📝 Spring AMQP 정리

📚 주제:
Spring AMQP는 Spring 프레임워크의 핵심 개념을 AMQP 기반 메시징 솔루션에 적용한 프로젝트로,
템플릿을 통해 메시지 송수신을 단순화하고, 메시지 기반 POJO 지원을 제공합니다.

🎯 목표

  • Spring AMQP의 주요 개념 및 활용 방법 학습
  • AMQP 리소스 관리 방식 이해
  • Spring Framework의 JMS 지원과의 차이점 및 유사점 비교

📖 핵심 내용:

1. ConnectionFactory

RabbitMQ의 기본 클라이언트 (Java RabbitMQ 클라이언트)에도 ConnectionFactory가 존재합니다.
하지만 Spring AMQP에서는 자체적으로 제공하는 ConnectionFactory를 사용합니다. 해당 ConnectionFactory는 채널과 연결을 캐싱하여 반복적으로 사용할 수 있도록 해줍니다.

  • 캐싱 방식
    ConnectionFactory의 캐싱은 CachingConnectionFactory 클래스에서 이루어집니다.
    이 클래스는 RabbitMQ 클라이언트의 ConnectionFactory를 감싸서 추가적인 기능(캐싱)을 제공합니다.

2. 메시지 송수신 예제

  • a. 순수 자바로 메시지 송수신하기
ConnectionFactory connectionFactory = new CachingConnectionFactory();
AmqpAdmin admin = new RabbitAdmin(connectionFactory);
admin.declareQueue(new Queue("myqueue"));
AmqpTemplate template = new RabbitTemplate(connectionFactory);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
  • b. Spring Boot에서 메시지 송수신하기

Spring Boot 자동 설정 및 비동기 POJO 리스너 사용

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(AmqpTemplate template) {
        return args -> template.convertAndSend("myqueue", "foo");
    }

    @Bean
    public Queue myQueue() {
        return new Queue("myqueue");
    }

    @RabbitListener(queues = "myqueue")
    public void listen(String in) {
        System.out.println(in);
    }

}

3. AMQP 리소스 관리

@RabbitListener를 사용할 경우, 리스너 컨테이너가 자동으로 생성되고 이를 통해 큐와 연결된 채널이 관리됩니다.

  • SimpleRabbitListnerContainerFactory : 멀티스레드 기반으로 메시지 처리 가능. 기본 리스너 컨테이너
  • DirectRabbitListnerContainerFactory : 메시지 전달 속도가 중요한 경우에 적합

4. RabbitTemplate

RabbitTemplate은 메시지 송수신을 위한 주요 클래스입니다.
이를 사용하여 큐에 메시지를 보내거나 메시지를 받을 수 있습니다.
RabbitTemplate에는 메시지 컨버터를 설정하여 객체를 메시지로 변환하거나, 메시지를 객체로 변환할 수 있습니다.

5. 메시지 컨버터

Spring AMQP는 기본적으로 직렬화된 메시지를 POJO로 변환하는 메시지 컨버터를 제공합니다.

  • 기본 메시지 컨버터 종류:
    • SimpleMessageConverter: 텍스트나 기본 객체를 처리
    • Jackson2JsonMessageConverter : JSON 형식으로 변환 (주로 POJO 기반 메시지 송수신에 사용)
    • SerializerMessageConverter: 사용자 정의 직렬화
  • 사용 예제
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    template.setMessageConverter(new Jackson2JsonMessageConverter());
    return template;
}

6. Spring JMS와의 차이점

  • 프로토콜
    • Spring JMS는 JMS(Java Messaging Service) 표준을 따르고, Spring AMQP는 AMQP 프로토콜에 기반합니다.
  • 브로커
    • Spring JMS는 ActiveMQ, IBM MQ와 같은 JMS 지원 브로커를 사용하지만, Spring AMQP는 RabbitMQ를 주로 사용합니다.
  • 리스너 설정
    • Spring AMQP는 @RabbitLisnter를 제공
    • Spring JMS는 @JmsListner를 제공

7. 에러 처리와 재처리

  • Spring AMQP에서는 전역 에러 처리기와 재처리 로직을 설정할 수 있습니다.
  • DLX 제공 : 메시지를 실패 처리 후 별도의 큐로 이동

8. RabbitMQ의 Exchange 유형

  • Direct Exchange: 특정 라우팅 키를 사용하는 메시지를 전달.
  • Fanout Exchange: 모든 큐로 메시지를 브로드캐스트.
  • Topic Exchange: 라우팅 키의 패턴 매칭을 기반으로 전달.
  • Headers Exchange: 메시지 헤더를 기준으로 큐에 전달.

💡 참고 자료:
spring amqp

@suna-ji suna-ji added the 문서화 문서화 label Jan 6, 2025
@suna-ji suna-ji added this to the version 1.0.0 milestone Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
문서화 문서화
Projects
None yet
Development

No branches or pull requests

1 participant