Complete the code to declare a RabbitMQ queue bean in Spring Boot.
import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public Queue myQueue() { return new Queue([1]); } }
The Queue constructor expects a string name for the queue. It must be passed as a string literal with quotes.
Complete the code to send a message to a RabbitMQ exchange using RabbitTemplate.
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend([1], message); } }
The convertAndSend method requires the exchange name as a string literal. It must be enclosed in quotes.
Fix the error in the listener method annotation to receive messages from the queue.
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageListener { @RabbitListener(queues = [1]) public void receiveMessage(String message) { System.out.println("Received: " + message); } }
The queues attribute expects the queue name as a string literal in quotes.
Fill both blanks to declare a direct exchange and bind it to a queue with a routing key.
import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public DirectExchange directExchange() { return new DirectExchange([1]); } @Bean public Binding binding(Queue queue, DirectExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with([2]); } }
The direct exchange needs a name as a string literal. The binding requires the routing key as a string literal.
Fill all three blanks to create a message listener container bean with a connection factory, queue name, and message listener.
import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitListenerConfig { @Bean public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory([1]); container.setQueueNames([2]); container.setMessageListener([3]); return container; } }
The container needs the connection factory bean, the queue name as a string literal, and the message listener adapter bean.