【问题标题】:Ack for Spring Rabbit Mq Example返回 Spring Rabbitmq 示例
【发布时间】:2017-03-06 07:07:30
【问题描述】:

我是使用 Spring Boot 集成的 Rabbit MQ 的新手。我从 spring 网站上获取了一个演示示例。在示例中,我从我的 Runner 类发送消息并从我的接收器类接收相同的消息。但我需要从接收者类向发送者类发送确认。有人可以告诉我这样做的程序是什么。以下是我的课程:

Application.java

@SpringBootApplication
public class Application {

    final static String queueName = "springRabbitQueue";

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("springRabbitQueueExchange");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }


    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return container;
    }


    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }


    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args);
    }

}

Runner.java

@Component
public class Runner implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;
    private final ConfigurableApplicationContext context;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
            ConfigurableApplicationContext context) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
        this.context = context;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(Application.queueName, "Hello from Rabit MQ Demo!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
        rabbitTemplate.setConfirmCallback(new ConfirmCallback(){

            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {

                System.out.println("confirm correlationData is : "+correlationData+"ack is : "+ ack);
            }

        });
        rabbitTemplate.setMandatory(true);

        context.close();
    }

}

Receiver.java

@Component
public class Receiver {

    private CountDownLatch latch = new CountDownLatch(1);

        public void receiveMessage(String message) {
            System.out.println("Received Demo<" + message + ">");
            latch.countDown();
        }

        public CountDownLatch getLatch() {
            return latch;
        }

}

【问题讨论】:

  • 您已将其设置为container.setAcknowledgeMode(AcknowledgeMode.AUTO);。您的侦听器已设置为自动确认...
  • 如果我需要将手动确认从我的 Receiver 类发送到 Runner 类,我该怎么办?
  • 检查所有的 AcknowledgeModes
  • 您有任何示例项目可以使用 Rabbit Mq 和 spring 集成发送手动确认吗?
  • 不,我没有。

标签: spring-boot rabbitmq


【解决方案1】:

您有点误解了消息传递原则。 SenderReceiver 是完全不同的组件,通常它们彼此不知道。

只需放置一条消息并从 Broker 那里得到消息在队列中的确认。

接收者只是从队列中获取消息并确认代理可以从队列中删除消息。

发送者和接收者之间没有联系。

如果你想实现一些东西来通知发送者接收者有一条消息,你应该考虑使用请求/回复模式:http://docs.spring.io/spring-amqp/reference/html/_reference.html#request-reply

更多示例在这里,顺便说一句:https://github.com/spring-projects/spring-amqp-samples

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多