gpt4 book ai didi

java - 重试属性中的 Rabbit 监听器与 Rabbit 模板

转载 作者:行者123 更新时间:2023-11-30 02:38:46 29 4
gpt4 key购买 nike

我们正在对业务异常进行重试操作,并使用 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

最佳答案

你的问题不清楚。

But both are doing the same operation except asynchronous and synchronous

事实并非如此;监听器只能接收消息(消息驱动),模板可以发送或接收(轮询)消息。

接收时,消息驱动一般效率更高;轮询通常仅用于按需消息接收。

要实现更复杂的重试(例如自定义消息恢复程序、将 RetryTemplate 配置为基于异常类型进行有条件重试),您需要定义 bean (RabbitTemplate) >, SimpleRabbitListenerContainerFactory 自己创建,而不是使用 Spring Boot 的默认 bean。

关于java - 重试属性中的 Rabbit 监听器与 Rabbit 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42373876/

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