gpt4 book ai didi

Spring AMQP Rabbittemplate 发布确认

转载 作者:行者123 更新时间:2023-12-02 11:49:40 25 4
gpt4 key购买 nike

我正在尝试在 RabbitTemplate 的帮助下使用 RabbitMq(我正在使用 Spring Boot 应用程序)。我能够完成通信部分,并且它工作正常,但我有一个问题。

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

****************编辑************************

正如 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

    @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;
}

当我在发送实际消息之前设置Call​​back和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)

`

最佳答案

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

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

The RabbitTemplate implementation of AmqpTemplate supports Publisher Confirms and Returns.

依此类推,直到:

Only one ConfirmCallback is supported by a RabbitTemplate.

更新

请仔细阅读 StackTrace 及其文档:

java.lang.IllegalStateException: Channel does not support confirms or returns; is the connection factory configured for confirms or returns?

上面链接的答案:

3.2.4 Publisher Confirms and Returns

Confirmed and returned messages are supported by setting the CachingConnectionFactory's publisherConfirms and publisherReturns properties to 'true' respectively.

关于Spring AMQP Rabbittemplate 发布确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31918800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com