Giriş
FanoutExchange kullanırken routing key önemsizdir. Tek yapmamız gereken FanoutExchange'e bir kuyruk bağlamak
Örnek
Şöyle yaparız
Şöyle yaparız
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
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.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQFanoutConfig {
@Bean
Queue marketingQueue() {
return new Queue("marketingQueue", false);
}
@Bean
FanoutExchange exchange() {
return new FanoutExchange("fanout-exchange");
}
@Bean
Binding marketingBinding(Queue marketingQueue, FanoutExchange exchange) {
return BindingBuilder.bind(marketingQueue).to(exchange);
}
}
Örnek
FanoutExchange yaratmak için şöyle yaparız
@Configurationpublic class FanoutExchangeConfiguration {private static String fanoutExchange;@Value("${broker.exchange.fanout.name}")private void setFanoutExchange(String fanoutExchange) {FanoutExchangeConfiguration.fanoutExchange = fanoutExchange;}@BeanFanoutExchange fanoutExchange() {return new FanoutExchange(FanoutExchangeConfiguration.fanoutExchange);}}
FanoutExchange'e kuyruk bağlamak için şöyle yaparız
@Configurationpublic class BrokerConfiguration {static String shipExchangeQueue;static String shipRoutingKey;@Value("${broker.exchange.direct.ship.routing-key}")private void setShipRoutingKey(String routingKey) {BrokerConfiguration.shipRoutingKey = routingKey;}@Value("${broker.exchange.queue.name}")private void setExchangeQueue(String exchangeQueue) {BrokerConfiguration.shipExchangeQueue = exchangeQueue;}@BeanQueue queue() {return new Queue(BrokerConfiguration.shipExchangeQueue);}@BeanBinding bindingToDirectExchange(Queue commonQueue, DirectExchange directExchange) {return BindingBuilder.bind(commonQueue).to(directExchange)
.with(BrokerConfiguration.shipRoutingKey);}@BeanBinding bindingToFanoutExchange(Queue commonQueue, FanoutExchange fanoutExchange) {return BindingBuilder.bind(commonQueue).to(fanoutExchange);}}
Örnek - FanoutExchange
Şöyle yaparız
@Configurationpublic class FanoutConfigure {@Beanpublic Queue fanoutMessageA() {return new Queue("fanout.messageA");}@Beanpublic Queue fanoutMessageB() {return new Queue("fanout.messageB");}@Beanpublic Queue fanoutMessageC() {return new Queue("fanout.messageC");}@Beanpublic FanoutExchange exchangeFanout() {/ / Declare a fanout switchreturn new FanoutExchange("fanoutExchange");}@BeanBinding bindingExchangeA(FanoutExchange fanoutExchange) {/ / Bind the queue to the switchreturn BindingBuilder.bind(fanoutMessageA()).to(fanoutExchange);}@BeanBinding bindingExchangeB(FanoutExchange fanoutExchange) {return BindingBuilder.bind(fanoutMessageB()).to(fanoutExchange);}@BeanBinding bindingExchangeC(FanoutExchange fanoutExchange) {return BindingBuilder.bind(fanoutMessageC()).to(fanoutExchange);}}
Mesaj gönder göndermek için şöyle yaparız
@Componentpublic class FanoutSender {@AutowiredRabbitTemplate rabbitTemplate;public void sendFanout() {String message = "...";// Leave "" because you do not need to specify routerkey"rabbitTemplate.convertAndSend("fanoutExchange", "", message);}}
Hiç yorum yok:
Yorum Gönder