2015-09-28 18 views
8

C'è un modo per ottenere il motivo che un errore HystrixCommand non è riuscito quando si utilizza l'annotazione @HystrixCommand in un'applicazione Spring Boot? Sembra che se implementi il ​​tuo HystrixCommand, hai accesso allo getFailedExecutionException ma come puoi accedervi quando usi l'annotazione? Mi piacerebbe essere in grado di fare cose diverse nel metodo fallback basato sul tipo di eccezione che si è verificato. È possibile?Ottieni l'eccezione di errore nel metodo di fallback @HystrixCommand

Ho visto un note su HystrixRequestContext.initializeContext() ma il HystrixRequestContext non ti dà accesso a nulla, c'è un modo diverso di usare quel contesto per ottenere l'accesso alle eccezioni?

risposta

7

non ho trovato un modo per ottenere l'eccezione con annotazioni sia, ma creare il mio Comando lavorato per me in questo modo:

public static class DemoCommand extends HystrixCommand<String> { 

    protected DemoCommand() { 
     super(HystrixCommandGroupKey.Factory.asKey("Demo")); 
    } 

    @Override 
    protected String run() throws Exception { 
     throw new RuntimeException("failed!"); 
    } 

    @Override 
    protected String getFallback() { 
     System.out.println("Events (so far) in Fallback: " + getExecutionEvents()); 
     return getFailedExecutionException().getMessage(); 
    } 

} 

Speriamo che questo aiuta qualcun altro pure.

2

non riuscivo a trovare un modo per ottenere l'eccezione con le annotazioni, ma ho trovato HystrixPlugins, con che è possibile registrare un HystrixCommandExecutionHook e si può ottenere l'eccezione esatto in quanto in questo modo:

HystrixPlugins.getInstance().registerCommandExecutionHook(new HystrixCommandExecutionHook() { 
      @Override 
      public <T> void onFallbackStart(final HystrixInvokable<T> commandInstance) { 

      } 
     }); 

L'istanza di comando è un GenericCommand.

+0

Ma questo li catturerà genericamente ad ogni esecuzione di un comando e non sarà legato all'esecuzione specifica del comando corretta? Non proprio quello che cercavo, ma potrebbe comunque essere utile in alcuni casi in cui è necessario conoscere i fallimenti. –

24

Basta aggiungere un parametro Throwable al metodo di fallback e riceverà l'eccezione che il comando originale ha prodotto.

Da https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica

@HystrixCommand(fallbackMethod = "fallback1") 
    User getUserById(String id) { 
     throw new RuntimeException("getUserById command failed"); 
    } 

    @HystrixCommand(fallbackMethod = "fallback2") 
    User fallback1(String id, Throwable e) { 
     assert "getUserById command failed".equals(e.getMessage()); 
     throw new RuntimeException("fallback1 failed"); 
    } 
1

maggior parte del tempo usando solo getFailedExecutionException(). GetMessage() mi ha dato valori nulli.

Exception errorFromThrowable = getExceptionFromThrowable(getExecutionException()); 
    String errMessage = (errorFromThrowable != null) ? errorFromThrowable.getMessage() 

questo mi dà risultati migliori tutto il tempo.

Problemi correlati