RabbitMQ以编程方式配置重试

时间:2019-12-12 13:05:19

标签: java spring rabbitmq

我无法正确配置重试机制。

当侦听器收到消息时,会故意抛出异常。这应该触发重试机制。而是将消息重新排队,从而在Rabbitmq和侦听器之间产生乒乓球。

package rabbittest;

import static rabbittest.RabbitConfig.QUEUE_NAME;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class Listener {

    @RabbitListener(queues = QUEUE_NAME, containerFactory = "rabbitListenerFactory")
    public void listen(@Payload final DataObject dataObject) {

        log.info("Received {}", dataObject);

        throw new RuntimeException("error :)");

    }
}
package rabbittest;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

@Configuration
public class RabbitConfig {

    public static final String QUEUE_NAME = "myQueue";
    public static final String DEAD_LETTER_QUEUE_NAME = "deadLetterQueue";

    public static final String EXCHANGE_NAME = "myExchange";
    public static final String DEAD_LETTER_EXCHANGE_NAME = "deadLetterExchange";

    public static final String ROUTING_KEY = "route";
    public static final String DEAD_LETTER_ROUTING_KEY = "dead";


    @Bean
    DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    @Bean
    DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }

    @Bean
    Queue queue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    @Bean
    Queue deadLetterQueue() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE_NAME).build();
    }

    @Bean
    Binding queueExchangeBinding(final Queue queue, final DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);

    }

    @Bean
    Binding deadLetterQueueExchangeBinding(final Queue deadLetterQueue, final DirectExchange deadLetterExchange) {
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with(DEAD_LETTER_ROUTING_KEY);

    }

    @Bean
    Jackson2JsonMessageConverter jsonMessageConverter() {

        return new Jackson2JsonMessageConverter();
    }

    @Bean
    RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory, final Jackson2JsonMessageConverter jsonMessageConverter) {

        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter);

        // Make channel transactional.
        rabbitTemplate.setChannelTransacted(true);

        return rabbitTemplate;
    }

    @Bean
    SimpleRabbitListenerContainerFactory rabbitListenerFactory(final ConnectionFactory connectionFactory) {

        final SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

        // Set connection factory.
        factory.setConnectionFactory(connectionFactory);

        // Use JSON converter.
        factory.setMessageConverter(jsonMessageConverter());

        // Configure Retry mechanism
        final RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));

        factory.setRetryTemplate(retryTemplate);

        return factory;

    }

}

我想念什么? 当我使用由spring提供的默认Listenercontainerfactory并配置了应用程序属性时,重试机制可以很好地工作...

0 个答案:

没有答案