我们正在进行业务异常的重试操作,并在使用 MessageRecoverer 进行一些尝试后存储消息,因此我们在XML中进行了第一次配置,用于重试,例如最大尝试次数和间隔等等。此链接中重试的属性https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#%20RABBIT 现在已更改为属性文件
spring.rabbitmq.listener 它是异步的,它有很多像无状态和并发的功能
spring.rabbitmq.template 其同步
但是除了异步和同步之外,两者都在做同样的操作。如果我错了,那么请纠正我,哪个人在表现方面更有效率。
更新帖子
如果我们基于重试获得异常必须执行
1)如果发生商业异常重试3次
2)如果运行时异常**重试1次
3)然后必须通过messagerecover恢复并存储异常
主要班级
public class Main {
@SuppressWarnings("resource")
public static void main(String[] args) {
new ClassPathXmlApplicationContext("/applicationContext.xml");
}
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd">
<!-- Spring configuration -->
<context:component-scan base-package="com.spring.rabbit.first.*" />
<context:mbean-export default-domain="com.spring.rabbit.first.deadletter" />
<!-- RabbitMQ common configuration -->
<rabbit:connection-factory id="connectionFactory"
username="guest" password="guest" port="5672" virtual-host="/" host="localhost" />
<!-- <rabbit:connection-factory id="connectionFactory"/> -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- Queues -->
<!-- <rabbit:queue id="springQueue" name="spring.queue" -->
<!-- auto-delete="true" durable="false" /> -->
<rabbit:listener-container
connection-factory="connectionFactory" advice-chain="retryAdvice">
<rabbit:listener queues="BBBqueue" ref="messageListener" />
</rabbit:listener-container>
<rabbit:listener-container
connection-factory="connectionFactory" advice-chain="retryAdvice">
<rabbit:listener queues="DDDqueue" ref="messageListener" />
</rabbit:listener-container>
<bean id="messageListener" class="com.spring.rabbit.first.deadletter.MessageHandler" />
<bean id="retryAdvice"
class="org.springframework.amqp.rabbit.config.StatelessRetryOperationsInterceptorFactoryBean">
<property name="messageRecoverer" ref="rejectAndDontRequeueRecoverer" />
<property name="retryOperations" ref="retrytest" />
</bean>
<bean id="rejectAndDontRequeueRecoverer"
class="com.spring.rabbit.first.deadletter.AutoConfiguringRepublishMessageRecoverer" />
<!-- <constructor-arg ref="amqpTemplate" </constructor-arg> -->
<!-- <constructor-arg name="errorTemplate" value="test"</constructor-arg> -->
<!-- </bean> -->
<!-- <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy"> -->
<!-- <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy"> -->
<!-- <property name="initialInterval" value="2000" /> -->
<!-- <property name="multiplier" value="10.0" /> -->
<!-- <property name="maxInterval" value="30000" /> -->
<!-- </bean> -->
<!-- </property> -->
<!-- <property name="retryPolicy"> -->
<!-- <bean class="org.springframework.retry.policy.SimpleRetryPolicy"> -->
<!-- <property name="retry" value="retrytest" /> -->
<!-- </bean> -->
<!-- </property> <property name="retryPolicy" ref="retrytest"></property>
</bean> -->
<bean id="retrytest" class="com.spring.rabbit.first.retry.RetryOperationTest" />
<rabbit:topic-exchange name="AAAqexchnage">
<rabbit:bindings>
<rabbit:binding queue="BBBqueue" pattern="" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:queue name="BBBqueue"></rabbit:queue>
<rabbit:topic-exchange name="CCCexchange">
<rabbit:bindings>
<rabbit:binding queue="DDDqueue" pattern="" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:queue name="DDDqueue"></rabbit:queue>
</beans>
消息处理程序
public class MessageHandler implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("Received message: " + message);
System.out.println("Text: " + new String(message.getBody()));
if(message!=null)
{
message = null;
if (message == null) {
throw new NullPointerException();
}
}
}
}
@Configuration
public class RetryOperationTest {
@Bean
public RetryTemplate retryTemplate() {
final RetryTemplate ret = new RetryTemplate();
ret.setRetryPolicy(retryPolicy());
return ret;
}
@Bean
public RetryPolicy retryPolicy() {
final Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>() {{
put(RuntimeException.class, true);
}
};
final RetryPolicy ret = new SimpleRetryPolicy(1, map, true);
return ret;
}
}
调试后出现错误,如
00:33:41.233 [main] WARN org.springframework.context.support.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer#0': Cannot resolve reference to bean 'retryAdvice' while setting bean property 'adviceChain'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'retryAdvice' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [com.spring.rabbit.first.retry.RetryOperationTest$$EnhancerBySpringCGLIB$$649b8c8] to required type [org.springframework.retry.RetryOperations] for property 'retryOperations'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.spring.rabbit.first.retry.RetryOperationTest$$EnhancerBySpringCGLIB$$649b8c8] to required type [org.springframework.retry.RetryOperations] for property 'retryOperations': no matching editors or conversion strategy found
答案 0 :(得分:2)
你的问题不明确。
但是除了异步和同步
之外,两者都在做同样的操作
事实并非如此;监听器只能接收消息(消息驱动),模板可以发送或接收(轮询)消息。
接收时,消息驱动通常更有效;轮询通常仅用于按需消息接收。
为了更复杂的重试(例如自定义消息恢复器,将RetryTemplate
配置为根据异常类型进行条件重试,您需要定义bean(RabbitTemplate
,SimpleRabbitListenerContainerFactory
自己而不是使用Spring Boot的默认bean。