Configuring a listener container

To receive the message asynchronously, the easiest way is to use an annotated listener endpoint. We will use the @RabbitListener annotation as a message listener endpoint. To create this listener endpoint, we have to configure a message listener container using the SimpleRabbitListenerContainerFactory class, which is an implementation of the RabbitListenerContainerFactory interface. The following code is used to configure SimpleRabbitListenerContainerFactory:

@Bean
public SimpleRabbitListenerContainerFactory listenerContainer() {
SimpleRabbitListenerContainerFactory factory = new
SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setMaxConcurrentConsumers(5);
return factory;
}

The listenerContainer() method will instantiate SimpleRabbitListenerContainerFactory. You can set the maximum number of consumers with the maxConcurrentConsumers property using the setMaxConcurrentConsumers() method.

The following is the class that contains all the previously discussed configuration methods:

@Configuration
@ComponentScan("com.packt.springhighperformance.ch7.bankingapp")
@EnableRabbit
public class RabbitMqConfiguration {

public static final String RABBIT_MESSAGE_QUEUE =
"rabbit.queue.name";
private static final String RABBIT_MESSAGE_EXCHANGE =
"rabbit.exchange.name";
private static final String ROUTING_KEY = "messages.key";

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new
CachingConnectionFactory("127.0.0.1");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}

@Bean
public Queue queue() {
return new Queue(RABBIT_MESSAGE_QUEUE, true);
}

@Bean
public DirectExchange exchange() {
return new DirectExchange(RABBIT_MESSAGE_EXCHANGE);
}

@Bean
Binding exchangeBinding(DirectExchange directExchange, Queue queue) {
return
BindingBuilder.bind(queue).to(directExchange).with(ROUTING_KEY);
}

@Bean
public RabbitAdmin rabbitAdmin() {
RabbitAdmin admin = new RabbitAdmin(connectionFactory());
admin.declareQueue(queue());
admin.declareExchange(exchange());
admin.declareBinding(exchangeBinding(exchange(), queue()));
return admin;
}

@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}

@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setRoutingKey(ROUTING_KEY);
template.setExchange(RABBIT_MESSAGE_EXCHANGE);
template.setMessageConverter(messageConverter());
return template;
}

@Bean
public SimpleRabbitListenerContainerFactory listenerContainer() {
SimpleRabbitListenerContainerFactory factory = new
SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setMaxConcurrentConsumers(5);
return factory;
}

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset