0
0
Spring Bootframework~10 mins

RabbitMQ integration basics in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a RabbitMQ queue bean in Spring Boot.

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]);
    }
}
Drag options to blanks, or click blank then click option'
AmyQueue
B"myQueue"
C"queueName"
D"rabbitQueue"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without quotes instead of a string literal.
Using incorrect queue names that don't match the intended queue.
2fill in blank
medium

Complete the code to send a message to a RabbitMQ exchange using RabbitTemplate.

Spring Boot
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);
    }
}
Drag options to blanks, or click blank then click option'
A"routingKey"
BmyExchange
C"queueName"
D"myExchange"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without quotes.
Using the queue name instead of the exchange name.
3fill in blank
hard

Fix the error in the listener method annotation to receive messages from the queue.

Spring Boot
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);
    }
}
Drag options to blanks, or click blank then click option'
A"myQueue"
BmyQueue
C"queueName"
D"rabbitQueue"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the queue name without quotes causing a syntax error.
Using a wrong queue name that does not exist.
4fill in blank
hard

Fill both blanks to declare a direct exchange and bind it to a queue with a routing key.

Spring Boot
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]);
    }
}
Drag options to blanks, or click blank then click option'
A"myDirectExchange"
B"myQueue"
C"routingKey"
D"directExchange"
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes.
Mixing up exchange and queue names.
5fill in blank
hard

Fill all three blanks to create a message listener container bean with a connection factory, queue name, and message listener.

Spring Boot
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;
    }
}
Drag options to blanks, or click blank then click option'
AconnectionFactory
B"myQueue"
ClistenerAdapter
D"queueName"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing queue name without quotes.
Using wrong bean names or variables.