Ack for Spring Rabbit Mq示例

时间:2017-03-06 07:07:30

标签: spring-boot rabbitmq

我是使用Spring Boot Integration的Rabbit MQ的新手。我从春季网站上获得了一个演示示例。在示例中,我从我的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;
        }

}

1 个答案:

答案 0 :(得分:1)

你误解了消息传递原则。 SenderReceiver是完全不同的组件,通常他们彼此不了解。

只需放置一条消息并从Broker获得消息,消息就在队列中。

Receiver只是从队列中获取一条消息,并向Broker确认该消息可以从队列中删除。

发件人与收件人之间没有联系。

如果您想要通知发件人接收者有消息,您应该考虑使用请求/回复模式:http://docs.spring.io/spring-amqp/reference/html/_reference.html#request-reply

此处有更多样本,BTW:https://github.com/spring-projects/spring-amqp-samples