How to configure and get iSonvert jSON payload in Object domain in Spring Boot and RabbitMQ

I recently had a lot of interest in Microservice architecture using Spring Boot. My implementation has two Spring boot applications;

The One application receives requests from the RESTful API, converts and sends the jSON payload to the RabbitMQ queue .

Application 2 , subscribed to queueA , receives a jSON payload (user of the Object domain) and is supposed to activate the service within the framework of Two, for example. send an email to the user.

Without using XML in my Application Two configuration , how do I configure a converter that converts the jSON payload received from RabbitMQ into a user of a domain object.

Below are snippets from Spring Boot Configuration on the second application

Application.class

@SpringBootApplication
@EnableRabbit
public class ApplicationInitializer implements CommandLineRunner {

    final static String queueName = "user-registration";

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AnnotationConfigApplicationContext context;

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("user-registrations");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

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

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Waiting for messages...");
    }

}

TestService.java

@Component
public class TestService {

    /**
     * This test verifies whether this consumer receives message off the user-registration queue
     */
    @RabbitListener(queues = "user-registration")
    public void testReceiveNewUserNotificationMessage(User user) {
        // do something like, convert payload to domain object user and send email to this user
    }

}
+5
source share
3 answers

I had the same problem, and after some research and testing that I found out, there is more than one way to configure RabbitMQ-Receiver in SpringBoot, but it is important to choose one and stick to it.

, , @EnableRabbit @RabbitListener, . :

org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer configureRabbitListeners :

 @Override
public void configureRabbitListeners(
        RabbitListenerEndpointRegistrar registrar) {
    registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
}

MessageHandlerFactory:

@Bean
public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
    DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
    factory.setMessageConverter(new MappingJackson2MessageConverter());
    return factory;
}

, SimpleRabbitListenerContainerFactory ( ) Autowire ConnectionFactory:

@Autowired
public ConnectionFactory connectionFactory;

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setConcurrentConsumers(3);
    factory.setMaxConcurrentConsumers(10);
    return factory;
}

, Bean, @RabbitListerner-Annotations. EventResultHandler ( TestService):

    @Bean
public EventResultHandler eventResultHandler() {
    return new EventResultHandler();
}

EventResultHandler ( TestService) @RabbitListener- (= POJO, JSON- ):

@Component
public class EventResultHandler {

    @RabbitListener(queues=Queues.QUEUE_NAME_PRESENTATION_SERVICE)
    public void handleMessage(@Payload Event event) {
        System.out.println("Event received");
        System.out.println("EventType: " + event.getType().getText());
    }
}

- Microservice, RabbitMQ-Server ... .

+10

Jackson MessageListenerAdapter#setMessageConverter

@Bean
public MessageConverter jsonMessageConverter() {
    return new Jackson2JsonMessageConverter();
}

MessageListenerAdapter?

+6

Spring Boot 2.1.4.RELEASE :

  1. "" RabbitMq :
    @Bean
    RabbitTemplate rabbitTemplate(RabbitTemplate rabbitTemplate) {
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
           return rabbitTemplate
        }
  1. :
        var receievedValie = rabbitTemplate.receiveAndConvert("TestQueue", new ParameterizedTypeReference<Integer>() {
            @Override
            public Type getType() {
                return super.getType();
            }
        })
0
source

All Articles