13 Mayıs 2020 Çarşamba

SpringBoot Amqp RabbitMQ Kullanımı - FanoutExchange Sınıfı

Giriş
FanoutExchange kullanırken routing key önemsizdir. Tek yapmamız gereken FanoutExchange'e bir kuyruk bağlamak

Örnek
Şö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
@Configuration
public class FanoutExchangeConfiguration {

    private static String fanoutExchange;

    @Value("${broker.exchange.fanout.name}")
    private void setFanoutExchange(String fanoutExchange) {
        FanoutExchangeConfiguration.fanoutExchange = fanoutExchange;
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange(FanoutExchangeConfiguration.fanoutExchange);
    }
}
FanoutExchange'e kuyruk bağlamak için şöyle yaparız
@Configuration
public 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;
    }

    @Bean
    Queue queue() {
        return new Queue(BrokerConfiguration.shipExchangeQueue);
    }

  @Bean
  Binding bindingToDirectExchange(Queue commonQueue, DirectExchange directExchange) {
    return BindingBuilder.bind(commonQueue).to(directExchange)
.with(BrokerConfiguration.shipRoutingKey);
  }

  @Bean
  Binding bindingToFanoutExchange(Queue commonQueue, FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(commonQueue).to(fanoutExchange);
  }
}
Örnek - FanoutExchange
Şöyle yaparız
@Configuration
public class FanoutConfigure {

  @Bean
  public Queue fanoutMessageA() {
    return new Queue("fanout.messageA");
  }
  @Bean
  public Queue fanoutMessageB() {
    return new Queue("fanout.messageB");
  }
  @Bean
  public Queue fanoutMessageC() {
    return new Queue("fanout.messageC");
  }

  @Bean 
  public FanoutExchange exchangeFanout() {/ / Declare a fanout switch
    return new FanoutExchange("fanoutExchange");
  }

  @Bean
  Binding bindingExchangeA(FanoutExchange fanoutExchange) {/ / Bind the queue to the switch
    return BindingBuilder.bind(fanoutMessageA()).to(fanoutExchange);
  }
  @Bean
  Binding bindingExchangeB(FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(fanoutMessageB()).to(fanoutExchange);
  }
  @Bean
  Binding bindingExchangeC(FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(fanoutMessageC()).to(fanoutExchange);
  }
}
Mesaj gönder göndermek için şöyle yaparız
@Component
public class FanoutSender {

  @Autowired
  RabbitTemplate 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