工作队列和重试队列

时间:2015-08-27 21:43:18

标签: spring-amqp spring-rabbitmq

我试图以递归方式实现死字法。我有两个队列,一个主队列和一个重试队列。 消息将首先放入主队列中。在使用者进程期间发生异常时,它会抛出AmqpRejectAndDontRequeueException,以便从队列中删除消息并将其添加到重试队列中。它在那里停留5秒钟,然后再次添加到工作队列的尾部。 当消费者抛出AmqpRejectAndDontRequeueException'时,它不会被传递到重试队列

你能帮我解决这个问题吗?

以下是配置,生产者和消费者文件。

producer config:

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory()
    connectionFactory.setUsername(userName)
    connectionFactory.setPassword(password)
    connectionFactory.setAddresses(hosts)
    return connectionFactory
}

@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory())
}

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory())
    template.setMessageConverter(jsonMessageConverter());
    return template
}

@Bean
public MessageConverter jsonMessageConverter() {
    return new Jackson2JsonMessageConverter()
}

@Bean
public DirectExchange searchNotificationExchange() {
    return new DirectExchange("search.notification.exchange", false, true)
}

@Bean
public Queue searchNotificationQueue() {
    Map<String, Object> args = new HashMap<String, Object>()
    args.put("x-dead-letter-exchange", "search.notification.retry.exchange")
    return new Queue(SEARCH_NOTIFICATION_QUEUE_NAME, true, false, false, args)
}

@Bean
public Binding binding() {
    return BindingBuilder.bind(searchNotificationQueue()).to(searchNotificationExchange()).with(SEARCH_NOTIFICATION_QUEUE_NAME);
}

@Bean
public DirectExchange searchNotificationRetryExchange() {
    return new DirectExchange("search.notification.retry.exchange", false, true)
}

@Bean
public Queue searchNotificationRetryQueue() {
    Map<String, Object> args = new HashMap<String, Object>()
    args.put("x-dead-letter-exchange", "search.notification.exchange")
    args.put("x-message-ttl",5000)
    return new Queue(SEARCH_NOTIFICATION_RETRY_QUEUE_NAME, true, false, false, args)
}

@Bean
public Binding retryBinding() {
    return BindingBuilder.bind(searchNotificationRetryQueue()).to(searchNotificationRetryExchange()).with(SEARCH_NOTIFICATION_RETRY_QUEUE_NAME);
}

从生产者配置

扩展的消费者配置
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory()
    factory.setConnectionFactory(connectionFactory())
    factory.setConcurrentConsumers(3)
    factory.setMaxConcurrentConsumers(10)
    factory.setMessageConverter(jsonMessageConverter())
    return factory
}

生产者:

try {
                    rabbitTemplate.convertAndSend(RabbitConfiguration.SEARCH_NOTIFICATION_QUEUE_NAME, sd);
                    println sd.deliveryId
                } catch(AmqpException amqbe) {
                    log.error("Error putting message into search notification queue.", amqbe)
                }

消费者:

@RabbitListener(queues =RabbitConfiguration.SEARCH_NOTIFICATION_QUEUE_NAME)
public void handleMessage(SearchNotificationMQMessage messageBody, Message message) {
try {
//process message
}  catch(Throwable t) {

        if(message.messageProperties.deliveryTag < 3) {
            throw new AmqpRejectAndDontRequeueException()
        }
    }

0 个答案:

没有答案