2017-02-21 3 views
0

MessageRecovererを使用してビジネス例外の再試行操作とメッセージの保存を行っているので、最大試行と間隔などの再試行のためにXMLの最初の設定を行った後の査読共通のプロパティは、再試行のために、このリンクで今のプロパティに変更https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#%20RABBIT は、それが非同期ウサギのリスナーとRabbitのテンプレートと再試行のプロパティ

spring.rabbitmq.listenerを提出し、それがステートレスと同時実行

などの機能の多くを持っていましたspring.rabbitmq.templateその同期

しかし、どちらも非同期と同期のもう1つの質問以外は同じ操作をしています。私が間違っていて、どちらがパフォーマンスのようなより効率的な方法を持っているかを親切に修正してください。

更新後の

我々は再試行に基づく例外を取得している場合

1のように実行することがあります)ビジネス例外は、ランタイム例外が**発生した場合、3回

2)のために再試行が発生した場合1回再試行してください

3)メッセージリポートとストア例外によって回復する必要があります

メインクラス

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

のようにデバッグした後、エラーを取得しています。

しかし、両方が真実ではない非同期および同期

除いて同じ操作を行っています。リスナーはメッセージ(メッセージ駆動型)のみを受信でき、テンプレートはメッセージを送信または受信(ポーリング)できます。

受信時には、メッセージ駆動型が一般的に効率的です。ポーリングは、通常、オンデマンドメッセージ受信にのみ使用されます。このような例外の種類に基づいて、条件付き再試行を持っているRetryTemplateを設定し、メッセージ復元器をカスタマイズするなど、再試行して、より洗練された(のために

、あなたはむしろ春ブーツのデフォルトの豆を使用するよりも豆(RabbitTemplateSimpleRabbitListenerContainerFactory自分自身を定義する必要があります。

+0

Thxには条件付き再試行のためのドキュメントまたはサンプルリファレンスがあります – kumar

+0

私はメッセージを送信するためにウサギテンプレートを使用していますが、リスナーを使用してリスナーを使用していますか? – kumar

+0

注入された[SimpleRetryPolicy'のjavadocs](http://docs.spring.io/spring-retry/docs/api/current/index.html?org/springframework/retry/policy/SimpleRetryPolicy.html)を参照してください。再試行テンプレートに - 例外や動作のマップを提供しました。 '... RuntimeException:true、... MyFooException:false'です。 –

関連する問題