InterviewSolution
| 1. |
What is AMQP? What does Spring Boot offer with respect to Messaging with AMQP? |
|
Answer» AMQP is a platform-independent protocol. Spring Boot provides Starter for AMQP spring-boot-starter-amqp. With this starter imported, if you have RabbitMQ running , Spring Boot will auto configure connection to RabbitMQ from your application. If you want to customize the configuration, you can use: spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=secret Additional Properties for Retry : spring.rabbitmq.template.retry.enabled=true spring.rabbitmq.template.retry.initial-interval=2s You can use AMQPTemplate or RabbitMessagingTemplate for sending messages similar to JMSTemplate. For receiving messages, you can use @RabbitListener(queue=”MyQueue”) @COMPONENT PUBLIC class SomeBean { @RabbitListener(destination=”somequeue”) public VOID processMessage(String content) { } }If you want to override DEFAULT Connection Factory, you can make use of SimpleRabbitListenerContainerFactoryConfigurer as follows : @Bean public SimpleRabbitListenerContainerFactory myFactory( SimpleRabbitListenerContainerFactoryConfigurer configurer) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); configurer.configure(factory, connectionFactory); factory.setMessageConverter(myMessageConverter()); return factory; }In this case , your listener will be changed to : @Component public class SomeBean { @RabbitListener(destination=”somequeue” containerFactory=”myAMQPFactory”) public void processMessage(String content) { } } |
|