Depois que eu implementei o exchange, nada é printado pelos consumidores, a exchange foi criada e as filas tambem
segue o código:
@PostMapping
public ResponseEntity<PagamentoDto> cadastrar(@RequestBody @Valid PagamentoDto dto, UriComponentsBuilder uriBuilder) {
PagamentoDto pagamento = pagamentoService.criarPagamento(dto);
URI endereco = uriBuilder.path("/pagamentos/{id}").buildAndExpand(pagamento.getId()).toUri();
rabbitTemplate.convertAndSend("pagamentos.ex", pagamento);
return ResponseEntity.created(endereco).body(pagamento);
}
package br.com.alurafood.pagamentos.amqp;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PagamentoAMQPConfiguration {
//Comentei porque não vai ser criado mais a a fila, e sim uma exchange para enviar para dois MS
// @Bean
// public Queue criaFila() {
//// return new Queue("pagamento.concluido",false);
// return QueueBuilder.nonDurable("pagamento.concluido").build();
// }
@Bean
public RabbitAdmin criaRabbitAdmin(ConnectionFactory conn) {
return new RabbitAdmin(conn);
}
@Bean
public ApplicationListener<ApplicationReadyEvent> inicializaAdmin(RabbitAdmin rabbitAdmin){
return event -> rabbitAdmin.initialize();
}
@Bean
public Jackson2JsonMessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory conn, Jackson2JsonMessageConverter jackson2JsonMessageConverter) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(conn);
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
return rabbitTemplate;
}
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("pagamentos.ex");
}
}
package br.com.alurafood.pedidos.amqp;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PedidoAMQPConfiguration {
@Bean
public Jackson2JsonMessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory conn, Jackson2JsonMessageConverter jackson2JsonMessageConverter) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(conn);
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
return rabbitTemplate;
}
@Bean
public Queue filaDetalhesPedido() {
return QueueBuilder.nonDurable("pagamentos.detalhes-pedido")
.build();
}
@Bean
public FanoutExchange fanoutExchange() {
return ExchangeBuilder.fanoutExchange("pagamentos.ex").build();
}
@Bean
public Binding bindPagamentoPedido(FanoutExchange fanoutExchange) {
return BindingBuilder.bind(filaDetalhesPedido())
.to(fanoutExchange());
}
@Bean
public RabbitAdmin criaRabbitAdmin(ConnectionFactory conn) {
return new RabbitAdmin(conn);
}
@Bean
public ApplicationListener<ApplicationReadyEvent> inicializaAdmin(RabbitAdmin rabbitAdmin){
return event -> rabbitAdmin.initialize();
}
}