出版商确认 - 兔子mq

时间:2018-04-02 12:12:09

标签: spring-boot

我想在我的代码中实现发布商确认功能,通过http://www.rabbitmq.com/blog/2011/02/10/introducing-publisher-confirms/但无法理解

我正在使用QueueBuilder,如下所示

@Bean
public Queue salesQueue() {
    return QueueBuilder.durable(salesQueue)
        .withArgument("x-max-length", 1)
        .withArgument("x-overflow", "reject-publish")
        .build();
}

RabbitTemplate创建

@Bean
public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) 
{
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMessageConverter(jsonMessageConverter());
    rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
    if (ack) {
       System.out.println("Message with correlation ID {} confirmed by 
       broker."+ correlationData.getId());
    } else {
            System.out.println("Broker is unable to handle message with 
            correlation ID {} : {}"+correlationData.getId()+","+
                    cause);
           }

    });
return rabbitTemplate;
}

将消息发送到队列

@Service
public class RabbitMQSender {
private static long sentMessageCount =0L;
@Autowired
private AmqpTemplate rabbitTemplate;

@Value("${rabbitmq.queue}")
String queueName;   
public void send() {
    long x=++sentMessageCount;
    rabbitTemplate.convertAndSend(queueName," 
    {'empId':'"+x+"','empName':'YYYYYY'}");
}

目前我已将队列长度设置为1,每当我发送大于1的消息时,我希望通过basic.nack消息通知发布者拒绝消息...

我请你帮我这个

谢谢

0 个答案:

没有答案