Spring AMQP rabbittemplate 发布确认
Spring AMPQ rabbit-template publish confirmation
我正在尝试在 RabbitTemplate 的帮助下使用 RabbitMq(我正在使用 spring 引导应用程序)。我能够进行通信部分并且它对我来说工作正常,但我有一个问题。
问题:如何在发布消息后获得确认?我没有在 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
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;
}
当我在发送实际消息之前设置回调和确认回调时
我遇到异常
`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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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?
上面 link 的答案:
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.
我正在尝试在 RabbitTemplate 的帮助下使用 RabbitMq(我正在使用 spring 引导应用程序)。我能够进行通信部分并且它对我来说工作正常,但我有一个问题。
问题:如何在发布消息后获得确认?我没有在 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
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;
}
当我在发送实际消息之前设置回调和确认回调时 我遇到异常
`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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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?
上面 link 的答案:
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.