Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spring Boot - RabbitMQ - Request-Reply Pattern
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.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class RequestReplyFixed {
private static final String EX_REPLY_REQUEST = "ex.reply-request";
private static final String MQ_REQUEST_QUEUE = "mq.request.queue";
private static final String ROUTING_KEY = "my-key";
private static final String MQ_REPLY_QUEUE = "mq.reply.queue";
public static void main(String[] args) throws Exception {
SpringApplication.run(RequestReplyFixed.class, args);
}
@Bean
public DirectExchange exchange() {
return new DirectExchange(EX_REPLY_REQUEST);
}
@Bean
public Binding binding() {
return BindingBuilder.bind(requestQueue()).to(exchange()).with(ROUTING_KEY);
}
@Bean
public Queue requestQueue() {
return new Queue(MQ_REQUEST_QUEUE);
}
@Bean
public Queue replyQueue() {
return new Queue(MQ_REPLY_QUEUE);
}
// Client - template sender
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setExchange(exchange().getName());
template.setRoutingKey(ROUTING_KEY);
template.setReplyAddress(MQ_REPLY_QUEUE);
return template;
}
// Client - listener to reply-response for the client
@Bean
public SimpleMessageListenerContainer replyListenerContainer(ConnectionFactory connectionFactory, RabbitTemplate rabbitTemplate) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueues(replyQueue());
container.setMessageListener(rabbitTemplate);
return container;
}
// Server - request consumer
@Bean
public SimpleMessageListenerContainer serviceListenerContainer(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueues(requestQueue());
container.setMessageListener(new MessageListenerAdapter(new MessageConsumer()));
return container;
}
}
@RestController
@RequestMapping("/")
class MessageController {
private RabbitTemplate rabbitTemplate;
public MessageController(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@GetMapping
public String home() throws Exception {
return (String) this.rabbitTemplate.convertSendAndReceive("Hello, world!");
}
}
class MessageConsumer {
public String handleMessage(String message) {
return message.toUpperCase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.