2017-05-09 28 views
1

私はSpringブート1.4.1-RELEASEとRabbitMQ 3.2.3を使用しています。RabbitMQでの例外処理Springブートアプリケーション

@SpringBootApplication 
@EnableAutoConfiguration 
public class EventStoreMessageDeliveryApplication { 

    public final static String queueName = "customer.default.queue"; // spring-boot 

    @Bean 
    Queue queue() { 
     return new Queue(queueName, true); 
    } 

    @Bean 
    FanoutExchange exchange() { 

     return new FanoutExchange("customer.events.fanout.exchange", true, false); // spring-boot-exchange 
    } 

    @Bean 
    Binding binding() { 

     return new Binding(queueName, Binding.DestinationType.QUEUE, "customer.events.fanout.exchange", "*.*", null); 
    } 

    @Bean 
    public ConnectionFactory connectionFactory() { 

     CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost"); 
     connectionFactory.setPublisherConfirms(true); 

     return connectionFactory; 
    } 

    @Bean 
    SimpleMessageListenerContainer container(MessageListenerAdapter listenerAdapter) { 

     SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); 
     container.setConnectionFactory(connectionFactory()); 
     container.setQueueNames(queueName); 
     container.setMessageListener(listenerAdapter); 
     container.setRecoveryBackOff(new ExponentialBackOff(3000, 2)); 
     return container; 
    } 

    @Bean 
    MessageListenerAdapter listenerAdapter(Receiver receiver) { 

     return new MessageListenerAdapter(receiver, "receiveMessage"); 
    } 

    public static void main(String[] args) throws InterruptedException { 

     SpringApplication.run(EventStoreMessageDeliveryApplication.class, args); 
    } 
} 

そして、私のリスナークラスは次のようになります - - 私のApplicationクラスは次のようになり、接続がブローカーがダウンしているときに来る可能性が拒否したように、私は例外を処理したい

@Component 
public class Receiver { 

    private CountDownLatch latch = new CountDownLatch(1); 

    public void receiveMessage(String message) { 

     System.out.println("Received <" + message + ">"); 

      // do something 

     latch.countDown(); 
    } 

    public CountDownLatch getLatch() { 

     return latch; 
    } 

} 

。どのようにしてそのような例外を処理できますか?私は例外のハンドルをどこで得ることができるのか分かりません。

答えて

0

SimpleRabbitListenerContainerFactoryを作成できます。これは基本的にRabbitConnectionFactoryのイベントのリスナーです。

@Bean 
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() { 
     SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); 
     factory.setConnectionFactory(connectionFactory()); 
     factory.setErrorHandler(rabbitErrorHandler()); 
     return factory; 
    } 

rabbitErrorHandlerは()org.springframework.util.ErrorHandlerの実装のビーンを返すことができます。 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/ErrorHandler.html