Я читаю эту статью , в которой говорится, что при обмене сообщениями, отличными от fanout
, сообщения направляются в очередь, соответствующую их ключам.
Если это так, то почему в следующей аннотации есть два аргумента (value
для @Queue
и key
)?Разве не достаточно просто указать имя очереди?
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = RabbitMQConfiguration.AUTHENTICATION_EMAILS_QUEUE, durable = "true"), exchange = @Exchange(value = RabbitMQConfiguration.EMAIL_MESSAGE_EXCHANGE_NAME), key = "accountActivationEmail"))
public void receiveMessage(Map<String, String> message) {
this.sendEmail(getEmail(message));
}
Еще один вопрос: имея аннотацию, которую вы видели для слушателей, нужно ли мне иметь отдельный класс @Configuration
для указания MessageListenerAdapter
, SimpleMessageListenerContainer
, Binding
, Exchange
,Queue
как показано ниже:
@Configuration
public class RabbitMQConfiguration {
public final static String AUTHENTICATION_EMAILS_QUEUE = "AUTHENTICATION_EMAILS_QUEUE";
public final static String EMAIL_MESSAGE_EXCHANGE_NAME = "EMAIL_EVENTS";
@Bean
public Queue authenticationEmailsQueue() {
return new Queue(AUTHENTICATION_EMAILS_QUEUE, false);
}
@Bean
public DirectExchange emailMessagesExchange() {
return new DirectExchange(EMAIL_MESSAGE_EXCHANGE_NAME);
}
@Bean
public Binding authenticationEmailMQBinding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(AUTHENTICATION_EMAILS_QUEUE);
}
@Bean
public SimpleMessageListenerContainer authenticationEmailContainer(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(AUTHENTICATION_EMAILS_QUEUE);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
public MessageListenerAdapter authenticationEmailHandlerAdapter(AuthenticationEmailHandler receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
}