Search

MQ & Spring

1. 설정

가. Basic Settings

1) 기본 설정

참고 코드

2) Queue 복수 설정

나. Producer

1) 코드 분석

@Component public class MqProducer implements CommandLineRunner { // 생략 @Override public void run(String... args) { System.out.println("Sending message..."); rabbitTemplate.convertAndSend(topicExchange, "cg.log.view.detail", args); } }
Java
복사

다. Listener

1) 코드 분석

@Service public class MqListener { // 생략 @RabbitListener(queues = "cg_q") public void receiveMessage(final Message message) { // 생략 logger.info("Logging Request Parameters in Interceptor: height={}, monthAfterBirth={}, sex={}", height, monthAfterBirth, sex); repository.save(new HeightRequestLog(height, monthAfterBirth, sex)); } }
Java
복사

라. 이슈

1) NumberFormatException

Message Body 내 빈값이 들어간 경우, 예외처리 필요
try { Float height = Float.parseFloat(bodyAsList.get(HEIGHT)); Integer monthAfterBirth = Integer.parseInt(bodyAsList.get(MONTH)); Sex sex = converter.convert(bodyAsList.get(SEX)); logger.info("Logging Request Parameters in Interceptor: height={}, monthAfterBirth={}, sex={}", height, monthAfterBirth, sex); repository.save(new HeightRequestLog(height, monthAfterBirth, sex)); } catch (NumberFormatException e) { logger.info("NumberFormatException Caused by {}", e.getMessage()); } catch (IllegalArgumentException e) { logger.info("Invalid Request Param in Sex Caused by {}", e.getMessage()); }
Java
복사

Reference

기본 세팅