Spring AMPQ兔模板发布确认

时间:2015-08-10 11:51:00

标签: spring spring-boot rabbitmq spring-rabbit

我试图在RabbitTemplate的帮助下使用RabbitMq(我正在使用Spring启动应用程序)。我能够进行通信部分,它可以正常工作,但我有一个问题。

问题:如何在发布消息后获得确认?我没有在 RabbitTemplate 中看到任何方法,我需要它,因为我遇到了一些问题,有时我的消息没有到达服务器,这对我来说是一个问题。

**************** EDIT *********************

正如Artem Bilan建议我已经实施但仍有一些不起作用。请看我的代码。

    public boolean sendMessage() {
    try {
        String jsonMessage = convertMessageToJson(message);
        template.setQueue("test_queue");
        template.setRoutingKey("test_queue");
        template.convertAndSend(null, "test_queue", jsonMessage,
                new CorrelationData(UUID.randomUUID().toString()));
        // template.convertAndSend(jsonMessage + counter.incrementAndGet());
        new AnnotationConfigApplicationContext(
                TestMessageConfiguration.class);
    } catch (Exception e) {
        return false;
    }
    return true;
}

我的豆子

    @Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    RetryTemplate retryTemplate = new RetryTemplate();
    ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(500);
    backOffPolicy.setMultiplier(10.0);
    backOffPolicy.setMaxInterval(10000);
    retryTemplate.setBackOffPolicy(backOffPolicy);
    template.setRetryTemplate(retryTemplate);

    template.setReturnCallback(new ReturnCallback() {
        @Override
        public void returnedMessage(Message message, int replyCode,
                String replyText, String exchange, String routingKey) {
            System.out.println("Received returnedMessage with result {}"
                    + routingKey);
            log.info("Received returnedMessage with result {}", routingKey);

        }
    });

    template.setConfirmCallback(new ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack,
                String cause) {
            System.out
                    .println("*************************************************************************************"
                            + ack);
            log.info("Received confirm with result {}", ack);

        }
    });
    template.setMandatory(true);

    return template;
}

当我在发送实际消息之前setCallback和ConfirmCallback 我正在异常

`java.lang.IllegalStateException: Channel does not support confirms or returns; is the connection factory configured for confirms or returns?
    at org.springframework.amqp.rabbit.core.RabbitTemplate.addListener(RabbitTemplate.java:1189)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1039)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1028)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:540)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:605)
    at com.xx.yy.backend.messaging.MessageSenderTest.sendMessage(MessageSenderTest.java:88)
    at com.xx.yy.backend.messaging.MessageSenderTest.sendMessageTest(MessageSenderTest.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

`

1 个答案:

答案 0 :(得分:4)

请注意,实际上,RabbitTemplate具有实施PublisherCallbackChannel.Listener的选项。

您可以在Reference Manual中找到更多信息:

  

AmqpTemplate的RabbitTemplate实现支持Publisher Confirms和Returns。

依此类推:

  

RabbitTemplate只支持一个ConfirmCallback。

<强>更新

请阅读StackTrace,因此请仔细阅读文档:

  

java.lang.IllegalStateException:Channel不支持确认或返回;连接工厂是否已配置为确认或返回?

以上链接的答案:

  

3.2.4发布者确认并返回

     

通过将CachingConnectionFactory的publisherConfirms和publisherReturns属性设置为“真实”来支持已确认和返回的消息。分别。