有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我们正在使用MessageRecoverer对业务异常进行重试操作,并在一些尝试后存储消息,因此我们有了第一个XML配置,用于重试,如最大尝试次数和间隔等。因此,在参考了此链接中重试的公共属性后https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#%20RABBIT 现在改为属性文件

春天。兔子。listener它是异步的,并且有很多特性,比如无状态和并发性

春天。兔子。模板其同步

但除了异步和同步之外,这两种操作都是相同的。还有一个问题。请纠正我,如果我错了,哪一个有更有效的方式,如在性能

更新帖子

如果我们得到基于重试的异常,则必须执行如下操作

1)如果出现业务异常,请重试3次

2)如果发生运行时异常**,请重试1次

3)然后必须通过messagerecover和store exception进行恢复

主课

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

共 (1) 个答案

  1. # 1 楼答案

    你的问题不清楚

    But both are doing the same operation except asynchronous and synchronous

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

    接收时,消息驱动通常更有效;轮询通常只用于按需接收消息

    为了使重试更复杂(例如定制消息恢复程序,根据异常类型将RetryTemplate配置为有条件重试),您需要自己定义bean(RabbitTemplateSimpleRabbitListenerContainerFactory),而不是使用Spring Boot的默认bean